How to make java application open console / terminal window?

Is there a way to make an .jar executable that opens a command prompt when double clicked?

I am making a text adventure game. At the moment, it's just a maze with rooms. In the end, it will be much larger and deeper, but for now I just want the main structure to be omitted. In any case, in order to complete this work, I get the output and input from the System.out.printf and java.util.Scanner commands. All of this works fine so far, but I realized that I will have problems when I try to send it to other people who do not know how or simply do not want to run the program from the command line.

+7
source share
9 answers

If you need full control, you can implement the console window in Swing, which does what you have now.

If you cannot open the specified window (if it does not work), or the user asks for it on the command line, then your current behavior will simply act by default.

+5
source

I found this, looking for the 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 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).

+11
source

Double-clicking on the bank opens it using any application associated with it in your OS. By default, javaw [.exe] is usually associated with jar files. This is a binary file that works without terminal windows. To see the terminal with a double click, you need to link the java binary [.exe] file with the jar files.

+3
source

Or you can provide .sh.bat which will open a terminal and call your java.

+2
source

As long as the .jar executable is executed using java.exe, a command prompt window will always be displayed. doing this with javaw.exe will prevent this.

for further reading: http://download.oracle.com/javase/1.4.2/docs/tooldocs/windows/java.html

+1
source

I think the easiest way is to write a simple shortcut for your jar file. for example, eclipse (like most ide) can export a jar file with all the necessary libraries, etc., so you just need to set the shortcut command as "java -jar filePath / file.jar" (filePath: e.g. / bin / file. jar)

0
source

One way to achieve this is to create a .bat file using the command: "java -jar filePath / yourfile.jar" (without ""). Do not forget to specify the path to the file, otherwise your file will not be found. Although this question has already been answered, this is an easy way to do this.

0
source

You can create your own window using Swing or Awt using TextPane, the only problem ... is how to enter and use how cmd does it. But you can always do this with warnings and all ...

Another method is executed directly from the batch file displayed on the console.

You should also consider that your game DIRECTLY on a batch ... is not a bad language to create and is present on every Windows operating system.

(Hope was helpful (because I'm new), and my English was NOT SO BAD ...)

0
source
  • Use Launch4j in the Basic Tab, enter the exe name in the Output file, and then load the jar file into the Jar Tab.
  • Go to the "Title" tab and select "Console".
  • Then go to the JRE tab and give the JRE version like 1.8.0
  • Then click the assembly cover button (view of the settings icon)
  • He will ask you to save the xaml file, just enter an arbitrary name and click "Save".
  • Finally, your .exe is created, and you can run it now.
0
source

All Articles