I want to open a console window and write any command I want, from a string which varies depending on what I input. The problem is that no commands with a / or \ work because of the last / in the CMDs parameters.
I've tried removing and changing the parameters, but the CMD won't run correctly then.
Runtime.getRuntime().exec("cmd /c start cmd.exe /K" + command + "\"");
I Want to be able to run any command (From the command string), even commands like cd C:\ which contains a / or \
Commands containing slash or backslash work just fine in your code, with or without the extra \" at the end, e.g. c:\windows\system32\winver or c:/windows/system32/winver
C:\ doesn't work because that isn't a valid CMD command, which if not a builtin (like cd or set or if) or drive: (or :label) must be a filename (either a pathname, or a simple name which is searched on %PATH%) which is a program (.exe or on older versions .com) or is associated to a program or is a .bat file. If you run CMD interactively and enter C:\ it fails, and passing the same thing from Java can't make it work any better.
It is a valid 'command' in the Search-formerly-Run box, or the start command. Those, deliberately, accept some things that are not programs, and figure out how to 'open' them -- in particular they 'open' a directory name like C:\ by running Explorer. Using your code to run explorer c:\ does work.
Related
I've been trying to create an installer for a program I wrote in Java, implementing GhostScript, that creates a virtual printer where the file is sent to. My program then reads the files and manages it accordingly. However, I had to manually configure the RedMon Redirect Port (RPT1:) and manually created a new printer, using that port, taking as arguments the .jar file:
Arguments configured on the printer's port:
I was able to create a new printer via NSIS (the program I'm using to create the installer) like so:
Exec 'cscript "C:\Windows\System32\Printing_Admin_Scripts\pt-BR\prnport.vbs" ^ -a -r RPT1:'
ExecWait 'rundll32 printui.dll,PrintUIEntry /if /f "$INSTDIR\ghostpdf.inf"^ /r "RPT1:" /m "Ghostscript PDF" /b "Impressora SPE" /u /Y'
Is there a way to do the same, via commands, for configuring ports? If possible, I'd like to create a separate port for my program (currently I'm using the default RedMon port), but also pass the Java arguments to it on installation, so the end user doesn't have to manually include these arguments. I've looked everywhere, but every guide and article I found had it done manually.
Thanks for any help!
It took me a bit of digging, but I finally found the command required to setup a new printer port using RedMon, and figured I'd share it here so less people have a hard time finding it.
rundll32 "REDMONPATH\redmon32.dll", RedMonConfigurePort Port="PORT:" Command="JAVA" Arguments="-jar ""JAR"" ""C:\Program Files (x86)\gs\gs9.21\bin\gswin32c -I\""C:\Program Files (x86)\gs\gs9.21\lib\"" -dSAFER -dBATCH -dNOPAUSE -sDEVICE=pdfwrite -sPAPERSIZE=a4 -r300"""
Where REDMONPATH is the path to your RedMon installation folder, PORT is your port's name, JAVA is your path to the java.exe, and JAR is the path to your .jar file. DO keep the double quotes, it's how we pass quotes via commands. You must run this command on the RedMon installation folder, though. Also, you MUST run as an administrator, otherwise you might get an error and wrongly assume you did something wrong. In my case, using NSIS, this is the full command line:
ExecWait 'rundll32 "$INSTDIR\redmon\redmon32.dll", RedMonConfigurePort Port="SPE:" Command="C:\Program Files (x86)\Java\jre1.8.0_144\bin\java.exe" Arguments="-jar ""C:\Program Files (x86)\Impressora SPE\ImpressoraSPE.jar"" \""C:\Program Files (x86)\gs\gs9.21\bin\gswin32c -l \""C:\Program Files (x86)\gs\gs9.21\lib\"" -dSAFER -dBATCH -dNOPAUSE -sDEVICE=pdfwrite -sPAPERSIZE=a4 -r300"""'
After that, you can install your new printer using the new port normally:
ExecWait 'rundll32 printui.dll,PrintUIEntry /if /f "$INSTDIR\ghostpdf.inf" /r "PORT:" /m "Ghostscript PDF" /b "PRINTER_NAME" /u /Y'`
If you're trying to run this from the command prompt and not NSIS, all you gotta do is remove the "ExecWait" bit and the single quotes around the command. Also replace "$INSTDIR\something" by the corresponding full path.
If anyone else needs further help with this subject, seeing that I personally had quite a hard time with it, feel free to ask them here and I'll try my best to help!
EDIT: I apologize for the constant edits, just making sure to leave the instructions that help the most people as I test these in different version of Windows.
I am running a batch (ScanProject.bat) file using java by following code
Process p= Runtime.getRuntime().exec("cmd /c start /wait ScanProject.bat "+ BaseProjDir+"\\"+jo.getString("Name")+" "+st.nextToken());
System.out.println("Exit value : "+p.waitFor());
And following is the batch file code :-
%2:
cd %1
ant -f ..\antbuild.xml analyse
exit
Batch file run successfully but problem is command prompt window do not closes automatically and hence Process do not terminated automatically and my program wait for infinite time to complete this process.Please suggest me any technique so that cmd exit after running ant -f ..\antbuild.xml analyse command.
Thanks.
cd /D "Full path of directory" or pushd "Full path of directory" with popd before exit is better to switch the current directory to any directory on any drive (cd and pushd/popd) or even to a network share (just pushd/popd). Run in a command prompt window cd /? and pushd /? for details.
cmd /C starts a new Windows command process with closing the process automatically after last command was executed. Run in a command prompt window cmd /? for details on options of Windows command interpreter.
start is a command to start a new Windows command process or a GUI/hybrid application in a separate process.
So what you do here is starting a new Windows command process which starts a new Windows command process.
Running in a command prompt window start /? outputs the help for this command. start interprets often the first double quoted string as title string for the new command process. This causes often troubles on command lines with at least 1 double quoted string. Therefore usage of start requires often an explicit definition of a title string in double quotes as first argument for start which can be even an empty string, i.e. simply "" as first argument after start.
As it can be read after running exit /? in a command prompt window, this command without /B always exits the current Windows command process immediately. So when ant.exe finished, the command process in which the batch file was processed is definitely terminated.
I'm having no experience on Java development, but in my point of view it should be enough to use the following execution command which does not need a batch file at all.
The Java code line
Process p= Runtime.getRuntime().exec("cmd.exe /C cd /D \"" + jo.getString("Name") + "\" && ant.exe -f ..\\antbuild.xml analyse");
should be enough to
start a new Windows command process,
set the current directory within this command process to the drive and directory specified by jo.getString("Name") which of course must return a directory path with drive letter and using backslashes as directory separators, and on success
execute ant in this directory with the specified parameters
with terminating the Windows command process automatically after console application ant.exe finished if ant.exe is a console application.
I'm not sure if cmd.exe /C is needed at all.
I suggest to test this command first manually from within a command prompt window. Then use it in the Java application if really working and producing the expected result. And finally I would further test if cmd.exe /C is needed at all in Java code.
See Single line with multiple commands using Windows batch file for details about the operator && to run a command after previous command was successful. And see also Why do not all started applications save the wanted information in the text files as expected? for an explanation of console / GUI / hybrid application.
NOTE: There is also Java Runtime method exec(String[] cmdarray, String[] envp, File dir) to execute a command like ant.exe with its parameters -f and ..\antbuild.xml and analyse in the directory defined with third parameter which might be better for this task.
Swap out exit for taskkill, assuming you do not have any other cmd processes running. Not very graceful but it will get the job done.
%2:
cd %1
ant -f ..\antbuild.xml analyse
taskkill /im cmd.exe
I'm trying to run a script from an Amazon Linux machine. The script invokes checkstyle like this (in a script called eval.sh):
CHECKSTYLE="java -jar /home/ec2-user/grader/ext/checkstyle-6.15-all.jar"
CHECKSTYLE_RULES="/home/ec2-user/grader/config/checks.xml"
CHECKSTYLE_OUT="quality.log"
"${CHECKSTYLE}" -c "${CHECKSTYLE_RULES}" -f xml -o "${CHECKSTYLE_OUT}" $(find "${_toCheck}" -name "*.java") 2>"quality.err"
When I run this, I get the following error in quality.err:
./grader/eval.sh: line 10: java -jar /home/ec2-user/grader/ext/checkstyle-6.15-all.jar: No such file or directory
I have tried to run the same command directly in the terminal and it is working. Both checkstyle-6.15-all.jar and checks.xml are where they should be.
What could cause this problem?
Change "${CHECKSTYLE}" to ${CHECKSTYLE} (without the quotes).
You are passing the entire value of the CHECKSTYLE variable as a single word (that's what the quotes do), so the shell is looking for a relative directory named java -jar, and is trying to find a file under that (nonexistent) directory with the path home/ec2-user/grader/ext/checkstyle-6.15-all.jar.
When you envoke "${CHECKSTYLE}" the shell thinks that is the command you are running. There is no such file name with the spaces and options have you have included there. If you envoke it simply as ${CHECKSTYLE} (drop the quotes) the shell will process it for whitespace as normal and split it into the appropriate pieces for creating the process.
I'm using Apple's automator to create a Shell Script. I can get it to run if I change directory specifically to where the jar file is. But what if I want to change to directory to wherever the Shell script happens to be running?
Right now I have the following, which works:
cd desktop/CommonDenom/
java -XstartOnFirstThread -jar CommonDenom.jar
I know there's a way to target whatever directory the Shell script is launched from, but I can't seem to get anything to work. Please be specific with instructions as I havent been using Automator very long. Unless someone can specify how ot writ ethe script without Automator. Thanks in advance.
A standard idiom for this in shell scripts is dirname $0. The $0 variable is the path to the script that was executed, and the dirname command takes a path and strips off the last component to leave the path to the containing directory
cd "`dirname $0`"
Just wanted to weigh in here. I've seen some people with this problem. If you are JUST on OSX and making your own scripts. This will do the trick :) kind of a hack, but works like a charm.
#! /bin/bash
sudo /Applications/XAMPP/xamppfiles/xampp startapache
open /Applications/XAMPP/htdocs
#!/bin/bash
DIR=$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )
cd "${DIR}"
...the rest of your script...
Credits to Ian C. on AskDifferent: https://apple.stackexchange.com/a/179064
I am not sure you mean "you want to change directory to wherever the script is run from" because you will be in the directory the script is run from when you start anyway, so there will be no need to change directory there. I think you mean you want the script to stay wherever it starts, yet still be able to find the jar file.
In which case, you probably jus need the following without any changing directory:
java -XstartOnFirstThread -jar ~/Desktop/CommonDemon/CommonDemon.jar
Uff... tried to google but with no result.
Hello everybody. I need to run via cmd.exe the next command from java programm (javascript syntax):
"/c cd c:\prb && Processing.bat c:\prb ext.dat auto"
it means i need to change current directory to c:\prb, write "Processing.bat c:\prb ext.dat auto" and press enter.
My Java code is:
String command = "cmd /c start cmd.exe /K cd c:\\prb Processing.bat c:\\prb prb ext.dat auto";
Process pr = Runtime.getRuntime().exec(command);
pr.waitFor();
but it doesn't work. I suppose that i miss some code between "c:\prb" AND "Processing.bat" in the command line.
Could anybody help me?
I can see at least one mistake: path c:\\prb Processing.bat contains space and therefore must be quoted:
"cmd /c start cmd.exe /K cd \"c:\\prb Processing.bat\" c:\\prb prb ext.dat auto"
I am not sure about c:\\prb prb ext.dat. Is it one path or 2 separate arguments? If it is one argument quote it too.
This time I will write a cope from my memory, I have used like 8 year ago :)
What is you trying to solve it is impossible, no that isn't the proper word, but: you want to launch cmd with a specified working directory, than launch a bat file.
Far as I remember:
Launch the .bat, but give a parameter other working directory
Or:
launch cmd (1 process)
give a param to execute the change directory
give another param to execute the bat file. All from Java, it is more complex.
How to do it it is another question.
here is what should be the command like if you do no specify the working directory:
String command = "cmd /c \"c:\prb\Processing.bat\"";
do not forget to change the working directory if you need. 8 years ago I did, sorry if I am robbing your time, just want to help