为什么 ANT 告诉我 JAVA_HOME 是错误的,而事实并非如此?

发布于 2024-08-07 12:00:33 字数 246 浏览 5 评论 0原文

我收到错误:

C:\dev\ws\springapp\build.xml:81: 无法找到 javac 编译器; com.sun.tools.javac.Main 不在类路径中。 也许JAVA_HOME没有指向JDK。 当前设置为“C:\Program Files\Java\jre6”

,但我已明确将 JAVA_HOME 设置为 C:\Program Files\Java\jdk1.6.0_14

ANT 从哪里获取此值?

I get the error:

C:\dev\ws\springapp\build.xml:81: Unable to find a javac compiler;
com.sun.tools.javac.Main is not on the classpath.
Perhaps JAVA_HOME does not point to the JDK.
It is currently set to "C:\Program Files\Java\jre6"

But I have clearly set my JAVA_HOME to be C:\Program Files\Java\jdk1.6.0_14

Where is ANT getting this value?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(24

℉絮湮 2024-08-14 12:00:33
  1. 在 Eclipse 中,单击“运行”→“外部工具”→“外部工具配置”。
  2. 单击JRE选项卡。
  3. 单击已安装的 JRE... 按钮。
  4. 单击添加按钮。
    (如果适用,请选择标准虚拟机。)
  5. 单击目录按钮。
  6. 浏览到已安装 Java 的 JDK 版本(不是 JRE)
    (例如C:\Program Files\Java\jdk1.7.0_04)。
  7. 单击完成确定
  8. 单独的 JRE 中选择 JDK,然后单击关闭
  9. 重新运行您的 Ant 脚本 — 玩得开心!

这在我遇到的特定场景中有效。

  1. In Eclipse click RunExternal ToolsExternal Tools Configurations.
  2. Click the JRE tab.
  3. Click the Installed JREs... button.
  4. Click the Add button.
    (Select Standard VM, where applicable.)
  5. Click the Directory button.
  6. Browse to your JDK version (not JRE) of your installed Java
    (e.g. C:\Program Files\Java\jdk1.7.0_04).
  7. Click Finish and OK.
  8. Select the JDK at Separate JRE and click Close.
  9. Re-run your Ant script — have fun!

This worked in a particular scenario I encountered.

夏日落 2024-08-14 12:00:33

当您说您已“明确设置”JAVA_HOME 为“C:\Program Files\Java\jdk1.6.0_14”时,这就是您从命令行运行“set”时看到的内容吗?我相信如果根本没有设置的话,Ant 会猜测 JAVA_HOME 的值...是否有可能您设置了 JAVAHOME 而不是 JAVA_HOME?

如果不是这样,我建议您编辑 ant.bat 批处理文件(以及它的名称 - 我不记得它是否复杂)以在开始处和在处打印出 JAVA_HOME其他各种有趣的地方。

When you say you have "clearly set" JAVA_HOME to "C:\Program Files\Java\jdk1.6.0_14" - is that what you see when you run "set" from the command line? I believe Ant will guess at a value for JAVA_HOME if one isn't set at all... is it possible that you've set JAVAHOME instead of JAVA_HOME?

If it's nothing like that, I suggest you edit the ant.bat batch file (and whatever it calls - I can't remember whether it's convoluted or not offhand) to print out JAVA_HOME at the start and at various other interesting places.

你曾走过我的故事 2024-08-14 12:00:33

当我尝试使用以下命令运行 Ant 构建时,我遇到了同样的问题:

java -cp ant.jar:ant-launcher.jar org.apache.tools.ant.Main

该命令的输出是:

BUILD FAILED
XXX/build.xml:8: Unable to find a javac compiler;
com.sun.tools.javac.Main is not on the classpath.
Perhaps JAVA_HOME does not point to the JDK.
It is currently set to "/usr/lib/jvm/java-6-openjdk/jre"

看来 java 可执行文件正在选择“/usr/lib/jvm/java-6-openjdk/jre”作为 JAVA_HOME,与安装 JDK 的“/usr/lib/jvm/java-6-openjdk”相反。

我通过在 任务中设置 fork="yes" 解决了这个问题。

看一下:http://ant.apache.org/manual/Tasks/javac。 html

I encountered the same problem when I try to run Ant build using the following command:

java -cp ant.jar:ant-launcher.jar org.apache.tools.ant.Main

The output of the command is:

BUILD FAILED
XXX/build.xml:8: Unable to find a javac compiler;
com.sun.tools.javac.Main is not on the classpath.
Perhaps JAVA_HOME does not point to the JDK.
It is currently set to "/usr/lib/jvm/java-6-openjdk/jre"

It seems the java executable is picking "/usr/lib/jvm/java-6-openjdk/jre" as JAVA_HOME, as opposed to "/usr/lib/jvm/java-6-openjdk" where JDK is installed.

I resolved this issue by setting fork="yes" in my <javac> task.

Take a look at: http://ant.apache.org/manual/Tasks/javac.html

囚我心虐我身 2024-08-14 12:00:33

也有可能您已将 /bin 包含在 JAVA_HOME 设置中,并且 Ant 正在向其中添加 /bin - 从而找不到任何 exe。这发生在我身上:}

It's also possible that you have included /bin in your JAVA_HOME setting, and Ant is adding /bin to it - thereby not finding any exe's. It's happened to me :}

旧人哭 2024-08-14 12:00:33

确保您使用尾随分号: 这不会起作用:

set JAVA_HOME=C:\Program Files (x86)\Java\jdk1.6.0_29;

这会:

set JAVA_HOME=C:\Program Files (x86)\Java\jdk1.6.0_29

Make sure you do not use the trailing semicolon: This will not work:

set JAVA_HOME=C:\Program Files (x86)\Java\jdk1.6.0_29;

This will:

set JAVA_HOME=C:\Program Files (x86)\Java\jdk1.6.0_29
停顿的约定 2024-08-14 12:00:33

我遇到过类似的问题。我会链接到从 Maven 构建运行 Ant 任务,然后我遇到了问题。我已按以下步骤修复它:

  • 确保 JAVA_HOME 设置正确。您可以在 Windows 命令行中检查它,如下所示:
    C:>回显%JAVA_HOME%
    结果如下: C:\Progra~1\Java\jdk1.6.0_30\
  • 将文件 tools.jar 从 %JAVA_HOME%\lib 复制到 Maven 的 lib 目录。

这对我有用。

I have met the similiar issue. I would link to run Ant task fron Maven build and I got the issue. I have fixed it as bellow steps:

  • Make sure JAVA_HOME is set right. You can check it on Windowns in command line as:
    C:>echo %JAVA_HOME%
    Result would look like: C:\Progra~1\Java\jdk1.6.0_30\
  • Copy file tools.jar from %JAVA_HOME%\lib to lib directory of Maven.

And it worked for me.

提笔书几行 2024-08-14 12:00:33

为了解决这个问题,在window->preferences->ant->中添加tools.jar文件运行时。

To solve this problem add tools.jar file in window->preferences->ant-> runtime .

冷…雨湿花 2024-08-14 12:00:33

在 Eclipse 中,将已安装的 JRE 设置设置为 JDK - 在项目中(项目属性 -> Java 构建路径 -> 库),或首选项中的全局默认值(Java -> 安装的 JRE)。 eclispe 设置比系统变量强。

In eclipse set the installed JRE setting to the JDK - in the project (project properties -> Java Build Path-> Libraries), or global default in preferences (Java->Installed JREs). The eclispe setting is stronger than the system variable.

深空失忆 2024-08-14 12:00:33

我希望您了解系统和用户环境变量。用户优先于系统。如果您在系统变量中设置了 JAVA_HOME,并且在用户变量中也有相同的条目,那么您将只能得到后一个。

右键单击我的电脑,转到属性,选择高级选项卡,然后单击环境变量以查看用户和系统环境变量的列表。

I hope you are aware about System and User environmental variables. The user ones are preferred over system. If you have set your JAVA_HOME in system variables and if there is an entry for the same in user variables, then you will get the latter one only.

Right click on My computer, Go to properties, Select Advanced tab and click on Environmental variables to see the list of user and system environment variables.

吾性傲以野 2024-08-14 12:00:33

分号让我失望:我将 JAVA_HOME 设置为“C:\jdk1.6.0_26;”而不是“C:\jdk1.6.0_26”。在按照 Jon Skeet 的建议检查 ant.bat 文件后,我删除了结尾的分号。这是该文件的一部分:

if "%JAVA_HOME%" == "" goto noJavaHome
if not exist "%JAVA_HOME%\bin\java.exe" goto noJavaHome

因此分号没有被从末尾修剪掉,导致无法找到该文件,因此默认为“C:\Java\jre6”或类似的内容。

令人困惑的部分是 HowtoBuild 页面声明使用分号,但是这似乎打破了它。

The semicolon was throwing me off: I had JAVA_HOME set to "C:\jdk1.6.0_26;" instead of "C:\jdk1.6.0_26". I removed the trailing semicolon after following Jon Skeet's suggestion to examine the ant.bat file. This is part of that file:

if "%JAVA_HOME%" == "" goto noJavaHome
if not exist "%JAVA_HOME%\bin\java.exe" goto noJavaHome

So the semi-colon wasn't being trimmed off the end, causing this to fail to find the file, therefore defaulting to "C:\Java\jre6" or something like that.

The confusing part is that the HowtoBuild page states to use the semi-colon, but that seems to break it.

春夜浅 2024-08-14 12:00:33

我遇到了同样的问题,将 javac 标记中的可执行属性设置为 javac.exe 的位置解决了我的问题。
这解决了问题

<javac srcdir="${srcDir}" destdir="${buildDir}" fork="true" executable="C:\Program Files\Java\jdk1.7.0_03\bin\javac"/>

I was facing the same problem, making the executable property in the javac tag to be set to the location of javac.exe resolved the problem for me.
This resolved the problem

<javac srcdir="${srcDir}" destdir="${buildDir}" fork="true" executable="C:\Program Files\Java\jdk1.7.0_03\bin\javac"/>
我家小可爱 2024-08-14 12:00:33

实际上在 superuser.com 上找到了这个答案,但我必须将 tools.jar 从 JDK\lib 目录复制到 JRE\lib 目录。

零意义...我唯一能想到的是 Sun 在最新的 Java 运行时(Java 7 Update 11)中引入了这个错误,或者 Ant 中如何读取当前 JDK 位置的错误(JRE 比 JDK 更新得更多)显然,Sun 也很愚蠢……他们应该在每次更新 JRE 时发布 JDK)。

我的 JAVA_HOME 设置正确。我通过“set JAVA_HOME”进行确认。它指向我的 JDK 目录并且拼写正确。然而,Ant 声称它找不到 javac,但认为 JAVA_HOME 位于我的 JRE 目录中。

在最新的 Sun JRE7 更新(10 和 11)之前,我的系统运行良好。 Ant的版本是1.8.4

Actually found this answer on superuser.com, but I had to copy tools.jar from my JDK\lib directory to the JRE\lib directory.

Makes ZERO sense...only thing I can think of is Sun introduced this bug in the latest Java runtime (Java 7 Update 11) or a bug in Ant in how it reads the current JDK location (the JRE is more updated than the JDK obviously which is also stupid of Sun...they should release the JDK each time they update the JRE).

My JAVA_HOME was set correctly. I confirmed by doing "set JAVA_HOME". It pointed to my JDK directory and was spelled correctly. However, Ant was claiming it couldn't find javac, but thought JAVA_HOME was in my JRE directory.

My system worked fine before the latest Sun JRE7 updates (10 and 11). Ant is version 1.8.4

财迷小姐 2024-08-14 12:00:33

如果您设置了 JAVA_HOME 但其中有拼写错误,您还会看到对 jre6 路径的虚假引用。

If you have JAVA_HOME set but there's a typo in it, you will also see the bogus reference to a jre6 path.

叶落知秋 2024-08-14 12:00:33

当其他一切都正确时,请尝试以下

  1. 转到窗口 ->首选项->蚂蚁->运行时-> ClassPath
  2. 展开全局条目并检查此处可用的 jdk 工具。
  3. 从特定版本的 java 添加所需的 tools.jar,例如“C:\Program Files\Java\jdk1.6.0_45\lib\tools.jar”
  4. 现在构建并检查结果。

注意:为您要使用的java编译器添加tools.jar,并在存在多个tools.jar(java编译器)时通过将其在tools.jar列表中向上移动来优先选择它。

When everything else is correct try the following

  1. Goto Window -> Preferences -> Ant -> Runtime -> ClassPath
  2. Expand Global Entries and check jdk tools available here.
  3. Add the required tools.jar from a specific version of java such as "C:\Program Files\Java\jdk1.6.0_45\lib\tools.jar"
  4. Now build and check the results.

Note: Add tools.jar for the java compiler that you want to use and give preference to it by moving it up in the list of tools.jar when multiple tools.jar (java compilers) are present.

情绪 2024-08-14 12:00:33

只需按照以下步骤设置 JDK 路径:

  1. 转到“运行”->外部工具->外部工具配置

  2. ->选择“JRE”
    然后从下拉列表中选择可用的 JDK 。前任。 jdk1.8.0_25

Just set the JDK path from the below steps:

  1. Go to 'Run' ->External Tools -> External Tools Configuration

  2. -> Select 'JRE'
    Then from the DropDown list select the available JDK . ex. jdk1.8.0_25

遗心遗梦遗幸福 2024-08-14 12:00:33

我也有同样的问题。我的JAVA_HOME指向的JDK包没有任何tools.jar
确保你的JDK instal.lation有tools.jar

(显然消息错误令人困惑)

I had the same problem. My JDK package pointed by JAVA_HOME didn't have any tools.jar
Be sure that your JDK instal.lation has tools.jar

(clearly the message error is confusing)

絕版丫頭 2024-08-14 12:00:33

将环境变量中的 JAVA_HOME 设置为 D:\Program Files\IBM\SDP\jdk
不要给出任何引号或分号。这对我有用。请尝试该解决方案。
实际上,在 ant.bat 中,它会检查适当的 JAVA_HOME,如果 ant.bat 无法找到它,那么它的 JAVA_HOME 指向默认的 JRE。

Set JAVA_HOME in the environment variables as D:\Program Files\IBM\SDP\jdk
Do not give any quotes or semicolon. It's works for me.Please try the solution.
Actually in ant.bat it checks for appropriate JAVA_HOME in case if ant.bat not able to find it then it's JAVA_HOME points the default JRE.

网白 2024-08-14 12:00:33

我也面临着同样的问题。我使用的是 Windows 7,并且安装了两个版本的 java。首先,我安装了最新版本的java 7,然后是版本5。

我的java安装目录的内容:

C:\Program Files\Java>
jdk1.5.0_14
jdk1.7.0_17
jre1.5.0_14
jre7

并且我的JAVA_HOME设置为正确的值,即:

C:\>set ja
JAVA_HOME=C:\Program Files\Java\jdk1.5.0_14

但我仍然遇到同样的问题:

XXXXXXX\build.xml:478: The following error occurred while
executing this line:
XXXXXXX\build.xml:477: Unable to find a javac compiler;
com.sun.tools.javac.Main is not on the classpath.
Perhaps JAVA_HOME does not point to the JDK.
It is currently set to "C:\Program Files\Java\jre7"

在尝试了所有在这个帖子中的建议我意识到了我的错误。我试图在“用户变量”而不是“系统变量”部分中设置环境变量。将其设置为“系统变量”后,它工作正常。不过,我面临另一个问题。

它指向的 java 的默认版本仍然是 7。

C:\>java -version
java version "1.7.0_17"
Java(TM) SE Runtime Environment (build 1.7.0_17-b02)
Java HotSpot(TM) Client VM (build 23.7-b01, mixed mode, sharing)

我不知道如何使其指向版本 5。

I was also facing the same problem. I am using Windows 7 and I had two versions of java installed. First I have installed latest version java 7 and then version 5.

Contents of my java installation directory:

C:\Program Files\Java>
jdk1.5.0_14
jdk1.7.0_17
jre1.5.0_14
jre7

and my JAVA_HOME was set to the correct value, which was:

C:\>set ja
JAVA_HOME=C:\Program Files\Java\jdk1.5.0_14

But still I was getting the same problem:

XXXXXXX\build.xml:478: The following error occurred while
executing this line:
XXXXXXX\build.xml:477: Unable to find a javac compiler;
com.sun.tools.javac.Main is not on the classpath.
Perhaps JAVA_HOME does not point to the JDK.
It is currently set to "C:\Program Files\Java\jre7"

After trying out all the suggestion in this thread I realized my mistake. I was trying to set the environment variable in "User variables" instead of "System Variables" section. After setting it in "System Variables" it worked fine. I am facing another problem though.

The default version of java it points to is still 7.

C:\>java -version
java version "1.7.0_17"
Java(TM) SE Runtime Environment (build 1.7.0_17-b02)
Java HotSpot(TM) Client VM (build 23.7-b01, mixed mode, sharing)

I am not sure how to make it point to version 5.

临风闻羌笛 2024-08-14 12:00:33

我在使用 Jenkins 构建项目时遇到了这个问题。首先,它找不到ant.bat,通过将ant.bat的路径添加到系统环境变量path来修复。然后ant找不到jdk目录。通过右键单击“我的电脑”> 修复了此问题。属性>高级>环境变量并创建一个名为JAVA_HOME的新环境变量,并为其分配值C:\Program Files\Java\jdk1.7.0_21。不要在用户变量中创建此环境变量。仅在系统变量下创建它。
在这两种情况下,我都必须重新启动系统。

I faced this problem when building my project with Jenkins. First, it could not find ant.bat, which was fixed by adding the path to ant.bat to the system environment variable path. Then ant could not find the jdk directory. This was fixed by right-clicking on my computer > properties > advanced > environment variables and creating a new environment variable called JAVA_HOME and assigning it a value of C:\Program Files\Java\jdk1.7.0_21. Don't create this environment variable in User Variables. Create it under System Variables only.
In both cases, I had to restart the system.

红焚 2024-08-14 12:00:33

遇到这个问题很常见。我无法在系统中设置任何特定的 Java 主目录,因为我有 2 个不同版本的 Java(Java 6 和 Java 7)用于不同的环境。
为了解决该问题,我在打开 build.xml 文件时将 JDK 路径包含在运行配置中。
这样,2 个不同的构建文件使用 2 个不同的 Java 版本进行构建。
我认为这个问题可能有更好的解决方案,但至少上述方法避免设置 JAVA_HOME 变量。

It is common to get this issue. I cannot set any specific Java home in my system as I have 2 different version of Java (Java 6 and Java 7) for different environment.
To resolve the issue, I included the JDK path in the run configuration when opening the build.xml file.
This way, 2 different build files use 2 different Java version for build.
I think there might be a better solution to this problem but at least the above approach avoid setting the JAVA_HOME variable.

猛虎独行 2024-08-14 12:00:33

仅供参考,我使用的是 Windows 7,必须重新启动 Windows 才能使新的 JAVA_HOME 设置生效。

FYI, I am using Windows 7 and had to restart Windows in order for the new JAVA_HOME setting to take effect.

蔚蓝源自深海 2024-08-14 12:00:33

有同样的问题;

c:\jb\src\build.xml:191: Unable to find a javac compiler;
com.sun.tools.javac.Main is not on the classpath.
Perhaps JAVA_HOME does not point to the JDK.
It is currently set to "C:\Program Files\Java\jre7"

Total time: 0 seconds

c:\jb\src>echo %JAVA_HOME%
c:\Program Files\Java\jdk1.7.0_13

解决方案:

path = c:\ant\bin\;%PATH%
path = c:\Program Files\Java\jdk1.7.0_17\bin;%PATH%
set "JAVA_HOME=c:\Program Files\Java\jdk1.7.0_13"

我想单独的jdk路径指令就可以做到这一点,但我懒得弄清楚上面的解决方案是防弹的。 :-)

也使用 Windows7

had the same problem;

c:\jb\src\build.xml:191: Unable to find a javac compiler;
com.sun.tools.javac.Main is not on the classpath.
Perhaps JAVA_HOME does not point to the JDK.
It is currently set to "C:\Program Files\Java\jre7"

Total time: 0 seconds

c:\jb\src>echo %JAVA_HOME%
c:\Program Files\Java\jdk1.7.0_13

solution:

path = c:\ant\bin\;%PATH%
path = c:\Program Files\Java\jdk1.7.0_17\bin;%PATH%
set "JAVA_HOME=c:\Program Files\Java\jdk1.7.0_13"

I guess the jdk path instruction alone would do it but I'm to lazy to figure it out above solution is bullet proof. :-)

Also using Windows7

千年*琉璃梦 2024-08-14 12:00:33

快速解决相同问题的方法是

将 C:\Program Files\Java\jdk1.6.0_14\lib\tools.jar 复制到 C:\Program Files\Java\jre6\lib\

出现此异常是因为 JAVA_HOME 被设置为 C :\Program Files\Java\jre6 并且 Ant 无法在其中找到 tools.jar。

Quick work around for the same is

Copy C:\Program Files\Java\jdk1.6.0_14\lib\tools.jar to C:\Program Files\Java\jre6\lib\

This exception is coming because JAVA_HOME is being set as C:\Program Files\Java\jre6 and Ant is not able to find tools.jar in it.

温柔戏命师 2024-08-14 12:00:33

如果需要使用内置 eclipse jdk 在 eclipse 中运行 ant,请在 build.xml 中添加以下行

<property name="build.compiler" value="org.eclipse.jdt.core.JDTCompilerAdapter"/>

If need to run ant in eclipse with inbuilt eclipse jdk add the below line in build.xml

<property name="build.compiler" value="org.eclipse.jdt.core.JDTCompilerAdapter"/>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文