Search code examples
javaruntime-compilation

Java - accessing strings in non-compiled .java files during runtime


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


Solution

  • 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