No com.sun.tools.javac in JDK7

不打扰是莪最后的温柔 提交于 2019-11-29 18:30:08

问题


I'm using JDK7 and Eclipse Indiago in Windows 7 64-bit. I set environment variable JAVA_HOME to F:\JDK7 and add %JAVA_HOME%\bin in path. It's my sample code:

com.sun.tools.javac.Main m1 = new com.sun.tools.javac.Main();
m1.compile(source);

Error I get:

Type com.sun.tools cannot be resolved to a type

Why there is no com.sun.tools ? What's the problem ?


回答1:


It looks like you are using Eclipse. By default Eclipse only imports JRE jars, not the ones from the JDK.

Solution 1:

  1. Go to Eclipse preferences (on Windows: Window-->Preferences)
  2. Open the preference Java-->Installed JREs
  3. Select your JRE and press edit
  4. Use "Add external jars" to include the tools.jar (found in JDK_HOME/lib)

Solution 2:

Edit your project build path and add an external library: tools.jar found in JDK_HOME/lib




回答2:


You are better off using the JavaCompiler API, rather than attemtping to call javac directly which is in tools.jar The API will add this for you if you use it.




回答3:


javac is in the JDK bin directory, but not the JRE bin.

I had a similar problem and it turned out that by mistake I had set my JAVA_HOME variable to the JRE instead of the JDK, i.e.,

C:\Program Files\Java\jre1.8.0_60 instead of 
C:\Program Files\Java\jdk1.8.0_60

Because I "knew" that I had copied the correct directory name, it took me ages to see those two different characters and fix the problem.



来源:https://stackoverflow.com/questions/10314904/no-com-sun-tools-javac-in-jdk7

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!