File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes Cant run apps[help] Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Cant run apps[help]" Watch "Cant run apps[help]" New topic
Author

Cant run apps[help]

Ross Lovell
Greenhorn

Joined: Mar 22, 2006
Posts: 5
Hi,

I hope this is the right place for this.

Im trying to run simple Java apps like helloworld but I keep getting errors.

I use xp and command prompt to run these apps.

I have changed my PATH variable so I know that isnt the problem.

I get this error(cannot read:HelloWorld.java) when running this command(javac HelloWorldApp.java).

Heres the file im trying to run which I got from the sun systems website:

/**
* The HelloWorldApp class implements an application that
* simply displays "Hello World!" to the standard output.
*/
class HelloWorldApp {
public static void main(String[] args) {
//Display "Hello World!"
System.out.println("Hello World!");
}
}

The file itself is in the bin folder in the jdk folder.

What am I doing wrong?

This is really bugging me as ive been coding in Flash for a year or so and after reading over a couple of books I understand Java pretty well. Just want to start porting my Flash games to Java for practice.

Thanks in advance.
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24187
    
  34

Hi Ross,

Welcome to Java!

The argument to javac is a literal filename or path to a filename. The PATH and CLASSPATH environment variables are irrelevant. If you type

dir

and you see "HelloWorldApp.java", then the command

javac HelloWorldApp.java

will work; otherwise, it won't. Generally, you want to "cd" to the location of your HelloWorldApp.java file, and run your command there.

Note that this location should not be the JDK/bin directory. If you've set the PATH variable properly, then this is unnecessary, and that location is not meant to hold your working files. Make a directory C:\MYJAVA, or something, and put HelloWorldApp.java in there; then

cd C:\MYJAVA
javac HelloWorldApp.java


[Jess in Action][AskingGoodQuestions]
Ross Lovell
Greenhorn

Joined: Mar 22, 2006
Posts: 5
Thanks. Actually works. Thought I was never going to get the thing to run.
Jeff Albertson
Ranch Hand

Joined: Sep 16, 2005
Posts: 1780
Originally posted by Ross Lovell:
Thanks. Actually works. Thought I was never going to get the thing to run.


What was your mistake?


There is no emoticon for what I am feeling!
Ross Lovell
Greenhorn

Joined: Mar 22, 2006
Posts: 5
Acally e got another problem.

Now I cant run the class file.

Im using this site to help:http://java.sun.com/docs/books/tutorial/getStarted/cupojava/win32.html#win32-2

Im doing everything right but its not working. I get the error they talk about at the bottom of the page: Exception in thread "main" java.lang.NoClassDefFoundError: HelloWorldApp

Ive tried everything they say to fix it but im just getting the error.

Any ideas?
Shaun Williams
Greenhorn

Joined: Mar 22, 2006
Posts: 16
Ross,

When you run the java app launcher (java.exe), do you put .class on the end of the application name?
For instance;

java HelloWorldApp.class

This is wrong, and will result in an exception.
instead, try dropping the .class extention from the filename:

java HelloWorldApp

See if that gives you any joy,
Shaun
Jeff Albertson
Ranch Hand

Joined: Sep 16, 2005
Posts: 1780
The java command doesn't automatically look for the class file in the current directory. You have to enter

java -classpath . HelloWorldApp

or equalivalently

java -cp . HelloWorldApp

The dot stands for the current directory. ]

In the long run I would suggest using Ant or an IDE to help you compile and run programs, but both come with a learning curve.
Jeff Albertson
Ranch Hand

Joined: Sep 16, 2005
Posts: 1780
Originally posted by Shaun Williams:
When you run the java app launcher (java.exe), do you put .class on the end of the application name?
For instance;

java HelloWorldApp.class


If he had done that his error message would have been "Exception in thread "main" java.lang.NoClassDefFoundError: HelloWorldApp/class"
Ross Lovell
Greenhorn

Joined: Mar 22, 2006
Posts: 5
Thanks Shaun it works. Im confused though because I know I tried that. Maybe its because I shut down command prompt and reopened it or something after all that fiddling about.

Anyway now I can get on with actually building proper apps.

Thanks all.
Jeff Albertson
Ranch Hand

Joined: Sep 16, 2005
Posts: 1780
Originally posted by Ross Lovell:
Thanks Shaun it works.


Now I'm (plus I'm contradicting my signature!)
[ March 22, 2006: Message edited by: Jeff Albertson ]
Shaun Williams
Greenhorn

Joined: Mar 22, 2006
Posts: 16
I understand that JDK 1.5 causes the exception "/class", but Ross maybe using an earlier version; I'm not sure if the exception described is thrown in 1.4, for instance.

Either that, or Ross just thanked the wrong person...
[ March 22, 2006: Message edited by: Shaun Williams ]
Ross Lovell
Greenhorn

Joined: Mar 22, 2006
Posts: 5
Nah im using 1.5.0_6

I got it from sun a few days ago so I presume its the newest version.

I am completely baffled as to why it didnt work before.
Shaun Williams
Greenhorn

Joined: Mar 22, 2006
Posts: 16
Actully, Jeff maybe right; if you updated your path but didnt close and reopen the command prompt, the changes wouldn't have been effected. This would be the case regardless of whether or not the ".class" extention was used.

Shaun
[ March 22, 2006: Message edited by: Shaun Williams ]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Cant run apps[help]