I am working on a Java application which will be used to launch other applications. Now, most of those have a GUI, but some are command-line tools. For the latter, I would like to open the appropriate command line and have them execute there.
That is, on Windows, I would like to open cmd.exe, and run the tool with the appropriate arguments. The problem is getting cmd.exe to stay open.
When I use ProcessBuilder to start
cmd.exe /k java -version
I get the desired output (I capture the output streams), but the cmd.exe window isn't visible.
When I run
cmd.exe /k start java -version
the cmd.exe window pops up for a split second and then disappears.
But when I just run cmd.exe /k start the cmd.exe window appears and stays open, while cmd.exe start doesn't show a window (as I would expect).
What I want is for the command line tool to run in its own cmd.exe and stay open for input.
Running this from the cmd.exe works.
cmd start cmd /k java -version
However, this just waits for you to run more cmd's.
If you want it to wait for user input use
cmd start java MyProgramWhichWaitsForInput
This was solved using the answer in "Starting a Java tool with cmd.exe". I suspected the problems were separate, but they were not.
Related
I'm new to Java. I already installed Java, JDK and added it to Path, but when I try to run a command (like "java -version") it just flashes a CMD but doesn't show what I want to see. This happens with every command and doesn't even let me run Java code using an IDE. There is no error message, just a CMD flashing every time I try to run something (it also happens when double click the files like "java", "javac", etc).
How do you run these commands? Try running them from an interactive terminal window. Open it by typing windows key + R and then cmd. Then cd into your directory and type your commands there. Alternatively, if your commands are in a .bat file … you may add a pause command at the end.
This is entirely expected as java -version immediately exits after printing the version. This means that if you didn't start it from cmd, PowerShell, or another interactive shell, it opens a window which disappears immediately because the program ended in a fraction of a second.
Start cmd (Command Prompt) or PowerShell (either directly or through Windows Terminal), and run your commands in that interactive shell.
I have created a batch file which calls a java process in background. All I want is to redirect the logs of the java process to a 'NUL' output so that the background terminal does not contain any output.
Contents of my batch file:
#echo off
START /MIN java -jar ./myapp.jar >null 2>&1
However when I run the batch, I minimized windows terminal opens up which still displays all the logs.
I require to disable this logging, without changing the app.jar
If you just want to ignore any console input and output, execute your Java program on a Windows platform and need to prevent the opening of a console window, then use the javaw executable instead of java.
dotvav's solution is best in your scenario, but for the record the proximate problem with your batch script as posted is that the redirection applies to the START command, not to the java command.
A corrected version would look something like this:
START /MIN cmd /c "java -jar ./myapp.jar >NUL 2>&1"
I use Windows 8.1, I start program instance by command line. There is this line
java -jar "C:\Program Files (x86)\Jenkins\jenkins.war" --ajp13Port=-1 --httpPort=8082
Program starts and everything is all right. But i want to hide console from task menu. Is it possible to run program by command line in this way?
You could use
1) javaw - java without console window to start your application
or
2) start /b *command* - to start a background task
Why not using javaw instead of java?
im trying to do something simple, i want to start Burp Suite with extra java memory, but i don't want the CMD window to stay open.
If i don't use a .bat file and simple open cmd and type start /b "" java -jar -Xmx2g burpsuite_pro_v1.6.07.jar, Burp opens, the the process is sent to background, but the CMD window stays open. i can, however, close it manually and Burp will keep working.
when i try to put the thing into a CMD window, it will not even be sent to background, Burp stays dependent on the CMD, and i cant even add exit to the file.
i tried to solve the issue by following:
run bat file in background - this worked, but required me to have THREE files, i prefer a more elegant "1 file solution"
Just add the below line in your bat file and the java procees will run in background with no window open!
start javaw {Path of Your jar or Java file}
The following batch file commands should accomplish your purpose:
start "" /B java.exe -Xmx2g -jar burpsuite_pro_v1.6.07.jar
exit
You can probably even leave out the /B switch.
References
How can I run a program from a batch file without leaving the console open after the program start?
How to use the start command in a batch file?
Turns out the solution was simple: using javaw.
the issue was with using java.exe, some attempts even closed the CMD but opened a java.exe window (blank)
modding the file to contain:
start javaw -Xmx2g -jar burpsuite_pro_v1.6.07.jar solved it for me.
I have the following code to open a command prompt window Runtime.getRuntime().exec("cmd.exe /c start");, but I'm trying to open the prompt with a different location loaded into it.
The idea behind the program is to allow the user to choose one of 3 options to load the command prompt window into, and they're mounted on different locations for example two of them are X:/myJava/ and H:/publicJava/.
How can I open the command prompt window that is loaded into these folders once the user makes their choice?
Use this Runtime.getRuntime().exec("cmd.exe /c start", null, new File("X:/myJava"));
You can use making a batch file with commands like
x:
cd publicJava
and then execute that batch file using the Runtime.getRuntime().exec("myBatch.bat");
Try this
Runtime.getRuntime().exec("cmd.exe /c start /d d:\\java");
The start command accepts a path via the "/d" switch. Keep in mind that you have to escape the backwards slash in the path, hence the double slash.