Open Command Prompt from Java - java

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.

Related

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.

How to create a shortcut to a particular command in command prompt?

Is it possible to create something like a desktop shortcut that I could double click to run a particular command that I would normally type out in command prompt? Here is one that I want to make a shortcut of:
C:\Users\jdave\Documents\Java>appletviewer ButtonExample.html
I asked this on Yahoo Answers because I thought it would be a little basic for Stack Overflow and one person said to create a batch file like so:
#echo off
start "C:\Users\jdave\Documents\Java>appletviewer ButtonExample.html"
I did this but all it does is open a command prompt window.
Your batch file should look something like this:
#echo off
cd C:\Windows\System32\
start notepad.exe C:\Users\"user1"\test.txt
Replace the "user1" with your username or give the proper path to your application.
Make sure that appletviewer takes the type of parameter that you are passing it.
You can just create a shortcut with
C:\Users\jdave\Documents\Java\appletviewer ButtonExample.html
You need to set java bin directory in environment variable path. Use following batch :
#echo off
cd "C:\Users\jdave\Documents\Java"
appletviewer GifExample.html
Your command doesn't look correct, try this:
C:\Users\jdave\Documents\Java\appletviewer GifExample.html
Ensure that appletviewer.exe exists in this location.
Right-click on Desktop > New > Shortcut. Enter Location
appletviewer GifExample.html
Click Next, enter name of shortcut, click Finish. You're done.
By the way, the > you see on the command prompt is not part of the command. The actual command starts after the > symbol.
Also, the 'appletviewer' program probably doesn't reside in C:\Users\jdave\Documents\Java. The above is correct if you can already run appletviewer from any folder in the command prompt. If you do C:\Users\jdave\Documents\Java\appletviewer GifExample.html it may not work at all!

Double Clicking JAR file does not open Command Prompt

I want to run a Jar file by double clicking it.
Following is the only Java class file present in it.
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.IOException;
public class Sysout{
public static void main(String[] args) throws IOException{
System.out.println("Hello World!");
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String msg = br.readLine();
System.out.println(msg);
br.read();
}
}
And Manifest file has Main-Class defined.
Using this link, I successfully ran the Jar file by double-clicking the batch file.
This opens the command prompt and runs the main class defined.
However, if I double click the Jar file directly, nothing happens.
I also checked this link and associated my .jar to javaw.exe
This link also suggests the same.
Also tried by associating the .jar with java.exe
What happens is the command prompt opens for a fraction of second and vanishes off.
Even if I am expecting the user to enter some data, double-clicking operation does not wait for the user to enter anything.
Where is the problem?
When you use the javaw association, it does not create a command window, and swallows all the System.out and System.err invocations.
You should reassociate your .jar file with the java binary, which should display the requisite command window.
If you used the simple Open With... option, it will have omitted the -jar option from the command line.
Open up an administrator command window (this is needed if you're using Vista or Windows 7 with UAC enabled) and do:
assoc .jar=jarfileterm
ftype jarfileterm="C:\Program Files\Java\jre7\bin\java.exe" -jar "%1" %*
In your case, you should replace the C:\Program Files\Java\jre7\bin\java.exe path with the one for your install of the jre.
When you double-click following this, then it should run correctly.
You can add another ftype:
ftype jarfile="C:\Program Files\Java\jre7\bin\javaw.exe" -jar "%1" %*
again substituting the path to the javaw binary with the one that's for your system.
You should now be able to toggle between windowed and non-windowed by alternately choosing assoc .jar=jarfileterm and assoc .jar=jarfile
If you want to keep the command window around after running the .jar, then you surround the calling of the java command with a cmd /s /k viz:
ftype jarfileterm=cmd /s /k ""C:\Program Files\Java\jre7\bin\java.exe" -jar "%1" %*"
assoc .jar=jarfileterm
If these commands worked, then double clicking on the jar file will cause a command window to pop-up and persist.
You cannot set a complex enough command line with either Open With... or using Default Programs that will allow the jar file to run. If you have successfully tried all these efforts ftype and assoc commands and it still doesn't work, then you will need to peel out the registry editor.
Launch regedit, and search for a key called .jar under HKEY_CLASSES_ROOT - this should result in a single value underneath it called (Default) with a value, if your ftype command invocations worked, then it should read jarfileterm. If it didn't work, then you're looking at an association that may have been created by another application (I don't know if the java updater replaces these entries, but if it does, then this could be the issue)
You need to next look for this key in the HKEY_CLASSES_ROOT. It will find this entry, which should contain the a key Shell (i.e. expand the folder jarfileterm and it should reveal another folder Shell), which contains a key Open which contains a key Command which contains a (Default) value that should contain the invocation command for launching .jar files. This command should match the last ftype jarfileterm=... entries that you typed in. If it doesn't then you should make it match one of the cmd /s /k or "c:\program files\java\jre7\bin\java.exe" options (depending on if you want to persist the command window in the event of an error in launching or not)
May be your .jar file has binded with any other default program, Right click and open with 'Java(TM) Platform SE binary'. This should work if have a executable jar file.
I am using the JDK to open the jar file in Windows 10.
Open regedit → HKEY_CLASSES_ROOT\jarfile\shell\open\command
Change the default from javaw to java
For example, mine is "C:\Program Files\Java\jdk-14.0.1\bin\java.exe" "-jar" "%1"

Starting a blank console

IS there any way to start a blank console in Windows platform ?
I'm writing a CLI where i want to open a separate window where user can log in and write their own command. When executing with cmd /c start command, it starts windows standard console.
Is there any other command ???
Assuming you're trying to start a java jar file, use a command like this:
start /d "%~dp0" java -jar "%~dp0\fpa.jar"
%~dp0 expands to the drive letter and path in which the batch file is located. Use that if you want to want to make sure that the PWD when running is the same location as the batch file. Otherwise, juse use
start java -jar "%~dp0\fpa.jar"
This will make sure that the batch file works even if you run it when not in the same directory as the jar file, as long as the jar file is in the same directory as the batch file.
You may need to make sure that java is in your path by having a line like
set path=jre6\bin;%PATH%
Also, you can eliminate the command line windows that comes up (for a GUI program by example) by using javaw instead of java.
You can use batch file in windows which would in turn start application that would accept user input. Something similar to this :
start cmd /c myapp.bat

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