• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Running Java Programs from the command prompt

 
Greenhorn
Posts: 18
Netbeans IDE Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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!
 
Greenhorn
Posts: 22
Chrome Java Windows
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Netbeans IDE Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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"
 
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Netbeans IDE Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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!
 
Bartender
Posts: 5167
11
Netbeans IDE Opera Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If your class is named First it should be in a file named First.java. Not myfirstprogram.java.
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.

 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic