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!
- 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.
- 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)
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
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
The Start in
field in the shortcut properties I put
C:\YourServerDirectory
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
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 reason
– Deme