• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Using java to run java files Problem

 
Mohit G Gupta
Ranch Hand
Posts: 634
Chrome Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.

 
Sumit Khurana
Ranch Hand
Posts: 68
Chrome Java MyEclipse IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
when you are running
java pack.sample1.MyClass1

than you must be in the directory above the pack
 
Mohit G Gupta
Ranch Hand
Posts: 634
Chrome Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2700
IntelliJ IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Trivikram Kamat
Ranch Hand
Posts: 155
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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

 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic