0

Today I asked many questions about Jars & JavaCompiler. Because I haven't been able to achieve my goal I'll ask the whole question now:

I want a ProgramOne.jar which compiles a class out of a file and than loads this class.

Somehow I can't achieve that with the approach which works in Eclipse (the ProgramOne not packed into a Jar).

So the approach which works in eclipse is:

File fRun = new File("someFile.java");
JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
StandardJavaFileManager fileManager = compiler.getStandardFileManager(null, null, null);
Iterable<String> options = Arrays.asList( new String[] { "-d", currentDir+"\\bin\\"} );
Iterable<? extends JavaFileObject> compUnits =  fileManager.getJavaFileObjects(fRun);
Boolean compRes = compiler.getTask(null, fileManager, null, options, null, compUnits).call();           

if(compRes == true){
    System.out.println("Compilation has succeeded");
    fileManager.close();
    ClassLoader cl = Thread.currentThread().getContextClassLoader();
    Class<?> compiledClass = cl.loadClass(someFile);
    cRun = compiledClass;
}else{
    System.out.println("Compilation error");
    fileManager.close();
    throw new Exception("Compilation Error");
}

This creates the class file in the folder <...>/ProgramOne/bin and then loads the class

How can I achieve that with the packed ProgramOne.jar?

Arian
  • 3,183
  • 5
  • 30
  • 58
  • use either maven or ant to build the jar (automatically) or run the jar command by hand. – DwB Mar 12 '12 at 17:56

1 Answers1

1

If I understand what you're trying to do, you'll need to programmatically recreate your ProgramOne.jar file.

So:

java -jar ProgramOne.jar foo.java

1) ProgramOne.jar compiles foo.java

2) ProgramOne.jar reassembles itself to include foo.class

3) ProgramOne.jar executes foo.class

See here for one example of jar file creation.

Community
  • 1
  • 1
Dave
  • 4,546
  • 2
  • 38
  • 59
  • Yes cool, that could be a solution (have to dig in deeper). You understood my question right. Is there a way so that I don't need to reassemble my ProgramOne.jar? For example, can I compile the class to some local folder and than load this class into the ProgramOne.jar from there? – Arian Mar 12 '12 at 19:35
  • No, unfortunately you can't. It's the same problem you have in your other question; you can't reference an external *.class file (or *.jar) file from inside a runnable jar file. – Dave Mar 12 '12 at 21:05
  • Can I read a *.java file then into the ProgramOne.jar and then create something like a virtual class? I would need such a class out of a *.java file to run it with a JUnitCore http://junit.sourceforge.net/javadoc/ – Arian Mar 12 '12 at 21:55
  • I don't know what you mean by 'virtual class' and I'm not sure how the JUnit link is relevant. Can you elaborate? – Dave Mar 12 '12 at 22:10
  • Well I thought maybe you can think of another way if I tell you the purpose, which is a Class build form a *.java and run with a JUnitCore. With virtual class I mean something like a class which is not in a file and only entity till the ProgrammOne.jar is closed. The User should not need to restart the jar file to execute a new *.java testcase. – Arian Mar 12 '12 at 22:20
  • I'll have a look at the URIClassloader, maybe this can also provide a solution – Arian Mar 13 '12 at 10:40