[英]Compiling java file at runtime works on eclipse but doesn't work from an exported runnable jar file
I've tried to compile, at runtime, a .java file from code and everything works fine on eclipse. 我试图在运行时从代码中编译一个.java文件,并且在eclipse上一切正常。 But when I've tried to export the project, making a runnable jar file and then execute the jar... well, nothing happens.
但是,当我尝试导出项目时,创建一个可运行的jar文件,然后执行该jar ...好吧,什么也没发生。 Why it doesn't work from the runnable jar file?
为什么从可运行的jar文件中不起作用?
Here's what I see on the java console after the execution of the jar: 这是执行jar之后在Java控制台上看到的内容:
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: 这是我用来编译源文件的Java代码:
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();
}
Great question, I will give the step by step instructions: 很好的问题,我将逐步说明:
1.) In Eclipse have your desired program highlighted, then click Run 1.)在Eclipse中突出显示所需的程序,然后单击“运行”。
2.) Go down the list to Run Configuration and click that 2.)在列表中转到“运行配置”,然后单击
3.) Make sure you select the right class as your main if there are multiple classes! 3.)如果有多个班级,请确保选择正确的班级作为主要班级!
4.) Name the program and then apply changes 4.)命名程序,然后应用更改
5.) Then right click on the project name and click Export 5.)然后右键单击项目名称,然后单击导出。
6.) Under the Java Section click Runnable JAR File 6.)在Java部分下,单击Runnable JAR File
7.) Then go through the drop down box till you find your program 7.)然后通过下拉框,直到找到您的程序
8.) Then export it to wherever you want it saved 8.)然后将其导出到您想要保存的任何位置
After you do all this the program should run from outside of Eclipse! 完成所有这些操作后,程序应从Eclipse外部运行!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.