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.
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:
- I closed sublime text 3
- I opened my test.java file using notepad. I realized that static was missing after public, so it was public void main(String args){}.
- I added static in notepad, so it became public static void main(String[] args){}
- I saved the file again in notepad.
- 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:
-cp H:\deleteFiles\deleteFiles
- the classpath to usedelete
- 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:
You have to specify the fully qualified class name (that means including the package name) to the
java
command. It looks like yourmyjava
class is in a packagecom.myexample.test
. So its fully qualified name iscom.myexample.test.myjava
.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).You're using the
src
directory, which contains.java
source files, but thejava
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 insidesrc
, 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
What Does "Possible Lossy Conversion" Mean and How to Fix It
How to Put a Control in the Jtableheader of a Jtable
Java Swing Revalidate() VS Repaint()
What Is the Concept of Erasure in Generics in Java
Why Does Java'S Hashcode() in String Use 31 as a Multiplier
How to Get a Platform-Dependent New Line Character
List of Java Class File Format Major Version Numbers
Occurrences of Substring in a String
How to Sort by Two Fields in Java
How to Check If a String Contains Another String in a Case Insensitive Manner in Java
Creating an Instance Using the Class Name and Calling Constructor
Normalization in Dom Parsing With Java - How Does It Work
Why Can't I Define a Static Method in a Java Interface