File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Linux / UNIX and the fly likes How to change the CLASSPATH in Linux? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Engineering » Linux / UNIX
Bookmark "How to change the CLASSPATH in Linux?" Watch "How to change the CLASSPATH in Linux?" New topic
Author

How to change the CLASSPATH in Linux?

Varuna Seneviratna
Ranch Hand

Joined: Jan 15, 2007
Posts: 167
java HelloWorld
Exception in thread "main" java.lang.NoClassDefFoundError: HelloWorld
Caused by: java.lang.ClassNotFoundException: HelloWorld
at java.net.URLClassLoader$1.run(URLClassLoader.java:217)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:205)
at java.lang.ClassLoader.loadClass(ClassLoader.java:321)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:294)
at java.lang.ClassLoader.loadClass(ClassLoader.java:266)
Could not find the main class: HelloWorld. Program will exit.

Above exception is generated when tried to run the HelloWorld program But it can be run when the command
unset CLASSPATH
is issued, But when a new terminal is opened again the aforementioned command has to issued to execute any program.How to change or reset CLASSPATH so that when in a new terminal the command
unset CLASSPATH
will not have to be unset


Varuna Seneviratna
Philip Thamaravelil
Ranch Hand

Joined: Feb 09, 2006
Posts: 99
Sounds like you just need to add 'unset CLASSPATH' to your profile.

For example, if you use bash as your shell, you can add it to .bash_profile


your profile is run each time you login with the respective user id.
Varuna Seneviratna
Ranch Hand

Joined: Jan 15, 2007
Posts: 167
In the post I created at http://www.coderanch.com/t/551474/Linux-UNIX/install-tomcat-Ubuntu-when-it CLASSPATH is set as
export CLASSPATH=/usr/local/tomcat/common/lib/jsp-api.jar:/usr/local/tomcat/common/lib/servlet-api.jar
in the file bashrc file, when I comment the CLASSPATH setting code the
java HelloWorld
works How do I be able to set the CLASSPATH to execute Java Programs and also to do Tomcat development
Philip Thamaravelil
Ranch Hand

Joined: Feb 09, 2006
Posts: 99
$TOMCAT_HOME//bin/catalina.sh has a place for setting hard-coded classpath entries.


or set it in your profile bashrc/.bash_profile.
Tim Holloway
Saloon Keeper

Joined: Jun 25, 2001
Posts: 16303
    
  21

The J2EE api Jars have no place in a default classpath. It's far better to supply them to your build processes that are explicitly intended to build only J2EE artefacts. Most of us use either Ant or Maven to do that, since building a J2EE deployable with raw batch command files is a rather nasty process.

Your primary cause of failure, however, isn't what you included. It's what you didn't include.

Java does not include the current directory in the classpath by default. Thus to run the "HelloWorld.class" file in the current directory, you need a command like this:



"." means your current directory. Note that the ".class" extension must not be supplied, unlike when you compile and must include the ".java" extension.

Although when you define a class without a package (default package), you are doing a Bad Thing and a lot of development tools will yell at you. Normally, you'd put the class in a package. For example:


If your work directory is "mydir" and it had 2 subdirectories: "src" and "classes", you'd put the java code in src/edu/ucf/cop1102/timh/HelloWorld.java and compile like so:



And you'd execute like so:


Customer surveys are for companies who didn't pay proper attention to begin with.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How to change the CLASSPATH in Linux?