• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

java gives ClassNotFoundException when run from file but not when run from cmd line

 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The topic says it all.

My file reads:

/opt/j2re/bin/java -server mypackage.MyClass


When I run from the command line:

./file

I get "ClassNotFoundException mypackage/MyClass"


But when I run from the command line:

/opt/j2re/bin/java -server mypackage.MyClass

Everything works fine.


I've also tried:

/opt/j2re/bin/java -server /pathtothepackage/mypackage.MyClass


Please help
[ June 17, 2004: Message edited by: Adam S. ]
 
Bartender
Posts: 9626
16
Mac OS X Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You need to set the classpath so the virtual machine can find your class.
/opt/j2re/bin/java -cp /somepath/ -server mypackage.MyClass
where somepath is the path to the directory in which the root directory of your package resides. You can also solve your problem by making an executable jar and using java -jar.
 
Adam S.
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Joe Ess:
You need to set the classpath so the virtual machine can find your class.
/opt/j2re/bin/java -cp /somepath/ -server mypackage.MyClass
where somepath is the path to the directory in which the root directory of your package resides. You can also solve your problem by making an executable jar and using java -jar.



I tried setting the classpath with the -cp flag, but still get the exception. I even put the class files in a jar file and tried that. I'd rather not make an executable jar because there are several main classes in the package.
 
Ranch Hand
Posts: 205
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What classpath exactly you have given and what is your path where your
class file resides? With -cp flag it should definately work.

For eg. if the path to your class file is
/home/userdir/javadir/mypackage/MyClass.class than try with following command.

/opt/j2re/bin/java -server -cp /home/userdir/javadir mypackage.MyClass
 
reply
    Bookmark Topic Watch Topic
  • New Topic