Setting up java classpath and java_home correctly in Ubuntu

限于喜欢 提交于 2019-11-28 12:09:57

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

java -cp . myfirstjavaprog

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™.

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.

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.
haasdas

Use

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

It sets a lot of classpath stuff.

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

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

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!