无法在另一台 PC 上运行 JAR 文件。环境变量可能有问题?

发布于 2024-09-16 22:29:27 字数 323 浏览 3 评论 0原文

我已经构建了一个 JAR 文件,它在安装了 Eclipse 的 PC (XP) 上运行良好。它也可以在另一台同样装有 Eclipse 的 PC 上运行。

我尝试在另一台没有 Eclipse 的 PC(XP)上运行它。虽然它包含了JDK和多个JRE。 JAR 文件只是无法通过单击 或 从命令提示符执行。

我不完全确定,但我最好的猜测是环境变量设置不正确。这是我从命令提示符收到的错误:

Exception in thread "main" java.lang.NoClassDefFoundError: ...

任何帮助将不胜感激。

I've built a JAR file and it executes fine on my PC (XP) which has Eclipse installed. It also works on another PC, which also has Eclipse.

I've tried running it on another PC(XP) that does not have Eclipse. Though it contains the JDK and multiple JRE. The JAR file just does not execute by clicking or from the command prompt.

I am not entirely sure, but my best guess is the Environment Variables are not set properly. Here is the error I receive from the command prompt:

Exception in thread "main" java.lang.NoClassDefFoundError: ...

Any help would be appreciated.

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

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

发布评论

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

评论(3

猫七 2024-09-23 22:29:27

肯定是CLASSPATH的问题。

堆栈跟踪还应该说明它找不到哪个类。一旦你有了这个,然后找到哪个罐子有这个类。然后将该 jar 文件添加到类路径或将其添加到类路径环境变量中。

It must be a CLASSPATH issue.

The stacktrace should also say which class it failed to find. Once you have that, then find which jar has that class. Then add that jar file to your classpath or add it to the classpath env variable.

夕色琉璃 2024-09-23 22:29:27

正如其他人所说,这可能是一个类路径问题。

需要注意的一件事是你的罐子是如何构造的。对话框中有许多用于导出可运行 jar 的选项;

  • 将类提取到 jar
  • 将依赖项压缩到 jar 中 - 在 jar 内创建 jar-in-jar-loader.jar。
  • 将 jar 放在 jar 旁边的子目录中。

根据您为此选择的内容取决于罐子的行为方式。如果提取类,则不在 JDK 中的依赖类应该位于类路径上。我推荐这种做法,因为它更简单。

现在的问题是 - 您是否使用类路径的依赖项而不是 Eclipse 项目的构建依赖项?如果是这样,它不会被打包到 / 压缩到 / 放在 jar 旁边,因为 eclipse 不知道它(但 java 仍然会在您的系统上找到它,因为它位于类路径上)。另外,如果您保存了 ANT 脚本并更新了 Eclipse 中的构建路径,Eclipse 将不会更新该 ANT 脚本 - 该脚本仅生成一次。

This is likely a classpath issue as others have said.

One thing to note is how your jar is constructed. You have a number of options in the dialog for exporting a runnable jar;

  • Extract classes into jar
  • Zip dependencies into the jar - creates jar-in-jar-loader.jar inside the jar.
  • Place jars in a subdirectory next to the jar.

Depending on what you have chosen for this depends on how the jar will behave. If the classes are extracted, dependent classes not in the JDK should be on the classpath. I'd recommend this course of action as it is simpler.

Now, the question is - are you using a dependency on your classpath not in the build dependencies of the eclipse project? If so, it won't be packed with / zipped into / put next to the jar because eclipse doesn't know about it (but java will still find it on your system because it's on the classpath). Also, if you've saved an ANT script and updated the build path in eclipse, eclipse won't update that ANT script - that is generated once only.

花之痕靓丽 2024-09-23 22:29:27

单击 jar 文件调用该文件时不考虑环境变量(相当于运行 javaw -jar your.jar)。

我非常确定它也无法在 Eclipse 之外的第一台 PC 上运行。

Environment variables are not considered when invoking a jar file when clicking on it (equivalent to running javaw -jar your.jar).

I'm pretty sure that it doesn't work on your first PC outside of Eclipse either.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文