• Post Reply Bookmark Topic Watch Topic
  • New Topic

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

 
Adam S.
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • 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. ]
 
Joe Ess
Bartender
Posts: 9443
12
Linux Mac OS X Windows
  • Mark post as helpful
  • send pies
  • 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
  • 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.
 
himanshu patel
Ranch Hand
Posts: 205
  • Mark post as helpful
  • send pies
  • 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
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!