I'm having problem separating two string variables as arguments for a bash script. My bash script takes two arguments like so:
#!/bin/bash
DATA1=$1
DATA2=$2
However, DATA1 and DATA2 are not what I intended, because bash is reading my strings wrongly. DATA2 is only the first part of the argument I wanted to put in, because I'm having trouble making a complicated(relatively) string into a single arguemnt.
I call this script from a Java app with variables like this:
String m1 = "some data";
String m2 = "some more"+mystring+"even more data"+anotherstring;
myscript.sh m1 m2
Only the first part of m2 is passed to DATA1 in the bash script. I've tried wrapping the whole thing in single quotes and double quotes I can't get it to accept the whole of m2 as the second argument.
Any help in creating two string arguments is appreciated.
It works for me, see:
Runtime.getRuntime().exec(new String[]{"/bin/sh", "/tmp/myscript.sh", m1,m2});
Your code calls:Runtime.getRuntime().exec(String)
It calls method exec(command,null,null) (see source code in src.zip) exec(String command, String[] envp, File dir) .
Method exec(String command, String[] envp, File dir) parse command by StringTokenizer. So text "some data" or "\"some data\"" is split to some data (or "some data ")
Related
I have a java code that execute a powershell script.My parameters are in a string array that I got from user.
String sentence = clientinp.readUTF();
String[] parts = sentence.split(",");
How should I put the parameters to the script every time I execute the code?
I tried this code:
String command = "powershell.exe $Add-DnsServerResourceRecordA -ZoneName -Name -IPv4Address -TimeToLive";
But I don't know how can I pass this array to powershell.What should I do?
Use a ProcessBuilder. You have to put each parameter (including path to the program) as items to an array or list and pass it to the constructor of ProcessBuilder.
for example:
String[] arguments = {"powershell.exe", "$Add-DnsServerResourceRecordA", "-ZoneName", "[your zone name]", "-Name", "[your name]", "-IPv4Address", "[your ipv4 address]", "-TimeToLive", "[your TTL]"};
ProcessBuilder processBuilder = new ProcessBuilder(arguments);
Process process = processBuilder.start();
As an alternative you can use Runtime.getRuntime().exec()
I want to use Runtime.getRuntime().exec(String[] cmdarray, String[] envy, File dir),because I use relative direction in my python program. When I write this code; it doesn't work and no error report is shown.
Process proc1=Runtime.getRuntime().exec("python /Users/g/Documents/Project/fyp/Q/__init__.py 453454336",,/Users/g/Documents/Project/fyp/Q/)
proc1.waitFor();
I use java in my mac. python version is 2.7.
Actually what I want to do in Terminal is:
cd /Users/g/Documents/Project/fyp/Q/
python __init__.py
Seems as if you are passing some arguments to your .py file
simply use
String command = "python /Users/g/Documents/Project/fyp/Q/__init__.py 453454336"
Runtime.getRuntime().exec(command)
Make sure you pass null to the environment variables array and third parameter is actually a File object.
Use following code...!
File directory = new File("/Users/g/Documents/Project/fyp/Q/");
Process proc1=Runtime.getRuntime().exec("python /Users/g/Documents/Project/fyp/Q/__init__.py 453454336",null,directory)
I am trying to pass filenames with spaces to pdflatex inside a java application using Runtime.exec().
This command executes well on the terminal
pdflatex --halt-on-error "/home/jody/test 1.tex"
i.e. a pdf is generated, and the return code is 0.
The same command will not work (i.e. return code 1 instead of 0; no pdf created) when passed to exec() like this
myRuntime.exec("pdflatex --halt-on-error \"/home/jody/test 1.tex\"", null, null);
How do i have to write such a command to get it to work?
Thanks
Jody
I found the solution:
Instead of using
public Process exec(String command,
String[] envp,
File dir)
where the entire call is contained in the string 'command',
i now use
public Process exec(String[] cmdarray,
String[] envp,
File dir)
where the call is split up into an array 'cmdarray' containing the command (i.e. "pdflatex") and its arguments as elements. That way pdflatex understands the space-infested file name as single word, and can complete its job.
I am trying to create a java program that takes some user-input variables and passes them to a perl script (it actually finds a certain string within the perl script and replaces it with the user-input variables). Here is the code:
String sedMain = "sed -e ";
String sedFirstLine = "'s/AAA/"+newFirstLine+"/' -e ";
String sedNewCntr = "'s/BBB/"+newCntr+"/' -e ";
String sedNewSpacing = "'s/SPACE/"+newSpacing+"/' -e ";
String sedNewDmax = "'s/MAX/"+newDmax+"/'";
String sedFile = " /filepath/myperlscript.pl > /filepath/myNEWperlscript.pl";
String sedCommand=sedMain+sedFirstLine+sedNewCntr+sedNewSpacing+sedNewDmax+sedFile;
System.out.println("SED COMMAND: "+sedCommand);
String testRun = "touch /filepath/hello.txt";
Process runSedCommand;
runSedCommand = Runtime.getRuntime().exec(sedCommand);
I am using an IDE, and when the sed command is printed to the console, it looks correct. I copied the sed command from the console and ran it from the terminal, and it worked. I wrote the string "testRun" to see if there was a problem with the Process in Java, and it created the file "hello.txt". For some reason though, my program is not creating the output perl file "myNEWperlscript.pl". I am very confused as to why this is not working. Can anyone help out?
exec() takes a String[] with the program name and paramaters as its elements, but you are concatenating everything together into a single String and so effectively loosing the arguments.
Try something like this:
String[] cmd = {"sed", "first argument", "second argument"};
Runtime.getRuntime().exec(cmd);
use the
exec(String[] cmdarray)
signature.
the command is sed, -e is a parameter, 's/AAA/\n/' is another parameter and so on. So you will have
String[] command = new String[] {"sed", "-e", "s/AAA/\n/", "next parameter without single quotes", , "next parameter without quotes..."}
Runtime.getRuntime.exec(command);
This is the only way your parameters will get well formatted on it's way to the shell, otherwise weird sruff can happen as any quotes after the first token on the string will be considered to be just one parameter and so quotes can be escaped and things like that
I am using Runtime.getRuntime().exec() to run a shell script from Java code. The code works fine when I pass the parameter as string
Runtime.getRuntime().exec("sh test.sh")
Since I have to pass additional arguments which are paths with spaces, so I replaced String with String array.
String[] cmd = {"sh test.sh", "/Path/to my/resource file"};
Runtime.getRuntime().exec(cmd)
I also tried with
String[] cmd = {"sh test.sh"};
Runtime.getRuntime().exec(cmd)
But neither of them worked. It's throwing an exception:
java.io.IOException: Cannot run program "sh test.sh":
java.io.IOException: error=2, No such file or directory
Why is the same script file when passed as String worked and when used with String array is throwing exception? How can I make this work with string array as argument to Runtime.exec()?
First string became the command. There is no file 'sh test.sh' to be executed.
Change
String[] cmd = {"sh test.sh", "/Path/to my/resource file"};
to
String[] cmd = {"sh", "test.sh", "/Path/to my/resource file"};
(In general use process builder API)