How do I make my java application open a console/terminal window?

前端 未结 13 1294
日久生厌
日久生厌 2020-12-28 21:12

Is there any way I can make an executable .jar that will open up the command line when double clicked?

I\'m making a text-based adventure game. As of right now it is

相关标签:
13条回答
  • 2020-12-28 21:39

    One way to accomplish this is to create a .bat file with the command: "java -jar filePath/yourfile.jar" (without ""). Make sure to include the file path or else your file won't be found. Though the question was answered already, this is a simple way to do it.

    0 讨论(0)
  • 2020-12-28 21:43
    1. Use Launch4j and in Basic Tab give your exe name in Output file and then load a jar file in Jar Tab.
    2. Go to Header Tab and select Console.
    3. Then go to JRE tab and give the JRE version e.g 1.8.0
    4. Then click the build wrapper button (kind of setting icon)
    5. It will ask you to save a xaml file just put some random name and click save.
    6. Finally your .exe is created and you can run now.
    0 讨论(0)
  • 2020-12-28 21:44

    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).

    0 讨论(0)
  • 2020-12-28 21:46

    Or you can provide a .sh .bat that will open a terminal and call your java in it.

    0 讨论(0)
  • 2020-12-28 21:51

    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.

    0 讨论(0)
  • 2020-12-28 21:52

    Double-clicking a jar opens it with whatever application you've associated to it in your OS. By default, javaw[.exe] is normally associated to jar files. That's the binary that runs without a terminal window. To see a terminal on double-click, you'd need to associate the java[.exe] binary with jar files.

    0 讨论(0)
提交回复
热议问题