Java: class not found on Unix

I am trying to run a Java file on a Unix machine. Let me first show the directory structure:

/home/username |_ SimpleMail.java |_ mail.jar 

I compiled java code using:

 # javac -classpath mail.jar SimpleMail.java 

After compilation, the directory has:

 /home/username |_ SimpleMail.java |_ mail.jar |_ SimpleMail.class 

Now I tried to run this SimpleMail class file. This gives a class error not found: I tried to start the class using:

 # java -classpath mail.jar SimpleMail 

Error:

 The java class is not found: SimpleMail 

What is wrong with this process?

Thanks:)

+4
source share
2 answers

You also need to include the current directory in the classpath to find the class file there:

 java -classpath mail.jar:. SimpleMail 

This will work if SimpleMail not included in the package. Otherwise, you must compile with -d . and include the package name when running java :

 javac -d . -classpath mail.jar SimpleMail.java java -classpath mail.jar:. my.pkg.SimpleMail 
+5
source
 # java -classpath mail.jar;. SimpleMail 

will make

0
source

All Articles