Java code compiles but does not execute : "Could not find the main class"
This concerns an attempt to run a network application. The code compiles correctly, however upon trying to run with "java SendMail" java returns :
C:\Butte>java SendMail
Exception in thread "main" java.lang.NoClassDefFoundError: SendMail (wrong name:
je3/net/SendMail)
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClassCond(Unknown Source)
at java.lang.ClassLoader.defineClass(Unknown Source)
at java.security.SecureClassLoader.defineClass(Unknown Source)
at java.net.URLClassLoader.defineClass(Unknown Source)
at java.net.URLClassLoader.access$000(Unknown Source)
at java.net.URLClassLoader$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
Could not find the main class: SendMail. Program will exit.
Is this a trivial configuration problem. I have a Java package recent enough to support other networking code. 开发者_StackOverflow社区The source code is :
package je3.net;
import java.io.*;
import java.net.*;
/**
* this software sends e-mail via a mailto : url
*/
public class SendMail {
public static void main(String[] args) {
// --- SNIP ----
}
}
Thank you very much,
Joel
Your class is in a package je3.net
, so you need to run it as:
java je3.net.SendMail
However, it sounds like you probably need to compile it differently too - as that will look for je3\net\SendMail.class
as a filename. Basically your source and output should match the package structure you declare.
You don't have to do it for the source - you could just write:
javac -d . SendMail.java
java je3.net.SendMail
as the -d .
part will make javac
create the right directory structure for you. However, I'd personally move SendMail.java
to a je3\net
directory.
精彩评论