Execute Java Program In Cmd

How to run applet in appletviewer. We can run applet in two ways first is run applet using html and second is run applet using appletviewer. 1.click on start then run. How to run applet using appletviewer. Type cmd in run. How to run applet using appletviewer. 3.type javac A.java(Filename.java). Compiling the Java program: On the command prompt use the command 'javac PrintHelloWorld.java' to compile the program. It should compile with out any errors. If it says 'not a recognized program', then it means the java is not installed or it is not proper. Go to Installation Of Java on your PC for installing the same. How to execute cmd commands via Java. Ask Question Asked 8 years, 10 months ago. Active 3 years, 5 months ago. Adapting C fork code to a Java program. How to execute cmd commands through java. Specific Prompt command with java.

  1. How To Execute Java Program In Command Prompt
  2. Execute Java Program In Cmd
  3. Execute Java Program Using Cmd
Active4 months ago

You will use the java command to execute your program. From the Command Prompt, type the java command below. C: introcs hello java HelloWorld Hello, World If all goes well, you should see the output of the program - Hello, World. It is path in your system where java compiler is available (For example variable value:C: Program Files Java jdk1.6.023 bin ). Inside bin javac is Java compiler. Step 4: Go to command prompt by using start-Run-cmd OR start- type cmd in search program and file. Type the program's name into Command Prompt. This must be the file's system name, not its shortcut name (for example, Command Prompt's system name is cmd). Press ↵ Enter. Once your command resembles start programname, doing so will run the 'start' command for your selected program.

I'm trying to execute a Java program from the command line in Windows. Here is my code:

Java software

I'm not sure how to execute the program - any help? Is this possible on Windows? Why is it different than another environment (I thought JVM was write once, run anywhere)?

Bob Jarvis
37.7k6 gold badges63 silver badges93 bronze badges
Elizabeth Turner

How To Execute Java Program In Command Prompt

Elizabeth Turner
1,1013 gold badges10 silver badges11 bronze badges

12 Answers

Source: javaindos.

Let's say your file is in C:mywork

Run Command Prompt

This makes C:mywork the current directory.

This displays the directory contents. You should see filenamehere.java among the files.

This tells the system where to find JDK programs.

This runs javac.exe, the compiler. You should see nothing but the next system prompt...

javac has created the filenamehere.class file. You should see filenamehere.java and filenamehere.class among the files.

This runs the Java interpreter. You should then see your program output.

If the system cannot find javac, check the set path command. If javac runs but you get errors, check your Java text. If the program compiles but you get an exception, check the spelling and capitalization in the file name and the class name and the java HelloWorld command. Java is case-sensitive!

Liam
17.3k16 gold badges80 silver badges135 bronze badges
Nicholas KadaeuxNicholas Kadaeux
2,3262 gold badges10 silver badges11 bronze badges
Execute Java Program In Cmd

To complete the answer :

  1. The Java File

  2. Compile the Java File to a *.class file

    • This will create a TheJavaFile.class file
  3. Execution of the Java File

  4. Creation of an executable *.jar file

    • You've got two options here -

      1. With an external manifest file :

        • Create the manifest file say - MANIFEST.mf

        • The MANIFEST file is nothing but an explicit entry of the Main Class

        • jar -cvfm TheJavaFile.jar MANIFEST.mf TheJavaFile.class

      2. Executable by Entry Point:

        • jar -cvfe TheJavaFile.jar <MainClass> TheJavaFile.class
  5. To run the Jar File

Jiri Tousek
11.2k5 gold badges24 silver badges40 bronze badges
jkhoslajkhosla
1,4671 gold badge9 silver badges12 bronze badges

In case your Java class is in some package. Suppose your Java class named ABC.java is present in com.hello.programs, then you need to run it with the package name.

Compile it in the usual way:

But to run it, you need to give the package name and then your java class name:

Dawid Ferenczy Rogožan
13.1k8 gold badges52 silver badges55 bronze badges
Harshad HolkarHarshad Holkar

Complile a Java file to generate a class:

Execute the generated class:

Dawid Ferenczy Rogožan
13.1k8 gold badges52 silver badges55 bronze badges
Paresh3489227Paresh3489227

It is easy. If you have saved your file as A.text first thing you should do is save it as A.java. Now it is a Java file.

Now you need to open cmd and set path to you A.java file before compile it. you can refer this for that.

Then you can compile your file using command

javac A.java

Then run it using

java A

So that is how you compile and run a java program in cmd.You can also go through these material that is Java in depth lessons. Lot of things you need to understand in Java is covered there for beginners.

Execute Java Program In Cmd
Eliza HelenaEliza Helena

You can compile any java source using javac in command line ; eg, javac CopyFile.java.To run : java CopyFile.You can also compile all java files using javac *.java as long as they're in the same directory

If you're having an issue resulting with 'could not find or load main class' you may not havejre in your path. Have a look at this question:Could not find or load main class

Community
monikamonika
3651 gold badge5 silver badges17 bronze badges

Assuming the file is called 'CopyFile.java', do the following:

The first line compiles the source code into executable byte code. The second line executes it, first adding the current directory to the class path (just in case).

YakovKYakovK

On Windows 7 I had to do the following:

quick way

  1. Install JDK http://www.oracle.com/technetwork/java/javase/downloads
  2. in windows, browse into 'C:Program FilesJavajdk1.8.0_91bin' (or wherever the latest version of JDK is installed), hold down shift and right click on a blank area within the window and do 'open command window here' and this will give you a command line and access to all the BIN tools. 'javac' is not by default in the windows system PATH environment variable.
  3. Follow comments above about how to compile the file ('javac MyFile.java' then 'java MyFile') https://stackoverflow.com/a/33149828/194872

long way

  1. Install JDK http://www.oracle.com/technetwork/java/javase/downloads/index.html
  2. After installing, in edits the Windows PATH environment variable and adds the following to the path C:ProgramDataOracleJavajavapath. Within this folder are symbolic links to a handful of java executables but 'javac' is NOT one of them so when trying to run 'javac' from Windows command line it throws an error.
  3. I edited the path: Control Panel -> System -> Advanced tab -> 'Environment Variables...' button -> scroll down to 'Path', highlight and edit -> replaced the 'C:ProgramDataOracleJavajavapath' with a direct path to the java BIN folder 'C:Program FilesJavajdk1.8.0_91bin'.
  4. This likely breaks when you upgrade your JDK installation but you have access to all the command line tools now.

  5. Follow comments above about how to compile the file ('javac MyFile.java' then 'java MyFile') https://stackoverflow.com/a/33149828/194872

Community
Chris SmithChris Smith

STEP 1: FIRST OPEN THE COMMAND PROMPT WHERE YOUR FILE IS LOCATED. (right click while pressing shift)
STEP 2: THEN USE THE FOLLOWING COMMANDS TO EXECUTE. (lets say the file and class name to be executed is named as Student.java)The example program is in the picture background.

Execute Java Program In Cmd

Ashwant ManikothAshwant Manikoth

As of Java 9, the JDK includes jshell, a Java REPL.

Assuming the JDK 9+ bin directory is correctly added to your path, you will be able to simply:

  1. Run jshell File.javaFile.java being your file of course.
  2. A prompt will open, allowing you to call the main method: jshell> File.main(null).
  3. To close the prompt and end the JVM session, use /exit

Full documentation for JShell can be found here.

HorseyHorsey

Now (with JDK 9 onwards), you can just use java to get that executed.In order to execute 'Hello.java' containing the main, one can use: java Hello.java

You do not need to compile using separately using javac anymore.

ShibaShiba

Since Java 11, java command line tool has been able to run a single-file source-code directly. e.g.

This was an enhancement with JEP 330: https://openjdk.java.net/jeps/330

For the details of the usage and the limitations, see the manual of your Java implementation such as one provided by Oracle: https://docs.oracle.com/en/java/javase/11/tools/java.html

akawagucakawaguc

protected by CommunityJan 6 '17 at 12:18

Thank you for your interest in this question. Because it has attracted low-quality or spam answers that had to be removed, posting an answer now requires 10 reputation on this site (the association bonus does not count).
Would you like to answer one of these unanswered questions instead?

Execute Java Program Using Cmd

Not the answer you're looking for? Browse other questions tagged java or ask your own question.