简体   繁体   English

在运行时编译Java文件可在Eclipse上运行,但不能从导出的可运行jar文件中运行

[英]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.

相关问题 从Eclipse导出的可运行JAR文件似乎无法执行 - Exported runnable JAR file from Eclipse doesn't seem to execute 为什么从Eclipse导出的可运行JAR不起作用? - Why doesn't a runnable JAR exported from Eclipse work? 带导出的可运行jar文件的Java Eclipse错误 - Java eclipse error with exported runnable jar file Java打开文件-在Eclipse上有效,在可运行的JAR / exe中不起作用 - Java opening files - works from Eclipse, doesn't work in runnable JAR/exe setIconImage仅在eclipse中起作用,当导出为可运行的jar文件时不起作用 - setIconImage only works in eclipse, doesnt work when exported as runnable jar file Java项目在Eclipse上工作但在导出到runnable jar文件时不起作用 - Java project working on Eclipse but not working when exported to runnable jar file Java从Eclipse导出为可运行的JAR文件 - Java export as runnable JAR file from Eclipse Java应用程序在Eclipse上工作正常,但在部署为可运行的jar后它无法正常工作 - a Java application works fine on Eclipse but it doesn't work properly after being deployed as a runnable jar 用eclipse导出的可运行jar不起作用,在eclipse中它仍然有效 - Runnable jar exported with eclipse isn't working, in eclipse it still works 在Eclipse中播放视频效果很好,但是当将Java项目导出为可运行的JAR文件时,它不再起作用 - Playing videos works fine in Eclipse but when exporting the java project as an runnable JAR file it doesnt work anymore
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM