Java编译器返回null
我正在按照 Codingbat.com 的思路创建一个程序。
在运行时,它需要编译代码,然后执行它。这一切都已处理完毕。 目前,我被迫使用 JavacTool,这需要将它一起打包。
我有两个基本问题:
1)如何阻止 ToolProvider.getSystemJavaCompiler() 在从可执行 jar 运行时返回 null?
2)如果上述不行,有没有办法添加com.sun.tools.javac.api.JavacTool的jar;没有将其作为引用库,以便它像常规导入一样工作?
感谢您回答这个问题,如果您愿意,我可以上传带有引用库的 Jar 以及不带引用库的 Jar。
需要明确的是,带有引用库的库可以工作,但它比通过 eclipse 运行的 jar 更大、更慢,它使用 JavaCompiler,而不是 JavacTool
谢谢
编辑:
我很确定这是可能的java,就像我以前见过的那样,但忘记了在哪里以及如何见过。
I am creating a program along the lines of Codingbat.com.
During Runtime, it needs to compile code, and then execute it. This has all been handled.
Currently, I am forced to use the JavacTool, which requires it to be packed alongside.
I have 2 basic questions:
1) How can I stop the ToolProvider.getSystemJavaCompiler() from returning null when ran from an executable jar?
2) If the above is not possible, is there a way to add the jar of com.sun.tools.javac.api.JavacTool; without having it as a referenced library, so that it acts like a regular import?
Thanks for answering this, if you would like, I could upload the Jar with the referenced library, and the jar without it.
Just to be clear, the one with the referenced library works, but it is way to large, and slower then the jar that is ran through eclipse, that uses the JavaCompiler, not the JavacTool
Thanks
Edit:
I am pretty sure this is possible with java as I have seen it before, yet forget where and how.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我怀疑这只是您运行的 Java 版本的问题。如果您运行 JRE 附带的版本,它将没有可用的工具。如果你运行的是JDK自带的版本,就会的。
作为一个例子,下面是一个简短但完整的程序:
在我的笔记本电脑上使用 java.exe 的 JRE 版本运行它:
现在使用 JDK:
因此尝试显式指定与 JDK 关联的 Java 二进制文件。
I suspect it's just a problem of which version of Java you run. If you run the version which comes with the JRE, it won't have the tools available. If you run the version which comes with the JDK, it will.
As an example, here's a short but complete program:
Running it with the JRE version of java.exe on my laptop:
And now with the JDK:
So try explicitly specifying the a Java binary associated with the JDK.
因此,您已经知道所有用户都将安装 JDK,并且您知道在 Java 程序中如何查找 JDK 的类路径。您不需要加载 DLL。您需要在tools.jar中加载com.sun.tools.javac.api.JavacTool类。请参阅如何在运行时加载 jar 文件了解如何加载工具.jar
So you already know that all your users will have a JDK installed and you know how to find the classpath of the JDK when you're in your Java program. You don't need to load a DLL. You need to load the com.sun.tools.javac.api.JavacTool class in tools.jar. See How to load a jar file at runtime on how to load tools.jar