Win a copy of Micro Frontends in Action this week in the Server-Side JavaScript and NodeJS forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Bear Bibeault
  • Junilu Lacar
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • salvin francis
  • Frits Walraven
Bartenders:
  • Scott Selikoff
  • Piet Souris
  • Carey Brown

Using java to run java files Problem

 
Ranch Hand
Posts: 634
Eclipse IDE Chrome 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.

 
Ranch Hand
Posts: 68
MyEclipse IDE Chrome Java
  • 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
Eclipse IDE Chrome 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???
 
Bartender
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.
 
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

reply
    Bookmark Topic Watch Topic
  • New Topic