The problem:
I can’t get my java app to run after compiling. My guess is there’s something wrong my classpaths.
Just to clear things up I have used an IDE, Eclipse, and I originally used that to compile and run my application. It ran perfectly, but now I need to run it strictly through the terminal/command line. The reason is that I follow a guideline that if it cant be run on the command line then it doesn’t exist. (Helps me really get to know my applications)
Anyway here’s the environment
OS
32bit Linux AWS distro (Based off of CentOS)
Installed software
- yum install subversion*
- yum install java*
The Application structure
- staff/src/com/staffS3/Helpers/Constants.java
- staff/src/net/UploadFile.java
Classpaths
- export CLASSPATH=”/home/ec2-user/staff/lib/aws-java-sdk-1.2.1.jar:.”
- export JAVA_HOME=”/usr/lib/jvm/jre/”
Compiling
javac /home/ec2-user/staff/src/com/staffS3/Helpers/Constants.java /home/ec2-user/staff/src/net/UploadFile.java
Compiles correctly (Atleast without any errors)
To run it
java UploadFile
Error
Exception in thread "main" java.lang.NoClassDefFoundError: UploadFile (wrong name: net/UploadFile)
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClass(ClassLoader.java:634)
at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:142)
at java.net.URLClassLoader.defineClass(URLClassLoader.java:277)
at java.net.URLClassLoader.access$000(URLClassLoader.java:73)
at java.net.URLClassLoader$1.run(URLClassLoader.java:212)
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: UploadFile. Program will exit.
Been at this for a while. Hopefully someone will notice that little(or huge) thing I’m missing :).
Thanks in advance
You musn’t
cdinto thesrc/netdirectory. Just go tosrcand doYou need to stand in a directory such that
javacan find the filenet/UploadFile.class.The “Wrong Name” error message (and it’s cause) is described well here.
Note also that setting the classpath through
-cpis the preferred way. I.e., tryFinally, mixing .java files and .class files is a bad idea. Use the
-dswitch when runningjavacto specify a destination directory for the class files. Call the directorybinorbuild.