0

I am able to use:

C:\Program Files (x86)\Java>javac -classpath mail.jar myFile.java

to compile,but when I try to run it:

C:\Program Files (x86)\Java>java -cp mail.jar myFile
Exception in thread "main" java.lang.NoClassDefFoundError: myFile
Caused by: java.lang.ClassNotFoundException: myFile
    at java.net.URLClassLoader$1.run(URLClassLoader.java:202)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(URLClassLoader.java:190)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:306)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:247)
Could not find the main class: myFile.  Program will exit.

I tried using various combinations using "." and "-classpath" to denote class path as other posts have sugested but couldn't figure it out. I've compiled and successfully run a basic helloworld.java in the same folder. running as admin. using java runtime 1.6.

2 Answers 2

3

Put your current directory into the classpath as well:

java -cp .;mail.jar myFile

If that doesn't work, post myFile.java so we can see what might be going on. For example, is myFile in the default package or is there a package statement?

0
1

You need to do one of the following:

  • pack the jar first (use the jar command) and use java -jar

  • not specify a classpath and just run your class file (standalone applications)

  • (if the jarfile is a library) you need to include the current directory in your classpath

3
  • He has to specify a classpath because (apparently) his app depends on mail.jar. Commented May 15, 2012 at 23:32
  • Ah. Well, to be honest, I'm used to sticking my own files in the other jarfile (minecraft) :P
    – Riking
    Commented May 15, 2012 at 23:33
  • eventually i'm going to use "exej" to make it a .exe. its for an expired account notifier Commented May 15, 2012 at 23:50

Not the answer you're looking for? Browse other questions tagged or ask your own question.