在 Ubuntu 中正确设置 java classpath 和 java_home
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1975684/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Setting up java classpath and java_home correctly in Ubuntu
提问by Huguenot
I am getting the error
我收到错误
Exception in thread "main" java.lang.NoClassDefFoundError:
线程“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.
当我尝试在 Ubuntu 上运行编译类时。我正在使用一个非常简单的 Helloworld 示例,互联网上已经存在的数百万个响应表明我的 CLASSPATH 和 JAVA_HOME 变量设置不正确。
However, I have edited the etc/environment to the correct folders as well as the current folder:
但是,我已将 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"
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
当我输入 set 命令时它们就会出现。在任何情况下,即使我使用手动设置类路径
sudo java -cp . myfirstjavaprog.class
须藤 java -cp 。我的第一个javaprog.class
I get the same error. Where else should I look? This must be a configuration problem.
我犯了同样的错误。我还应该看哪里?这应该是配置问题。
Many thanks
非常感谢
采纳答案by Pace
You want to drop the .class from the end. Just type...
你想从最后删除 .class 。只需键入...
java -cp . myfirstjavaprog
回答by duffymo
No, I think it's that CLASSPATH environment variables are ignored.
不,我认为是忽略了 CLASSPATH 环境变量。
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.
正确的做法是在编译和运行时使用 -classpath 选项。为每个项目设置它。你眼前的证据告诉你就是这样。
Why is CLASSPATH ignored? Several reasons:
为什么 CLASSPATH 被忽略?几个原因:
- It's a Java 1.0 artifact that's fallen out of favor.
- The JVM has no guarantee that you've set one as an environment variable.
- IDEs have their own requirements, so they don't rely on it.
- Java EE app servers have their own requirements, so they don't rely on it.
- 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.
- 这是一个失宠的 Java 1.0 工件。
- JVM 不保证您已将其设置为环境变量。
- IDE 有自己的要求,因此他们不依赖它。
- Java EE 应用服务器有自己的要求,所以他们不依赖它。
- 你必须每次都给出完整的路径,因为每个项目都可能不同。一旦您通过“Hello, World”,您会发现自己编写脚本或使用 Ant 和 Maven 等工具来帮助您为项目设置 CLASSPATH。
回答by Anand
for setting java_home variable, here are instructions.
设置 java_home 变量,这里有说明。
http://luckydev07.blogspot.com/2009/08/setting-javahome-in-ubuntu-linux.html
http://luckydev07.blogspot.com/2009/08/setting-javahome-in-ubuntu-linux.html
and
和
classpath can be set similarly
可以类似地设置类路径
回答by Thorbj?rn Ravn Andersen
I would strongly recommend you spend some time looking on the Sun tutorial. It will help you later - class paths are notorious trouble makers.
我强烈建议您花一些时间查看 Sun 教程。稍后它会帮助你 - 班级路径是臭名昭著的麻烦制造者。
回答by Carl Smotricz
I strongly recommend getting rid of the CLASSPATH environment variable, or at least taking your JRE/JDK out of it.
我强烈建议去掉 CLASSPATH 环境变量,或者至少去掉 JRE/JDK。
"."
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.
"."
除非另有说明,否则隐含在类路径中。从 Java 1.3 开始,Java 已经足够聪明,可以根据 javac/java 可执行文件的执行路径找到自己的运行时和库。从那时起,在类路径上指定那些是多余的,如果不是完全错误的话。当然.../lib
是不正确的,因为那里只有 jars,没有类,如果它们没有单独和明确地命名,它们就不会从类路径中提取出来。
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™.
现代 java 足够智能,您只需java <classname>
在类路径的根目录中输入即可,它会 Just Work™。
回答by Steve B.
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 或 javac 调用需要一个类路径来知道它可以在哪里访问它的类。当你跑
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 提供查找可运行类的位置列表。它不会查找其他任何地方,包括“.”,除非您告诉它。所以“CLASSPATH”对你没有帮助,除非你跑
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).
许多程序被配置为使用 JAVA_HOME,但最终运行 java 程序只需要一个配置好的类路径和 java 的路径(它们通过 JAVA_HOME 变量找到它,所以你仍然需要它来处理 ant 之类的东西,但它在概念上仍然只是一个快捷方式同样)。
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.
您的 PATH 是系统查找二进制文件的路径。如果 java 不在您的路径上(键入“which java”,它将显示您的路径上是哪个 java(如果有))运行 /full/path/to/java 与仅运行“java”并拥有系统相同在 PATH 变量中找到二进制文件。
回答by Huguenot
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.
好的,我在错误的地方寻找问题。事实证明,Java 很好,而我是两个不同问题出现相同错误的受害者。
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.
我最初试图运行我从 Java 网站获得的 Swing 示例,但我没有注意到它有一个包定义。我已经设置了正确的文件夹结构,现在它运行良好。
When I tried to run a HelloWorld example, I had accidentally included the .class extension.
当我尝试运行 HelloWorld 示例时,我不小心包含了 .class 扩展名。
Both of these problems gave me ClassNotFound errors.
这两个问题都给了我 ClassNotFound 错误。
Thank you very much for all your help.
非常感谢您的帮助。
回答by haasdas
Use
用
sudo update-java-alternatives -s java-6-openjdk
It sets a lot of classpath stuff.
它设置了很多类路径的东西。