1

I've tried to compile, at runtime, a .java file from code and everything works fine on eclipse. But when I've tried to export the project, making a runnable jar file and then execute the jar... well, nothing happens. Why it doesn't work from the runnable jar file?

Here's what I see on the java console after the execution of the jar:

30/10/15 13:47:19,597 iconservicesagent[265]: 
Failed to get the real path for: /var/folders/xd/jg7p911x3bd76jyy071hl6lw0000gn/
C/com.apple.iconservices

And here the java code that I use to compile a source file:

try {
         JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
         DiagnosticCollector<JavaFileObject> diagnostics = new DiagnosticCollector<JavaFileObject>();
         StandardJavaFileManager fileManager = compiler.getStandardFileManager(diagnostics, null, null);
         Iterable<? extends JavaFileObject> compilationUnits = fileManager.getJavaFileObjectsFromStrings(Arrays.asList("generated/Crilin.java"));
         JavaCompiler.CompilationTask task = compiler.getTask(null, fileManager, diagnostics, null,
                 null, compilationUnits);
        task.call();

            fileManager.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
Saverio Java
  • 78
  • 11

1 Answers1

0

Great question, I will give the step by step instructions:

1.) In Eclipse have your desired program highlighted, then click Run

2.) Go down the list to Run Configuration and click that

3.) Make sure you select the right class as your main if there are multiple classes!

4.) Name the program and then apply changes

5.) Then right click on the project name and click Export

6.) Under the Java Section click Runnable JAR File

7.) Then go through the drop down box till you find your program

8.) Then export it to wherever you want it saved

After you do all this the program should run from outside of Eclipse!

Josh A
  • 51
  • 6
  • Josh, you've misunderstood my question! I already exported the Runnable Jar File. The problem is that doesn't generate .class of the .java created at runtime when instead on eclipse it does. – Saverio Java Oct 30 '15 at 16:13
  • Oh I see, my apologies about that. That is quite an interesting issue, can you please include all of your code if possible? – Josh A Oct 30 '15 at 17:02
  • No need to apologize. The code is just the one posted. Feel free to try by yourself. If you're going to try, you have to change only the string "generated/Crilin.java" with the path of the .java file that you want to compile. – Saverio Java Oct 30 '15 at 17:32
  • So I think I get what your question is. From what I can see from your code the issue is that once exported the file paths of the exported program and the `generated/Crillin.java`. In Eclipse it works because they are in the same src folder, but not once exported. I suggest looking over (http://stackoverflow.com/questions/320542/how-to-get-the-path-of-a-running-jar-file) as there are a bunch of ways suggested and you can see which best suits you. If this doesn't work maybe re-post your question as it has been up for a while without success. Hope this helps! – Josh A Oct 31 '15 at 14:13
  • No, I'm sorry. Is not what I'm looking for. – Saverio Java Nov 01 '15 at 17:41