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.)

Woot4Moo
  • 23,987
  • 16
  • 94
  • 151
  • Why "(Obviously, I cannot simply compile them with my project.)"? – g051051 Jul 14 '11 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. – Ladlestein Jul 14 '11 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. – Hot Licks Jul 14 '11 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. – Loduwijk Jul 14 '11 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()`) – Loduwijk Jul 14 '11 at 22:10
  • "looks like he doesn't even need it to happen at runtime" - I actually do. – guywhoneedsahand Jul 14 '11 at 22:14
  • Then why did you accept Woot4Moo's answer, which does not provide an answer for doing it at runtime? If you understand how, you could take his answer and convert it into one that works at runtime using Runtime.getRuntime().exec(), but that was not mentioned anywhere. – Loduwijk Jul 14 '11 at 22:21
  • Further, if that is what you are doing (executing that at runtime with exec()), then you should make some comment somewhere about that. One of the main purposes of Stackoverflow is for people to be able to look at this in the future, people who have the same problem as you did, and be able to find a full, useful answer. If you did it somehow at runtime, then Woot4Moo's answer is not complete and you should just leave a comment or something stating how you solved the issue by expanding the answer. – Loduwijk Jul 14 '11 at 22:23
  • I'm pretty sure that's what he was suggesting. – guywhoneedsahand Jul 14 '11 at 22:27

2 Answers2

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

Woot4Moo
  • 23,987
  • 16
  • 94
  • 151
  • Can I do that all during runtime? – guywhoneedsahand Jul 14 '11 at 21:53
  • I would say yes, on account of during the programs execution it will gain those references where applicable – Woot4Moo Jul 14 '11 at 21:55
  • NetBeans refuses to compile the code. I'm using JavaCompiler. – guywhoneedsahand Jul 14 '11 at 21:57
  • I added the oracle tutorial on resource bundles which is what you are dealing with here. – Woot4Moo Jul 14 '11 at 22:00
  • 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? – guywhoneedsahand Jul 14 '11 at 22:01
  • yes javac is the compiler for the Java programming language. I would recommend not using an IDE until you understand the underlying components of developing in Java or any language for that matter. Best of luck :) – Woot4Moo Jul 14 '11 at 22:03
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?

g051051
  • 1,021
  • 5
  • 11
  • AspectJ is an Aspect Oriented programming system for Java. Way too involved to go into here...see http://www.eclipse.org/aspectj/. – g051051 Jul 14 '11 at 21:59
  • I know what aspectJ is, that was supposed to come across is the OP looking for an Aspect oriented solution – Woot4Moo Jul 14 '11 at 22:01
  • 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. – g051051 Jul 14 '11 at 22:03