• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

"java -cp . vs java"??

 
Rob Hermann
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I can only run the class file if I use java -cp . MyFirstApp

1)Why does this work? and not "java MyFirstApp"

2)Why can't I get it to run in the browser window???

PATH is correct
Everything is installed correctly
Other applets have worked

Command prompt error:
Exception in thread "main" java.lang.NoClassDefFounError: MyFirstApp

Error in java consol is:
java.lang.ClassCastException: MyFirstApp
at sun.applet.AppletPanel.createApplet(Unknown Source)
at sun.plugin.AppletViewer.createApplet(Unknown Source)
at sun.applet.AppletPanel.runLoader(Unknown Source)
at sun.applet.AppletPanel.run(Unknown Source)
at java.lang.Thread.run(Unknown Source)
Exception in thread "Thread-4" java.lang.NullPointerException
at sun.plugin.util.GrayBoxPainter.showLoadingError(Unknown Source)
at sun.plugin.AppletViewer.showAppletException(Unknown Source)
at sun.applet.AppletPanel.runLoader(Unknown Source)
at sun.applet.AppletPanel.run(Unknown Source)
at java.lang.Thread.run(Unknown Source)
java.lang.NullPointerException
at sun.plugin.util.GrayBoxPainter.showLoadingError(Unknown Source)
at sun.plugin.AppletViewer.showAppletStatus(Unknown Source)
at sun.applet.AppletPanel.run(Unknown Source)
at java.lang.Thread.run(Unknown Source)
 
vignesh hariharan
Ranch Hand
Posts: 77
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
first of all be clear if you r going to run an applet or an application..

I can only run the class file if I use java -cp . MyFirstApp

1)Why does this work? and not "java MyFirstApp"

2)Why can't I get it to run in the browser window???


java classname is used to run application based programs
appletviewer classname.java is used to run applets

u seem to run an applet and trying to use java classname.. how will this work???
[ April 01, 2006: Message edited by: vignesh hariharan ]
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24208
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"java MyFirstApp" is not working because you have a value set for the environment variable "CLASSAPATH", and that value does not include "." (dot), which stands for the current directory.

I always tell people new to Java to not set this variable to anything. If some installer sets a value for it, to remove that value. Later, when you learn more, you may choose to set it.

But in any case, go to your control panel, remove the value for CLASSPATH altogether, and the "-cp ." will no longer be needed.
.
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24208
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And one more thing: to run a class in a browser, it has to be an applet. This is a class that extends java.applet.Applet, and has an init() and/or start() method that does something useful. An applet is different from an application, which is a class that has a main() method, as yours does. Applications are run only on the local machine, without a browser being involved.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic