Aerrow
Aerrow

Reputation: 12134

Could not Found the main Class in Java while i'm running

I have a java swing code In that There are 5 classes, the Main Class refer other four classes, here i added some jar also, i give the following cmd for compiling,

C:\Users\FSSD\Desktop\FinalAttempt\install\lib>javac WriteHelper.java JavaDemo.j
ava DataBaseHelper.java FileEncryption.java SendEmail.java -cp dnsns.jar;dsn.jar
;imap.jar;javaws.jar;jce.jar;jsse.jar;jxl-2.6.jar;localedata.jar;mail.jar;mailap
i.jar;pop3.jar;rt.jar;smtp.jar;sqlitejdbc-v056.jar;sunjce_provider.jar;sunmscapi
.jar;sunpkcs11.jar;tools.jar JavaSamp.java

Here JavaSamp holding mainclass the other classes are WriteHelper,JavaDemo,DataBaseHelper,fileEncription and SendEmail classes, when i Complied using above cmd it Compiled Successfully, When i run this class i having the following Exception

C:\Users\FSSD\Desktop\FinalAttempt\install\lib>java WriteHelper.java JavaDemo.ja
va DataBaseHelper.java FileEncryption.java SendEmail.java -cp dnsns.jar;dsn.jar;
imap.jar;javaws.jar;jce.jar;jsse.jar;jxl-2.6.jar;localedata.jar;mail.jar;mailapi
.jar;pop3.jar;rt.jar;smtp.jar;sqlitejdbc-v056.jar;sunjce_provider.jar;sunmscapi.
jar;sunpkcs11.jar;tools.jar JavaSamp
Exception in thread "main" java.lang.NoClassDefFoundError: WriteHelper/java
Caused by: java.lang.ClassNotFoundException: WriteHelper.java
        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:307)
        at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:248)
Could not find the main class: WriteHelper.java.  Program will exit.

How do i overcome it, thanks in advance...

Upvotes: 0

Views: 2221

Answers (4)

Jon Skeet
Jon Skeet

Reputation: 1499860

You don't run a Java class by telling it about source code. You tell it which class to run. For example:

java -cp .;dnsns.jar;...[... as before ...] com.foo.JavaSamp

That's assuming a class called JavaSamp in a package of com.foo. Note that when you compile, you should probably use something like -d . to tell it to put the class files into a folder structure rooted in the current directory, based on the package name within the source file.

Upvotes: 3

Stephen C
Stephen C

Reputation: 718708

When you run java WriteHelper.java you are telling Java to look for a class called "java" in the "WriteHelper" package. It isn't there, and that is what the exception is telling you when it says:

Exception in thread "main" java.lang.NoClassDefFoundError: WriteHelper/java

Run the class as java WriteHelper. The java command expects a class name ... not a class file name.


There are other problems with the way that you are compiling and running code.

  • The -cp option and its value must appear before the names of the Java source files (for javac) and the name of the Java class (for java).

  • The java command expects ONE class name, not lots of class names. You need to figure out which class is the one with the public static void main(String[] args) method and use that one (only) as the java class argument. (I would guess, that if you have a class called Main that that has the main entry point method.)

  • This will only work if the classes are all declared in the default class. If the source code starts with a package declaration, you need to organize the classes in a directory tree whose components mirror the class packages; see @Jon Skeet's answer.

Finally, you would be well advised to read the manual pages for java and javac carefully, along with the linked page that talks about how the classpath works. Once you understand them, this stuff won't seem like black magic anymore.

Upvotes: 0

stjohnroe
stjohnroe

Reputation: 3206

You need to include the classes generated in the first step to the class path in the second step.

Upvotes: 0

Buhake Sindi
Buhake Sindi

Reputation: 89169

To run a java file, you specify which class file (ends with a .class extension but you don't specify the extension) and the rest of the classes and libraries (with a .jar extension) are provided in the classpath.

e.g.

java WriteHelper -classpath "<directory_where_class_file_exists>;mail.jar/localedata.jar;

To compile the file, use javac instead. What you did is you told java to look for java in WriteHelper directory (or package).

Upvotes: 0

Related Questions