java compiler = null,JavaCompiler返回null

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.

解决方案

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:

import javax.tools.*;

public class Test

{

public static void main(String[] args)

{

System.out.println(ToolProvider.getSystemJavaCompiler());

}

}

Running it with the JRE version of java.exe on my laptop:

c:\Users\Jon\Test>"\Program Files\java\jre7"\bin\java Test

null

And now with the JDK:

c:\Users\Jon\Test>"\Program Files\Java\jdk1.7.0"\bin\java Test

com.sun.tools.javac.api.JavacTool@441944ae

So try explicitly specifying the a Java binary associated with the JDK.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值