I am trying to start a Lisp Image from Java in Mac OSX. Using the Image from my console I type the following:
lisp_image --eval '(package::method "some_argument")'
everything runs fine.
In Java I have the problem to pass the quotes and double quotes using the Runtime.getRuntime().exec("lisp_image --eval '(package::method \"some_argument\")'").
I also tried to use :
Runtime.getRuntime().exec(new String[] {"lisp_image", "--eval ", "\'(package::method ",
"--eval ", "\"", "some_argument", "\")", "\'"});
and various things with escaping using the backslash. Nothing works.... Using String Array seems to work only for Unix (or Windows) commands.
Any ideas?
Thanks in advance,
Sven
As I understand it you want to invoke the list_image with two arguments, --eval and '(package::method \"some_argument\")' where the single quotes is just there to prevent the shell from breaking it up into multiple arguments.
Then you should use
Runtime.getRuntime().exec(new String[] {"lisp_image", "--eval", "(package::method \"some_argument\")"});
Related
So, I pretty much have the below code to write some text to a file using ProcessBuilder:
val requestStringBody = "{\"transfers\": [{\"amount\": 100, \"name\": \"Steve Rogers\", \"taxId\": \"330.731.970-10\",\"bankCode\": \"001\",\"branchCode\": \"1234\",\"accountNumber\": \"123456-0\"}]}"
ProcessBuilder("cmd", "/c", ".\\git-bash.lnk", "-c", "echo -n \"$requestStringBody\" >> message.txt")
.directory(File(My Path))
.start()
.waitFor()
I had to use the git-bash.lnk due to some windows issues.
I've tried the same command using a different variable as the requestBodyString and it works fine, which leads me to believe that that string has an issue with it, but I just cant figured out what.
EDIT: Solved it using File(fileName).writeText(fileContent).
Your actual problem probably comes from the fact that the string you are embedding in your command line contains double quotes and that messes up quoting (also quoting on Windows command line is a mess).
That being said, this looks like a very convoluted way to write text to a file... as #VGR wrote, you would be better off just using regular APIs to write to files in Java.
For instance using Kotlin 1.5's Path.writeText:
val requestStringBody = "{\"transfers\": [{\"amount\": 100, \"name\": \"Steve Rogers\", \"taxId\": \"330.731.970-10\",\"bankCode\": \"001\",\"branchCode\": \"1234\",\"accountNumber\": \"123456-0\"}]}"
Path("message.txt").writeText(requestStringBody)
Or with older versions:
File("message.txt").writeText(requestStringBody)
My groovy script calls other commands via vagrant. One of those commands is to echo some quotes on a file within docker.
The goal is, so that within the container, i want to have BB_GENERATE_MIRROR_TARBALLS = "1". Now to do this on a bash script, i would need something like this:
BB_GENERATE_MIRROR_TARBALLS = \"1\"
The issue manifests itself when i have to escape double quotations on the groovy as well.
If i call vagrant("echo BB_GENERATE_MIRROR_TARBALLS = \\\"1\\\" >> ${yoctoDir}/build/conf/local.conf" on my groovy file, the outcome on the local.conf will be BB_GENERATE_MIRROR_TARBALLS=1 (without quotes).
The correct way to do this would be to include an extra backslash on both sides (3 for the groovy, 1 for the bash script), however when i do that, groovy doesnt run and gives me syntax errors.
What would be the correct way to insert this literal string(BB_GENERATE_MIRROR_TARBALLS=\"1\") on the groovy?
In groovy you can do the following:
def my_var = /BB_GENERATE_MIRROR_TARBALLS = "1"/
echo my_var >> ${yoctoDir}/build/conf/local.conf
I have a variable toPath (contains path like C:/Program Files(x86)/bla).
This variable I pass as agrument: '[-operation update -contents ' + toPath + ']'
But because I have a space in this variable I get IllegalArgumentException.
How can I fix this?
I'm not sure but it looks like you are trying to do a typical newcomer mistake.
If you are trying to run a command that is build from multiple variables you can be vulnerable to injection attacks. To prevent this, use the subprocess module and hand in all parameters as a list. The module will take care of all the stuff to make it work with spaces as well.
For example ls -l should be run as:
subprocess.call(["ls", "-l"])
Your example caontains [] and might be rather different but without it would be:
subprocess.call(['-operation','update', '-contents', toPath])
Please note that there are other functions than call() (which returns the return code only) in the subprocess module.
Pass argument in double quotes.
toPath = "\"C:/Program Files(x86)/bla\"";
try
'[-operation update -contents "' + toPath + '"]'
I know that by using the command in the terminal
date --set="2011-12-07 01:20:15.962"
you would actually be able to change the System clock, so I tried it in Java and came up with the following statement
Process p = Runtime.getRuntime().exec("date --set=\"2011-12-07 01:20:15.962\"");
but it was not able to set the clock.
Do you have any idea guys how it may be able work?
Premise:
The machine is Slackware,
The privilege is root level
There are two problems with this line of code:
Process p = Runtime.getRuntime().exec("date --set=\"2011-12-07 01:20:15.962\"");
You did not wait for the process to complete (see also http://docs.oracle.com/javase/6/docs/api/java/lang/Process.html#waitFor())
Parameters should be separated from program name, try this:
"date", "-s", "2011-12-07 01:20:15.962"
Alternatively, invoke shell as the process, and pass in a line of code:
.exec("sh", "-c", "date --set=\"2011-12-07 01:20:15.962\"")
Process p=Runtime.getRuntime().exec(new String[]{"date","--set","2011-12-07 01:20:15.962"});
The above statement worked like magic. #Howard Gou was right with "Parameters should be separated from program name"
The parts of the command statement should be passed by using a String array.
I need to build the following command using ProcessBuilder:
"C:\Program Files\USBDeview\USBDeview.exe" /enable "My USB Device"
I tried with the following code:
ArrayList<String> test = new ArrayList<String>();
test.add("\"C:\\Program Files\\USBDeview\\USBDeview.exe\"");
test.add("/enable \"My USB Device\"");
ProcessBuilder processBuilder = new ProcessBuilder(test);
processBuilder.start().waitFor();
However, this passes the following to the system (verified using Sysinternals Process Monitor)
"C:\Program Files\USBDeview\USBDeview.exe" "/enable "My USB Device""
Note the quote before /enable and the two quotes after Device. I need to get rid of those extra quotes because they make the invocation fail. Does anyone know how to do this?
Joachim is correct, but his answer is insufficient when your process expects unified arguments as below:
myProcess.exe /myParameter="my value"
As seen by stefan, ProcessBuilder will see spaces in your argument and wrap it in quotes, like this:
myProcess.exe "/myParameter="my value""
Breaking up the parameter values as Joachim recommends will result in a space between /myparameter= and "my value", which will not work for this type of parameter:
myProcess.exe /myParameter= "my value"
According to Sun, in their infinite wisdom, it is not a bug and double quotes can be escaped to achieve the desired behavior.
So to finally answer stefan's question, this is an alternative that SHOULD work, if the process you are calling does things correctly:
ArrayList<String> test = new ArrayList<String>();
test.add("\"C:\\Program Files\\USBDeview\\USBDeview.exe\"");
test.add("/enable \\\"My USB Device\\\"");
This should give you the command "C:\Program Files\USBDeview\USBDeview.exe" "/enable \"My USB Device\"", which may do the trick; YMMV.
As far as I understand, since ProcessBuilder has no idea how parameters are to be passed to the command, you'll need to pass the parameters separately to ProcessBuilder;
ArrayList<String> test = new ArrayList<String>();
test.add("\"C:\\Program Files\\USBDeview\\USBDeview.exe\"");
test.add("/enable");
test.add("\"My USB Device\"");
First, you need to split up the arguments yourself - ProcessBuilder doesn't do that for you - and second you don't need to put escaped quotes around the argument values.
ArrayList<String> test = new ArrayList<String>();
test.add("C:\\Program Files\\USBDeview\\USBDeview.exe");
test.add("/enable");
test.add("My USB Device");
The quotes are necessary on the command line in order to tell the cmd parser how to break up the words into arguments, but ProcessBuilder doesn't need them because it's already been given the arguments pre-split.
I wasn't able to get it to work in any of the above ways. I ended up writing the command to a separate script (with "\ " for each space) and writing that into a script file, then calling the script file.
Split the arguments and add it to the command list. The ProcessBuilder will append quotes to the argument if it contains space in it.
ArrayList<String> cmd= new ArrayList<String>();
cmd.add("C:\\Program Files\\USBDeview\\USBDeview.exe");
cmd.add("/enable");
cmd.add("My USB Device");