12

Windows 7 64 ビットで JDK7 と Eclipse Indiago を使用しています。環境変数 JAVA_HOME を F:\JDK7 に設定し、パスに %JAVA_HOME%\bin を追加します。それは私のサンプルコードです:

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

私が得るエラー:

タイプ com.sun.tools をタイプに解決できません

com.sun.tools がないのはなぜですか? どうしたの ?

ここに画像の説明を入力

4

3 に答える 3

28

Eclipseを使用しているようです。デフォルトでは、Eclipse は JRE jar のみをインポートし、JDK からのものはインポートしません。

解決策 1:

  1. Eclipse の設定に移動します (Windows の場合: [ウィンドウ] --> [設定])。
  2. 設定を開きますJava - >インストールされたJRE
  3. JREを選択して編集を押します
  4. 「外部 jar の追加」を使用して tools.jar (JDK_HOME/lib にあります) を含めます。

解決策 2:

プロジェクトのビルド パスを編集し、外部ライブラリを追加します: JDK_HOME/lib にある tools.jar

于 2012-04-25T11:48:51.187 に答える
8

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.

于 2012-04-25T11:47:24.250 に答える
1

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.

于 2015-09-07T10:44:03.370 に答える