aspose file tools*
The moose likes Beginning Java and the fly likes Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark ""java -cp .   vs   java"??" Watch ""java -cp .   vs   java"??" New topic
Author

"java -cp . vs java"??

Rob Hermann
Greenhorn

Joined: Mar 31, 2006
Posts: 11
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

Joined: Jun 23, 2005
Posts: 77
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 ]

Regards,
vignesh
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24187
    
  34

"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.
.


[Jess in Action][AskingGoodQuestions]
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24187
    
  34

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.
 
Consider Paul's rocket mass heater.
 
subject: "java -cp . vs java"??