在 Ubuntu 中正确设置 java 类路径和 java_home

发布于 2024-08-16 17:02:09 字数 620 浏览 10 评论 0原文

我收到错误

线程“main”中出现异常java.lang.NoClassDefFoundError:

当我尝试在 Ubuntu 上运行已编译的类时。我使用一个非常简单的 Helloworld 示例,互联网上已有的数百万条回复表明我的 CLASSPATH 和 JAVA_HOME 变量设置不正确。

但是,我已将 etc/environment 编辑到正确的文件夹以及当前文件夹:

PATH=".:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games"

JAVA_HOME="/usr/lib/jvm/java-1.5.0-sun/"

CLASSPATH=".:/usr/lib/jvm/java-1.5.0-sun/lib"

当我键入 set 命令时它们会出现。无论如何,即使我使用手动设置类路径

sudo java -cp 。 myfirstjavaprog.class

我得到同样的错误。我还应该看哪里?这一定是配置问题。

非常感谢

I am getting the error

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

When I try and run a compiled class on Ubuntu. I am using a very simple Helloworld example, and the millions of responses which already exist on the internet suggest that my CLASSPATH and JAVA_HOME variables have been incorrectly set.

However, I have edited the etc/environment to the correct folders as well as the current folder:

PATH=".:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games"

JAVA_HOME="/usr/lib/jvm/java-1.5.0-sun/"

CLASSPATH=".:/usr/lib/jvm/java-1.5.0-sun/lib"

and they appear when I type the set command. In any case, even when I set the classpath manually using

sudo java -cp . myfirstjavaprog.class

I get the same error. Where else should I look? This must be a configuration problem.

Many thanks

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

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

发布评论

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

评论(8

ぺ禁宫浮华殁 2024-08-23 17:02:09

您想从末尾删除 .class。只需输入...

java -cp . myfirstjavaprog

You want to drop the .class from the end. Just type...

java -cp . myfirstjavaprog
禾厶谷欠 2024-08-23 17:02:09

我强烈建议删除 CLASSPATH 环境变量,或者至少将 JRE/JDK 从中删除。

除非另有说明,"." 隐式地存在于类路径中。从 Java 1.3 开始,Java 已经足够聪明,可以根据 javac/java 可执行文件的执行路径找到自己的运行时和库。从那时起,在类路径上指定这些内容即使不是完全错误,也是多余的。当然 .../lib 是不正确的,因为那里只有 jars,没有类,并且如果它们没有单独且显式命名,则不会从类路径中获取它们。

现代 java 足够智能,您只需在类路径的根目录中输入 java 即可,它就会 Just Work™。

I strongly recommend getting rid of the CLASSPATH environment variable, or at least taking your JRE/JDK out of it.

"." is implicitly in the classpath unless otherwise specified. And since about Java 1.3, Java has been smart enough to find its own runtime and libraries based on the execution path of the javac/java executables. Since then, it's been redundant, if not outright wrong, to specify those on the classpath. Certainly .../lib is incorrect, as there are only jars there, no classes, and those aren't picked up off the classpath if they're not individually and explicitly named.

Modern javas are smart enough that you can just type java <classname> when standing in the root directory of the classpath, and it will Just Work™.

拥有 2024-08-23 17:02:09

你正在混合苹果和橙子。
命令行上的原始 java 或 javac 调用需要一个类路径来知道它可以在哪里访问其类。当您运行时,

java -cp pathelement1:pathelement2... MyClass

您将向 java 提供查找可运行类的位置列表。它不会查找其他任何地方,包括“.”,除非您告诉它。因此,除非您运行“CLASSPATH”,否则它对您没有帮助。

java -cp $CLASSPATH MyClass

换句话说,它只是不断重新输入类路径的快捷方式。

许多程序都配置为使用 JAVA_HOME,但最终运行 java 程序只需要配置的类路径和 java 路径(它们通过 JAVA_HOME 变量找到它,因此您仍然需要它来完成像 ant 这样的事情,但它在概念上仍然只是一个快捷方式以及)。

您的 PATH 是系统查找二进制文件的路径。如果 java 不在您的路径上(输入“which java”,它将显示您的路径上有哪个 java(如果有的话))运行 /full/path/to/java 与仅运行“java”并让系统相同在 PATH 变量中找到二进制文件。

You are mixing apples and oranges.
A raw java or javac invocation on the command line needs a classpath to know where it can access its classes. When you run

java -cp pathelement1:pathelement2... MyClass

you're giving java the list of places to find runnable classes. It's not going to look anywhere else, including ".", unless you tell it to. So "CLASSPATH" doesn't help you unless you run

java -cp $CLASSPATH MyClass

Inotherwords, its just a shortcut to keep having to retype the classpath.

Many programs are configured to use JAVA_HOME, but ultimately running java programs just need a configured classpath and the path to java (they find it through the JAVA_HOME variable, so you still need it for things like ant, but its' still conceptually just a shortcut as well).

your PATH is the path where the system looks for binaries. If java is not on your path (type "which java", it will show you which, if any, java is on your path) running /full/path/to/java is identical to just running "java" and having the system find the binary in the PATH variable.

救星 2024-08-23 17:02:09

不,我认为是 CLASSPATH 环境变量被忽略了。

正确的方法是在编译和运行时使用 -classpath 选项。为每个项目设置它。你眼前的证据告诉你确实如此。

为什么 CLASSPATH 被忽略?有几个原因:

  1. 它是一个已经失宠的 Java 1.0 工件。
  2. JVM 不保证您已将其设置为环境变量。
  3. IDE 有自己的要求,因此它们不依赖它。
  4. Java EE 应用服务器有自己的要求,因此它们不依赖它。
  5. 您每次都必须给出完整的路径,因为每个项目都可能不同。一旦您完成了“Hello, World”,您就会发现自己正在编写脚本或使用 Ant 和 Maven 等工具来帮助您为项目设置 CLASSPATH。

No, I think it's that CLASSPATH environment variables are ignored.

The right way to do it is to use the -classpath option when you compile and run. Set it for each and every project. The evidence you have before your eyes tells you it's so.

Why is CLASSPATH ignored? Several reasons:

  1. It's a Java 1.0 artifact that's fallen out of favor.
  2. The JVM has no guarantee that you've set one as an environment variable.
  3. IDEs have their own requirements, so they don't rely on it.
  4. Java EE app servers have their own requirements, so they don't rely on it.
  5. You have to give the whole path every time because every project is likely to be different. Once you progress past "Hello, World" you'll find yourself scripting it or using tools like Ant and Maven that will help you set the CLASSPATH for your project.
江挽川 2024-08-23 17:02:09

使用

sudo update-java-alternatives -s java-6-openjdk

它设置了很多类路径的东西。

Use

sudo update-java-alternatives -s java-6-openjdk

It sets a lot of classpath stuff.

玉环 2024-08-23 17:02:09

对于设置 java_home 变量,这里有说明。

http://luckydev07.blogspot.com/2009/ 08/setting-javahome-in-ubuntu-linux.html

classpath可以类似设置

for setting java_home variable, here are instructions.

http://luckydev07.blogspot.com/2009/08/setting-javahome-in-ubuntu-linux.html

and

classpath can be set similarly

[旋木] 2024-08-23 17:02:09

我强烈建议您花一些时间查看 Sun 教程。稍后它将对您有所帮助 - 类路径是臭名昭著的麻烦制造者。

http://java.sun.com/docs/books/tutorial/ getStarted/TOC.html

I would strongly recommend you spend some time looking on the Sun tutorial. It will help you later - class paths are notorious trouble makers.

http://java.sun.com/docs/books/tutorial/getStarted/TOC.html

乖乖哒 2024-08-23 17:02:09

好吧,我在错误的地方寻找问题。事实证明,Java 没问题,而我却因为两个不同的问题而遇到了相同的错误。

我最初尝试运行一个从 Java 网站获得的 swing 示例,但我没有注意到它有一个包定义。我已经设置了正确的文件夹结构,现在它运行良好。

当我尝试运行 HelloWorld 示例时,我不小心包含了 .class 扩展名。

这两个问题都给我带来了 ClassNotFound 错误。

非常感谢您的帮助。

Ok I was looking for the problem in the wrong place. It turned out that Java was fine and I was the victim of getting the same error for two separate problems.

I was originally trying to run a swing example which I got from the Java website, but I hadn't noticed that it had a package definition. I've set up the correct folder structure and now it runs fine.

When I tried to run a HelloWorld example, I had accidentally included the .class extension.

Both of these problems gave me ClassNotFound errors.

Thank you very much for all your help.

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