File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Running Java Programs from the command prompt

 
Fergal Crawley
Greenhorn
Posts: 18
Chrome Java Netbeans IDE
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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!
 
Walter Ho
Greenhorn
Posts: 22
Chrome Java Windows
  • 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 18
Chrome Java Netbeans IDE
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 22
Chrome Java Windows
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
nothing wrong at all.
You only compiled the code so that the JVM can run it.

To run it.
"java Hello"
 
Chris Rothburn
Greenhorn
Posts: 15
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 22
Chrome Java Windows
  • 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 18
Chrome Java Netbeans IDE
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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

 
Poornima shridhar
Greenhorn
Posts: 2
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am not able to see the output in my command prompt ! if their is an error it is shown otherwise my output is not shown

code :


class First {
public static void main(String[] arguments) {
System.out.println("Let's do something using Java technology.");
}
}


path is :C:\Program Files\Java\jdk1.7.0_60\bin\javac myfirstprogram.java

thanks in advance!
 
Darryl Burke
Bartender
Posts: 5115
11
Java Netbeans IDE Opera
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If your class is named First it should be in a file named First.java. Not myfirstprogram.java.
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Pie
Posts: 15150
31
Android IntelliJ IDE Java Scala Spring
  • 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Oracle's Java tutorials have a good explanation of this, in the Getting Started section.

The tutorial explains how you go from source file to class file, and how you then run that class file, step by step.

 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic