This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes Running Java Programs from the command prompt Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Running Java Programs from the command prompt" Watch "Running Java Programs from the command prompt" New topic
Author

Running Java Programs from the command prompt

Fergal Crawley
Greenhorn

Joined: Nov 28, 2011
Posts: 18

My PC runs on Windows XP. When I try to run a java program (written using NetBeans) from the command prompt, the program opens in NotePad but does not run.

This is what I have been typing at the command prompt:

C:\java\hello\src\hello\Hello.java


The above is the correct path to the Java file on my PC.

Can you please tell me how to run the program from the command prompt or perhaps link me to a tutorial that explains it?

Thanks!


Beginner
Walter Ho
Greenhorn

Joined: Apr 09, 2008
Posts: 22

You can't run the java source code (.java).

You have to compile the source to get a class file (.class). You do this with the javac command.
I assume you have the Java SDK, if not, you're going to need it.
On the command prompt, type in javac and press Enter. This will indicate whether you have the compiler.
If you do, change to the directory where you have the source code.
In you case, "cd C:\java\hello\src\hello\"
then "javac Hello.java"

After compiling it, you should be able to run it with
"java Hello"
Fergal Crawley
Greenhorn

Joined: Nov 28, 2011
Posts: 18

Thanks for your help Walter. I have the JDK installed and have verified that it is configured correctly, using the command prompt.

When I type "cd C:\java\hello\src\hello\" into the command line, it outputs "C:\java\hello\src\hello\" on the command line. When I type "javac Hello.java" on that command line, it just outputs ""C:\java\hello\src\hello\" again, without running the program.

The program is the simple "Hello World!" one below, and at least I don't think it is being run from the command line.



Am I doing something wrong?

Thanks!
Walter Ho
Greenhorn

Joined: Apr 09, 2008
Posts: 22

nothing wrong at all.
You only compiled the code so that the JVM can run it.

To run it.
"java Hello"
Chris Rothburn
Greenhorn

Joined: Dec 14, 2011
Posts: 15
Struggled a long time with the same issue yesterday, also on Windows XP. Although I don't really know what I am talking about, here are some suggestions:

First, go to the file (Hello.java) and right click it.
If this gives you the option of a command prompt, open the command prompt and input

javac Hello.java

This will create a class file in the same folder. To run it, from the same command prompt input

java -classpath . Hello

If you cannot open a command prompt on the file, then open the command prompt on the folder (hello) by right clicking the folder. Then the commands become

javac hello/Hello.java

java -classpath hello Hello

If that doesn't work, read my recent thread on unwanted file extentions, as your text editor may be appending a hidden file extention to your file (Hello.java.doc or suchlike.) But do read it to the end as there are a lot of red herrings thrown out (due to my inexperience) before I finally resolved the problem.
Walter Ho
Greenhorn

Joined: Apr 09, 2008
Posts: 22

Fergal Crawley wrote:Thanks for your help Walter. I have the JDK installed and have verified that it is configured correctly, using the command prompt.

When I type "cd C:\java\hello\src\hello\" into the command line, it outputs "C:\java\hello\src\hello\" on the command line. When I type "javac Hello.java" on that command line, it just outputs ""C:\java\hello\src\hello\" again, without running the program.

The program is the simple "Hello World!" one below, and at least I don't think it is being run from the command line.



Am I doing something wrong?

Thanks!


Aha. You're using a package.
Type in "cd C:\java\hello\src\" into the command line, it should output "C:\java\hello\src\" on the command line.
You then compile the code with "javac hello\Hello.java" on that command line, it just outputs ""C:\java\hello\src\" again, providing there are no compiler errors.
Now to run your code, you have use "java hello.Hello" to run the code.
Remember, you have to use the fully qualified name (hello.Hello) because it is part of a package.
Please see K&R SCJP book, Chapter 10, specifically page 792, which explains compiling and running with packages.
Fergal Crawley
Greenhorn

Joined: Nov 28, 2011
Posts: 18

Walter Ho wrote:...Aha. You're using a package.
Type in "cd C:\java\hello\src\" into the command line, it should output "C:\java\hello\src\" on the command line.
You then compile the code with "javac hello\Hello.java" on that command line, it just outputs ""C:\java\hello\src\" again, providing there are no compiler errors.
Now to run your code, you have use "java hello.Hello" to run the code....


Thanks Walter, that's exactly what I needed to do, problem solved

Thanks also Chris, I'll consider those unwanted file extensions should I run into any further problems.

I also came across a couple of useful articles that others with similar issues might find helpful.

Installing and Configuring JDK
Compiling and Running Java from the Command Window

 
jQuery in Action, 2nd edition
 
subject: Running Java Programs from the command prompt
 
Similar Threads
Problem with JDK at command prompt
How to open command prompt from a java program
A Java service!?
Command Prompt, not working??
Protected