To view the Java console, right click on the Java icon in the system tray (assuming you're using Windows) and choose "Open console" - as pictured at the bottom of this page
Answer from George3 on Stack ExchangeTo view the Java console, right click on the Java icon in the system tray (assuming you're using Windows) and choose "Open console" - as pictured at the bottom of this page
- Click Start
- Select Settings
- Select Control Panel.
- Double click the Java icon.
- Click the Advance tab.
- Click on the + sign
- Select Show Console and click Apply.

How do I make my java application open a console/terminal window? - Stack Overflow
cmd - How do I run a Java program from the command line on Windows? - Stack Overflow
How to get right to "Java" in control panel from cmd line in win7? - Stack Overflow
Running Java Programs in Windows 10 Command Prompt - Stack Overflow
Videos
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.

You need to add the location of your JDK to your PATH variable, if you wish to call javac.exe without the path.
set PATH=%PATH%;C:\path\to\your\JDK\bin\dir
Then...
javac.exe MyFirstProgram.java
OR, you can simply call it via the full path to javac.exe from your JDK installation e.g.
C:\path\to\your\JDK\bin\javac.exe MyFirstProgram.java
I found this while looking for an answer myself, I ended up writing this bit:
/**
* This opens a command line and runs some other class in the jar
* @author Brandon Barajas
*/
import java.io.*;
import java.awt.GraphicsEnvironment;
import java.net.URISyntaxException;
public class Main{
public static void main (String [] args) throws IOException, InterruptedException, URISyntaxException{
Console console = System.console();
if(console == null && !GraphicsEnvironment.isHeadless()){
String filename = Main.class.getProtectionDomain().getCodeSource().getLocation().toString().substring(6);
Runtime.getRuntime().exec(new String[]{"cmd","/c","start","cmd","/k","java -jar \"" + filename + "\""});
}else{
THEMAINCLASSNAMEGOESHERE.main(new String[0]);
System.out.println("Program has ended, please type 'exit' to close the console");
}
}
}
not sure if my answer is still relevant, but feel free to use it with the comment kept in o/
Only flaw I can think of is that it leaves the cmd window open after the program completes.
Usage: place this class in the same package as your main class and set it as the main class, it will open a command prompt window if one is not open, or if one is open launch the main class. Name / location of jar file is automatic. Designed for windows, but if you want it for another system just message me and I'll fix it. (I could do OS detection but I'm lazy and just making this so I can turn in a double-click jar file to my professor who uses windows).
If you want full control, you can implement a Console window in Swing which does what you have now.
If you cannot open said window (if headless) or the user asks for it on the command line, then just default to your current behaviour.
Source: javaindos.
Let's say your file is in C:\mywork\
Run Command Prompt
CopyC:\> cd \myworkThis makes C:\mywork the current directory.
CopyC:\mywork> dirThis displays the directory contents. You should see filenamehere.java among the files.
CopyC:\mywork> set path=%path%;C:\Program Files\Java\jdk1.5.0_09\binThis tells the system where to find JDK programs.
CopyC:\mywork> javac filenamehere.javaThis runs javac.exe, the compiler. You should see nothing but the next system prompt...
CopyC:\mywork> dirjavac has created the filenamehere.class file. You should see filenamehere.java and filenamehere.class among the files.
CopyC:\mywork> java filenamehereThis 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!
To complete the answer :
The Java File
Copy
TheJavaFile.javaCompile the Java File to a *.class file
Copy
javac TheJavaFile.java- This will create a
TheJavaFile.classfile
- This will create a
Execution of the Java File
Copy
java TheJavaFileCreation of an executable
*.jarfileYou've got two options here -
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
Executable by Entry Point:
jar -cvfe TheJavaFile.jar <MainClass> TheJavaFile.class
To run the Jar File
Copy
java -jar TheJavaFile.jar
Alternate method of launching Java Control Panel:
Click Windows Start button.
In the Start Search box, type:
Windows 32-bit OS:
C:\Program Files\Java\jre7\bin\javacpl.exe**Windows 64-bit OS:
C:\Program Files (x86)\Java\jre7\bin\javacpl.exe**
Ref: Docs
If you really don't have shortcut to "Configure Java" in the menu start, then run directly this Java Panel by accessing C:\Program Files\Java\jreJAVA_VERSION\bin\javacpl.exe or C:\Program Files (x86)\Java\jreJAVA_VERSION\bin\javacpl.exe
If you only have jdk installed, then C:\Program Files\Java\jdkJAVA_VERSION\jre\bin\javacpl.exe
Java doesn't actually have to be installed. You can run any Java that's available to you.
This is sometimes useful for running Java from a network share.
Try running the java.exe program by fully qualifying it. If it runs that way, your problem is with your PATH.
To illustrate that you can run any Java by qualifing it, I have 9 JDK's and 1 JRE installed:
- 32-bit JDK: Versions 1.4, 5, 6, 7, and 8
- 64-bit JDK: Versions 5, 6, 7, and 8
- 32-bit JRE: Version 8
Copied from a single command prompt window:
C:\>"C:\prog\java32\jdk1.4.2_19\bin\java.exe" -version
java version "1.4.2_19"
Java(TM) 2 Runtime Environment, Standard Edition (build 1.4.2_19-b04)
Java HotSpot(TM) Client VM (build 1.4.2_19-b04, mixed mode)
C:\>"C:\prog\java32\jdk1.5.0_22\bin\java.exe" -version
java version "1.5.0_22"
Java(TM) 2 Runtime Environment, Standard Edition (build 1.5.0_22-b03)
Java HotSpot(TM) Client VM (build 1.5.0_22-b03, mixed mode)
C:\>"C:\prog\java32\jdk1.6.0_45\bin\java.exe" -version
java version "1.6.0_45"
Java(TM) SE Runtime Environment (build 1.6.0_45-b06)
Java HotSpot(TM) Client VM (build 20.45-b01, mixed mode, sharing)
C:\>"C:\prog\java32\jdk1.7.0_79\bin\java.exe" -version
java version "1.7.0_79"
Java(TM) SE Runtime Environment (build 1.7.0_79-b15)
Java HotSpot(TM) Client VM (build 24.79-b02, mixed mode, sharing)
C:\>"C:\prog\java32\jdk1.8.0_51\bin\java.exe" -version
java version "1.8.0_51"
Java(TM) SE Runtime Environment (build 1.8.0_51-b16)
Java HotSpot(TM) Client VM (build 25.51-b03, mixed mode)
C:\>"C:\prog\java64\jdk1.5.0_22\bin\java.exe" -version
java version "1.5.0_22"
Java(TM) 2 Runtime Environment, Standard Edition (build 1.5.0_22-b03)
Java HotSpot(TM) 64-Bit Server VM (build 1.5.0_22-b03, mixed mode)
C:\>"C:\prog\java64\jdk1.6.0_45\bin\java.exe" -version
java version "1.6.0_45"
Java(TM) SE Runtime Environment (build 1.6.0_45-b06)
Java HotSpot(TM) 64-Bit Server VM (build 20.45-b01, mixed mode)
C:\>"C:\prog\java64\jdk1.7.0_79\bin\java.exe" -version
java version "1.7.0_79"
Java(TM) SE Runtime Environment (build 1.7.0_79-b15)
Java HotSpot(TM) 64-Bit Server VM (build 24.79-b02, mixed mode)
C:\>"C:\prog\java64\jdk1.8.0_51\bin\java.exe" -version
java version "1.8.0_51"
Java(TM) SE Runtime Environment (build 1.8.0_51-b16)
Java HotSpot(TM) 64-Bit Server VM (build 25.51-b03, mixed mode)
C:\>"C:\Program Files (x86)\Java\jre1.8.0_51\bin\java.exe" -version
java version "1.8.0_51"
Java(TM) SE Runtime Environment (build 1.8.0_51-b16)
Java HotSpot(TM) Client VM (build 25.51-b03, mixed mode, sharing)
Open command prompt cmd and type where java , this will output where java executable is, then you can go to System Propreties>Advanced>Environement Variables> (Right click on computer and click on Propreties)
and add the where java output to the PATH variable separated with a semi-colon ;
I know that people recommend staying away from rt.exec(String), but this works, and I don't know how to change it into the array version.
rt.exec("cmd.exe /c cd \""+new_dir+"\" & start cmd.exe /k \"java -flag -flag -cp terminal-based-program.jar\"");
If you are running two commands at once just to change the directory the command prompt runs in, there is an overload for the Runtime.exec method that lets you specify the current working directory. Like,
Runtime rt = Runtime.getRuntime();
rt.exec("cmd.exe /c start command", null, new File(newDir));
This will open command prompt in the directory at newDir. I think your solution works as well, but this keeps your command string or array a little cleaner.
There is an overload for having the command as a string and having the command as a String array.
You may find it even easier, though, to use the ProcessBuilder, which has a directory method to set your current working directory.
Hope this helps.
Second Edit: May have fixed the Environment Variables to work with JAVA_HOME. Minor change to what was printed but was able to run the file like so:
C:\Users\Test>cd Documents\MyFirstProgramFolder C:\Users\Test\Documents\MyFirstProgramFolder>java MyFirstProgram We did it! Again! Number 3!
Again, making sure I was in the right directory was the first issue, but now I may have the Environment Variables fixed. I hope at least. Thank you again everyone.
Edit: Have my issue mostly solved. I am able to compile and run the code. cd-ing to the correct directory seemed to be the issue in the command prompt and that is all I really need for now. Thank you to everyone who took the time try to help me!
Hello! I am new to learning Java but have been stuck on a single issue for the past day and a half. I have recently started reading Head First Java to try and teach myself Java. I took a single programming class that was taught in Java 3-4 years ago. All of the programming experience I have has always been in some sort of IDE. In Head First Java they want you to work through the command line after writing code. I have been entirely unsuccessful at setting up Java to work through the command prompt. I have now tried to download 2-3 different JDK's, followed 2-3 tutorials on how to just get "Hello World" to print, and followed a guide on how to set up the Java Environment. None have been successful and I am frequently met with the below errors/issues when I try to parse together my understanding trying to fix the issue. I would really like to learn how to actually do things without the need of an IDE. Any help at all would be greatly appreciated! So the question summed up, is more or less where am I going wrong for setting up the Java environment, or JVM?
In the Command Prompt:
C:\Users\Test>"C:\Program Files\Java\jdk1.8.0_161\bin"\Javac MyFirstProgram.java
javac: file not found: MyFirstProgram.java
Usage: javac <options> <source files>
use -help for a list of possible options
In Windows Powershell:
PS C:\Users\Test\Documents\MyFirstProgramFolder> "C:\Program Files\Java\jdk1.8.0_161\bin"\javac MyFirstProgram.java
At line:1 char:41
+ "C:\Program Files\Java\jdk1.8.0_161\bin"\javac MyFirstProgram.java
+ ~~~~~~
Unexpected token '\javac' in expression or statement.
+ CategoryInfo : ParserError: (:) [], ParentContainsErrorRecordException
+ FullyQualifiedErrorId : UnexpectedToken
Below are some of the websites I have tried:
https://www.instructables.com/How-to-Make-Your-First-Computer-Program/ - This instructables link is what I just used to receive the above responses.
https://www.tutorialspoint.com/java/java_environment_setup.htm
https://www.geeksforgeeks.org/setting-environment-java/
The Code:
class MyFirstProgram {
public static void main(String[] args) {
System.out.println("Hello, World");
}
}