Start a java program without the console
Asked Answered
G

8

61

I am trying to use this GUI mod for a Minecraft Server. I wrote a batch file so the server can start with more RAM. When I run just the .jar file, no command window opens and it runs just fine (of course with about 256mb ram) I was reading online that javaw starts a jar file without a command-line-console. But when I use javaw, the command console opens, but when I close it the program remains open. this is my batch file:

@echo off 
"%ProgramFiles(x86)%\Java\jre6\bin\javaw.exe" -jar -Xms1024m -Xmx1024m crafty.jar 
@echo on

I don't understand java as well as most, so please try to be as clear as possible. Thanks

Glockenspiel answered 19/4, 2011 at 2:8 Comment(4)
Maybe, the app is programmed to close the GUI frame only, not the whole process itself in its setDefaultCloseOperation(op) call. download.oracle.com/javase/1.4.2/docs/api/javax/swing/… Or, The javaw command is identical to java, except that with javaw there is no associated console window. Use javaw when you don't want a command prompt window to appear. The javaw launcher will, however, display a dialog box with error information if a launch fails for some reasonDeme
Shouldn't this be on gaming.stackexchange.com?Paragon
@Blessed Geek: no. Super User maybe. But now there are three close-votes for off-topic, so it cannot be migrated anymore.Retread
@Glockenspiel I guess u should accept an answer if you are still able to read this comment.Chew
I
123

If you want to start a java program without console popup under windows, this should be helpful: In command prompt type the following:

start javaw -jar -Xms1024m -Xmx1024m crafty.jar

If you want you can also write this as a batch file.

Impedance answered 19/4, 2011 at 2:30 Comment(2)
This is excelent on windows 7. I tried "start java ..." and it still leaves cmd.exe window on, javaw works great. Thanks.Camembert
Yes, in Windows 7, you must use javaw -jar ... without start.Scrapple
G
12
  • You should Create Shortcut of "%ProgramFiles(x86)%\Java\jre6\bin\javaw.exe", let's name it as Minecraft, then
  • edit the Properties of Minecraft shortcut. In the Target textbox, append -jar -Xms1024m -Xmx1024m crafty.jar in the end of javaw.exe
  • change the Start in as the folder which contains the crafty.jar

Double-click the Minecraft icon to star the server.

That's all.

Gallup answered 19/4, 2011 at 2:41 Comment(3)
You can also do this with the EXE version: start javaw -jar -Xms1024m -Xmx1024m Minecraft.exePenthouse
one has to use javaw.exe, not java.exe!Komsomol
Some Applications, especially java Applications, have a large number of start-up command-line options which will exceed the 260 character limit for the shortcut "Target" field. In this case, create an environment variable with all the options in it and use the variable in the "target" field.Tremolite
J
10

Create a .bat file with

start javaw -jar yourjar.jar arg0 arg1
Judiejudith answered 13/3, 2012 at 13:24 Comment(0)
W
4
start javaw -jar yourjar.jar arg0 arg1

will open the console, but close immediately. it is different from running window .exe.

Wildebeest answered 28/4, 2013 at 23:26 Comment(0)
S
3

You will always get the command window opening and closing because you are starting it inside a command window or batch script (which launches an implicit command window to run itself). In order not to get a command window you must open the file from "not a command window" i.e. an executable launcher.

Take a look at Launch4j which can run a java program from an exe. It can also hide-away the jar file inside the exe if you like.

http://launch4j.sourceforge.net/

There's a little YouTube clip showing them creating an exe from a jar.

Styptic answered 19/7, 2013 at 18:11 Comment(0)
B
2

A batch file is a way of starting the command prompt with the code pre-written, using javaw is a way of open then closing the prompt. Like I said a batch is a commands prompt you can't stop it from opening.

Bergess answered 29/7, 2014 at 21:31 Comment(1)
I'm very sorry about that I had the same problemBergess
B
0

It's few years, but for windows today as for linux you have the supervisor (pytho based)

supervisor windows based on python

Beal answered 7/4, 2020 at 7:24 Comment(0)
C
0

I have a solution for my case: I wanted to start a server.jar without CMD or PowerShell window, only with the .jar window.

Run Java command without keeping Windows Terminal

Pre-requisites

  • Java JRE or JDK installed
  • JAVA_HOME environment variable configured.
  • Attention to PowerShell Execution Policy (Ex.: RemoteSigned)
    • In this solution it will bypass the policy, but it's good to understand it
  • Be a windows administrator user

Solution: Execute with PowerShell!

  1. Create a file named start.ps1 (Windows PowerShell Script) on your server directory, in this file should contains only the power shell command:
Start-Process -FilePath java -WindowStyle Hidden -ArgumentList "-Xms2G -Xmx4G -jar server.jar"

Note if your have a specific directory to your java version, you can change the file path attribute, for example: -FilePath "C:\YourSpecificDirectory\java\bin\java.exe" instead of -FilePath java.

In my case I have only one default version of Java in my PC and it's configured with JAVA_HOME environment variable, so I don't need to pass the complete file path like the example above.

  1. Run the start.ps1 with PowerShell and be happy!

Bonus: Easy shortcut for the script

To facilitate my use (I would like to start it from my Windows search bar, for example)

  1. Create a Windows Shortcut with your prefered name in this Directory:

    C:\Users\YOUR_USER\AppData\Roaming\Microsoft\Windows\StartMenu\Programs

Note to override the "YOUR_USER" to your current windows user name.

Shortcut file name example: MinecraftServer

  1. The Target field in the shortcut properties I put

    powershell.exe -ExecutionPolicy Bypass -File start.ps1

The result target will be like this automatically C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe -ExecutionPolicy Bypass -File start.ps1

  1. The Start in field in the shortcut properties I put

    C:\YourServerDirectory

  2. Done! When you try to search for "Minecraft Server" on the search bar the system will recommend you this Shortcut. Once executed it will start your server only with the Minecraft Server window.


Other automated possibilities

With this you can also create customized triggers with Windows Task Scheduler for running the script start.ps1. For example, whenever you log in your Windows User or when you start your PC.

See more about Windows Tasks.


My researches & References

Executing PowerShell command without Window

Running Java command with PowerShell

About PowerShell Execution Policy

Casimir answered 15/7 at 19:18 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.