aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Using java to run java files Problem Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Using java to run java files Problem" Watch "Using java to run java files Problem" New topic
Author

Using java to run java files Problem

Mohit G Gupta
Ranch Hand

Joined: May 18, 2010
Posts: 634

i have a file MyClass.java in pack/sample1

it contains:


i am in sample1 directory

javac MyClass.java

complies fine


but when i run the file

java MyClass

i get the following error:



C:\Users\mohit\DOCUME~1\Javafiles\pack\sample1>java MyClass
Exception in thread "main" java.lang.NoClassDefFoundError: MyClass (wrong name:
pack/sample1/MyClass)
at java.lang.ClassLoader.defineClass1(Native Method)
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)
at java.lang.ClassLoader.loadClassInternal(Unknown Source)
Could not find the main class: MyClass. Program will exit.


if i run using java pack.sample1.MyClass
then also the error appears

C:\Users\mohit\DOCUME~1\JAVAFI~1\pack\sample1>java pack.sample1.MyClass
Exception in thread "main" java.lang.NoClassDefFoundError: pack/sample1/MyClass
Caused by: java.lang.ClassNotFoundException: pack.sample1.MyClass
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)
at java.lang.ClassLoader.loadClassInternal(Unknown Source)
Could not find the main class: pack.sample1.MyClass. Program will exit.


OCPJP 6.0 93%
OCPJWCD 5.0 98%
Sumit Khurana
Ranch Hand

Joined: Sep 19, 2010
Posts: 68

when you are running
java pack.sample1.MyClass1

than you must be in the directory above the pack
Mohit G Gupta
Ranch Hand

Joined: May 18, 2010
Posts: 634

what about..



C:\Users\mohit\DOCUME~1\Javafiles\pack\sample1>java MyClass

it too give error



As sumit saidd
when you are running
java pack.sample1.MyClass1

than you must be in the directory above the pack


why you must be in the directory above the pack???
Wouter Oet
Saloon Keeper

Joined: Oct 25, 2008
Posts: 2700

Because that's how it is defined. Is a class is in a package it must be called from the parent directory of the package directory.


"Any fool can write code that a computer can understand. Good programmers write code that humans can understand." --- Martin Fowler
Please correct my English.
Trivikram Kamat
Ranch Hand

Joined: Sep 26, 2010
Posts: 155
mohitkumar gupta wrote:
if i run using java pack.sample1.MyClass
then also the error appears

C:\Users\mohit\DOCUME~1\JAVAFI~1\pack\sample1>java pack.sample1.MyClass
Exception in thread "main" java.lang.NoClassDefFoundError: pack/sample1/MyClass
Caused by: java.lang.ClassNotFoundException: pack.sample1.MyClass
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)
at java.lang.ClassLoader.loadClassInternal(Unknown Source)
Could not find the main class: pack.sample1.MyClass. Program will exit.



Sumit Khurana wrote:
when you are running
java pack.sample1.MyClass1

than you must be in the directory above the pack


Consider you have file Test.java with following package:


Then in order to run the code after compiling, you need to cd to folder containing pack1 and run following command


OCPJP6
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Using java to run java files Problem