How to Run a Java Program from the Command Line on Windows

How do I run a Java program from the command line on Windows?

Source: javaindos.

Let's say your file is in C:\mywork\

Run Command Prompt

C:\> cd \mywork

This makes C:\mywork the current directory.

C:\mywork> dir

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

C:\mywork> set path=%path%;C:\Program Files\Java\jdk1.5.0_09\bin

This tells the system where to find JDK programs.

C:\mywork> javac filenamehere.java

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

C:\mywork> dir

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

C:\mywork> java filenamehere

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!

Running java programs from the command line on Windows 10

Steps to fix this error in windows 10/8/7

1.Check your javac path on Windows using Windows Explorer C:\Program Files\Java\jdk1.7.0_02\bin and copy the address.

2.Go to Control Panel. Environment Variables and Insert the address at the beginning of var. Path followed by semicolon. i.e C:\Program Files\Java\jdk1.7.0_02\bin; . Do not delete the path existent, just click in and go to the left end and paste the line above. Do not try anything else, because you just need to link your code to "javac.exe" and you just need to locate it.

3.Close your command prompt and reopen it,and write the code for compile and execution.
Sample Image

Running a java program on command prompt

Solution:

I was using "Sublime Text 3" when writing and saving my test.java program. @Silvio Mayolo suggested using a java decompiler to find out the problem, and I noticed that when saving my program in Sublime, the static gets deleted in test.java file for some reason. Then I did the following steps:

  1. I closed sublime text 3
  2. I opened my test.java file using notepad. I realized that static was missing after public, so it was public void main(String args){}.
  3. I added static in notepad, so it became public static void main(String[] args){}
  4. I saved the file again in notepad.
  5. I ran javac test.java in command prompt, and then java test, and I got my
    Hello World output.

How to run a java program in cmd with arguments

java Test arg1 arg2 arg3 ...

or

java Test "arg1 arg2 arg3" ...

More details here Command-Line Arguments

Trying to build/run java program from cmd line with external library

java -cp "ejml-v0.34-libs/*;." MatrixServer

. to include your current directory(where compiled files are located ) along with ejml-v0.34-libs folder.

<classpath> - list of directories and/or JAR-files where needed classes reside separated by ";" for Windows or ":" for linux (default classpath is "." - the current directory);

run java file in windows command prompt

You are missing the actual class to be run. The -cp H:\deleteFiles\deleteFiles.delete only defines the classpath to be used, but not which class you want to run (and you limit the classpath to a single class as well).

What you want is:

java -cp H:\deleteFiles\deleteFiles delete

Note the blank (space) between H:\deleteFiles\deleteFiles which means you are passing two parameters to the java command:

  1. -cp H:\deleteFiles\deleteFiles - the classpath to use
  2. delete - the class to run

If you need the classes that are part of the jar files, you need to add them to the classpath as well:

java -cp H:\deleteFiles\deleteFiles;H:\deleteFiles\deleteFiles\a.jar;H:\deleteFiles\deleteFiles\b.jar delete

How to run java program in command prompt,created by intellij

Three issues:

  1. You have to specify the fully qualified class name (that means including the package name) to the java command. It looks like your myjava class is in a package com.myexample.test. So its fully qualified name is com.myexample.test.myjava.

  2. When you run the java command you have to be in the directory that is at the base of the package hierarchy (or put that directory on the classpath).

  3. You're using the src directory, which contains .java source files, but the java command expects compiled .class files, so you need to use the project's output directory. Its location in your project will depend on your IDE and configuration but it will contain same-named structure as inside src, except with .class files instead of .java files.

In your case, navigate to:

C:\myjava\sampl1\out\production\

Then run:

java com.myexample.test.myjava


Related Topics



Leave a reply



Submit