tuxxi
tuxxi

Reputation: 709

Start a java program without the console

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

Upvotes: 61

Views: 101512

Answers (8)

rickybustillos
rickybustillos

Reputation: 41

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

Upvotes: 0

developer learn999
developer learn999

Reputation: 395

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

supervisor windows based on python

Upvotes: 0

RollingBoy
RollingBoy

Reputation: 2817

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.

Upvotes: 123

Coder Kid
Coder Kid

Reputation: 61

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.

Upvotes: 2

David Newcomb
David Newcomb

Reputation: 10943

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.

Upvotes: 3

Dave
Dave

Reputation: 507

start javaw -jar yourjar.jar arg0 arg1

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

Upvotes: 4

Nans
Nans

Reputation: 156

Create a .bat file with

start javaw -jar yourjar.jar arg0 arg1

Upvotes: 10

Yi Zhao
Yi Zhao

Reputation: 6784

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

Upvotes: 12

Related Questions