I found this, looking for the answer myself, I ended up writing this bit:
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 contained in o /
The only drawback I can think of is that it leaves the cmd window after the program terminates.
Usage: put 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 it is not open, or if one of them starts the main class. The name / location of the jar file automatically. Designed for windows, but if you want another system to just let me know and I will fix it. (I could detect the OS, but I'm lazy and just doing it, so I can include the double-click jar file to my professor who uses windows).
Brandon barajas
source share