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.

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.
How to get right to "Java" in control panel from cmd line in win7? - Stack Overflow
process - Java: Open console programmatically and execute commands from it - Stack Overflow
How to open the command prompt and insert commands using Java? - Stack Overflow
macos - Where is my Java console on OS X? - Ask Different
Videos
Here is an example I used to create a launch icon for a program.
Runtime rt = Runtime.getRuntime();
rt.exec("cmd.exe /c cd \""+"c:\\CombineImages\\"+"\" & start cmd.exe /k \"java -Xms1G -Xmx1G jar CombineImages.jar\"");
Put that code in your main method and replace with whatever command you want to run.
This should work
You need something like
String[] command =
{
"cmd",
};
Process p = Runtime.getRuntime().exec(command);
new Thread(new SyncPipe(p.getErrorStream(), System.err)).start();
new Thread(new SyncPipe(p.getInputStream(), System.out)).start();
PrintWriter stdin = new PrintWriter(p.getOutputStream());
stdin.println("dir c:\\ /A /Q");
// write any other commands you want here
stdin.close();
int returnCode = p.waitFor();
System.out.println("Return code = " + returnCode);
SyncPipe Class:
class SyncPipe implements Runnable
{
public SyncPipe(InputStream istrm, OutputStream ostrm) {
istrm_ = istrm;
ostrm_ = ostrm;
}
public void run() {
try
{
final byte[] buffer = new byte[1024];
for (int length = 0; (length = istrm_.read(buffer)) != -1; )
{
ostrm_.write(buffer, 0, length);
}
}
catch (Exception e)
{
e.printStackTrace();
}
}
private final OutputStream ostrm_;
private final InputStream istrm_;
}
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.
It is somewhat maddening, but the Java console won't display unless a Java application is actually running. The easiest way to get it started is to visit https://www.java.com/verify/ from the Firefox browser.
If you're opening a Runnable Jar (one which you click in finder to open), just try running it in terminal (from Spotlight search) using java -jar /path/to/file.jar.
If it's a Java Web Applet, you need to go into System Preferences -> Java -> Advanced -> Show Console -> Apply. From here, you'll need to actually run the program before the console displays, as @danorton said.
'The easiest way to get it started is to visit https://www.java.com/verify/ from the Firefox browser.'
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