Java commands flash a cmd - java

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.

Related

Open gnome-terminal in background from Java

I'm trying to open gnome-terminal to execute a command from Java by the following code:
Runtime.getRuntime().exec(new String[]{"gnome-terminal", "-e", "command"});
But this code will show terminal for user, how I can open gnome-terminal in background?
You don't need to open a terminal visually to execute a command. Shell interpreter is a program which you can invoke without the terminal window. What terminal window does is it just sends what you type on the terminal, interacts with the underlying shell interpreter and gives you the results. When you need to run a OS specific command you would want to directly call the shell interpreter you want to with(i.e bash, csh, ksh) and get the result from within the Java program.
Working with commands and reading output from them is not that easy sometimes. So I would suggest you to have a look at Apache Exec which will ease reading data from externally invoked program.

creating a bat file which sends to background and closes (1 file)

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.

It can't process the by clicking the spark-shell windows command script. What i do?

In the Process of installing spark 1.0.0 by double clicking the bin/spark-shell windows command script file. Then opened one command prompt file and then immediately closed it self only. Are there any commands required to run this. Could you please tell me step by step process.
First of all, you have to open a terminal. Theorically, you at least have the following on your machine :
cmd (for sure)
powershell (maybe not, if you're using Vista or less).
From there, you have two options :
if you added path_to_spark_folder\bin to your PATH variable (see there for more informations), you can run spark-shell as soon as the console is opened
if you didn't, you'll have to go to path_to_spark_folder\bin yourself, using the cd command.
You now can run spark-shell.

Executing .bat files using jenkins

I'm trying to run a jar file using a bat command with jenkins. and I want to popup the cmd and execute the jar file. but the problem is jenkins execute commands inside its console. Then i inserted "start" command and hoped it'll work coz it creates a separate cmd to run the jar.
here is my bat code
start "window_name" java -jar myjarfile.jar
but when i executing using it jenkins it doesn't create a separate cmd window but it executes the jar file anyhow. it shows this line,
C:\Update>start "window_name" java -jar myjarfile.jar
any idea how to solve this? I want to pop up a black window when executing.
The trick is figuring out in what session you want to start the cmd.exe. On a remote server (which is most often the case with Jenkins), it's not necessary straight forward. Your Remote Desktop Session is not in the same session as someone who is logged in physically at the console.
Bring up Windows Task Manager
Click the Users tab
Note down the ID of the session of the logged in user that you want
Download psexec from Windows Sysinternals
Edited from here downwards
Open elevated command prompt: type cmd into Start's quicksearch, right click cmd.exe, select Run as Administrator.
Type C:\path\to\psexec.exe -accepteula and press enter.
Type C:\path\to\psexec.exe -i 1 cmd and press enter. (If you see a command prompt appear, all is good, close it now)
In Job configuration, configure Execute Windows Batch command step
Write the following:
C:\path\to\psexec.exe -accepteula && C:\path\to\psexec.exe -i 1 cmd /c start C:\full\path\to\java.exe -jar myjarfile.jar
A more detailed explanation is provided in this answer Open Excel on Jenkins CI
Thanks Guys, May be your solutions too will do the trick. Finally what I did is i created a socket program and executed server myself. Then scheduled jenkins to execute the client.(Server in my environment & client in jenkin's environment) When client connects to the server it executes the bat file. Now everything works fine.

Running a tool in its own cmd.exe

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.

Categories

Resources