java程序编译Java文件,从java程序中编译.java文件?

I found this code:

JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();

if(compiler.run(null, null, null, fileName) != 0) {

System.err.println("Could not compile.");

System.exit(0);

}

However, this returns a NullPointerException

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException

at net.foxycorndog.foxy.compiler.Compiler.compile(Compiler.java:25)

at net.foxycorndog.foxy.compiler.Parser.parse(Parser.java:41)

at net.foxycorndog.foxy.Foxy$ActionHandler.actionPerformed(Foxy.java:99)

I read that the JRE does not include the ability to compile within a java program, but the JDK does.

I don't want a program that only works on a few computers that have had to manually set the path to the JDK library instead of JRE. With this in mind, is there any work around for this problem?

I would also like it to work over cross platform if possible.

解决方案

There's no workaround to the fact that the JRE doesn't contain a compiler, and the JDK does. If you want to use the Java compiler, then you'll need to run your program with the JDK.

There are other ways to create executable Java code at runtime, however; there are various bytecode assemblers that let you build your code dynamically without use of a compiler. ASM is one that rocks hard; the Apache BCEL is another.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值