0

I've got a problem for you.

I've got a bunch of Java files (.java) sitting around and they all contain a class declaration and an array of strings. I need to do stuff with the array. What is the best way to access it?

I tried using JavaCompiler class, but that didn't seem to work - so should I use regex or something?

Here's a sample of what the files look like:

package com.mypack.costmgr;

public class Cost_en extends java.util.ListResourceBundle {

 static final Object[][] contents = new String[][] {
    {"ACTIVE", "ACTIVE"},
    {"Joe", "asfag"},
    {"lolcats", "cheezburger"},
    {"HELP", "OH GOD NOT THE BEES"},
    {"asdfffff", "hacks"}
 };

 public Object[][] getContents() {
     return contents;
 }
}

And there's probably a hundred of these files.

So, to summarize: what is the best way to gain access to that data?

(Obviously, I cannot simply compile them with my project.)

9
  • Why "(Obviously, I cannot simply compile them with my project.)"? Commented Jul 14, 2011 at 22:01
  • JavaCompiler "doesn't work", you say. What happens? It's part of the JEE SDK; it's supposed to work. You shouldn't blithely ignore that failure and try another method. Detail the error messages here. Commented Jul 14, 2011 at 22:03
  • It's a simple scanning/parsing problem. If all the files are like the one above, read until you see the "contents =", then read and parse the lines until you get to the "}". You might be able to coerce a JSON scanner into reading the values, but it's probably not worth the effort vs just reading them with custom code. Commented Jul 14, 2011 at 22:04
  • I agree with g051051. My first thought after reading the question was "Why is it obvious that you cannot compile them with your project?" I would think the obvious answer is to do just that: compile them automatically as needed from within your program and access their data from the compiled class file. Commented Jul 14, 2011 at 22:06
  • Haha, look at the comments to Woot4Moo's answer: looks like he doesn't even need it to happen at runtime. So here's the real answer: javac *.java and in all of your files do import com.mypack.costmgr.* so you can just access them all as normal (Cost_en.contents or Cost_en.getContents()) Commented Jul 14, 2011 at 22:10

2 Answers 2

1

You have to compile the .java files and make them .class files. Then you put those .class files on your classpath. At this point you can now make a reference to the contents of each of those files. Since contents is static you can get a reference to it by doing the following:

class MyAwesomeClass  
 {  
    Object[][] myArray = Cost_en.contents;  
 }  

Resource bundles

Sign up to request clarification or add additional context in comments.

6 Comments

Can I do that all during runtime?
I would say yes, on account of during the programs execution it will gain those references where applicable
NetBeans refuses to compile the code. I'm using JavaCompiler.
I added the oracle tutorial on resource bundles which is what you are dealing with here.
Ok - I just took a look at that. So, (and correct me if I'm wrong) I use javac to compile the .java files into .class files - how do I then get the class files to be 'included' or how do I reference them from my project?
|
0

Spring Roo has an interesting Java language parser and manipulation framework for their plugins. It's used to extract info from user created .java files as part of the code supporting AspectJ. Maybe you can create a Roo plugin to handle what you're trying to do?

3 Comments

AspectJ is an Aspect Oriented programming system for Java. Way too involved to go into here...see eclipse.org/aspectj.
I know what aspectJ is, that was supposed to come across is the OP looking for an Aspect oriented solution
Oh, I see. Roo uses AspectJ and the framework is for supporting that, but it does seem support some very interesting source file reading and rewriting capabilities.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.