Running cmd with spaces from java - java

I want to run a command through java session. The command contains spaces. as
"C:\With Space\sample.exe" -command_option "C:\Source File\test.c"
This works if
C:\WithoutSpace\sample.exe -command_option "C:\Source File\test.c"
if we keep the quotes in C:\With Space\sample.exe we get error as :'The filename, directory name, or volume label syntax is incorrect.' and if we remove the quotes then the exe do not run...
please guide.
Thanks,

Try this:
String[] arg = {"cmd","/c","C:/Source File/test.c"};
ProcessBuilder pb = new ProcessBuilder(arg);
Process pr = pb.start();
Also, you can use Runtime.exec(String[]) version
Example:
Runtime rt = Runtime.getRuntime();
String[] args = { "cmd", "/c", "C:/Source File/test.c"};
try
{
Process proc = rt.exec(processCommand);
}

You can try this:
Process process = Runtime.getRuntime().exec("'C:\WithoutSpace\sample.exe' -command_option 'C:\Source File\test.c'");
BufferedReader in = new BufferedReader(new InputStreamReader(process.getInputStream()));
while ((line = in.readLine()) != null) {
//line contain command output
}

Related

how can i execute python script via terminal in Java

Im trying to run python script via terminal but it always throws an exception: No such file or directory
StringBuffer output = new StringBuffer();
String command = "python3 Users/lounah/Documents/programming/ApplicationName/scriptName.py " + params.toString();
ProcessBuilder processBuilder = new ProcessBuilder(command);
Process process = processBuilder.start();
BufferedReader reader =
new BufferedReader(new InputStreamReader(process.getInputStream()));
String line;
while ((line = reader.readLine())!= null) {
output.append(line + "\n");
}
When you pass a string in ProcessBuilder it tries to run a program located in that path.
Instaed you should use a String[] with the path of your executable ( '/python3/python.exe' or 'python' or 'py') followed by the path of your script, followed by the arguments.
String[] command = {
"python3",
"Users/lounah/Documents/programming/ApplicationName/scriptName.py",
params.toString()
};
ProcessBuilder processBuilder = new ProcessBuilder(command);

Shell Script to add Appium path on mac

Is there any possible way to use a shell script to add adb path and apppium path on mac? or Is there a java code to set path on mac?
I have seen a question related to this(here set windows PATH environment variable at runtime in Java) but it's for windows. someone can help me to work on mac
I am using this code but still no improvement. I can set windows environment variable not mac.
public static void testProcessBuilder(String path) throws IOException {
//export ANDROID_HOME=/usr/local/Cellar/android-sdk/24.3.3
//export PATH=$PATH:$ANDROID_HOME/tools:$ANDROID_HOME/platform-tools
String[] args = new String[] {"/bin/bash", "-c", "export "+"JAVA_HOME=/Library/Java/Home"};
ProcessBuilder processBuilder = new ProcessBuilder(args);
Process p = processBuilder.start();
String line;
BufferedReader r = new BufferedReader(new InputStreamReader(p.getInputStream()));
while ((line = r.readLine()) != null) {
System.out.println(line);
}
r.close();
}
String[] args = new String[] {"/bin/bash", "-c", "export "+"JAVA_HOME=/Library/Java/Home"};
replace the above line to this line
String[] args = new String[] {"/bin/bash", "-c","-l" "echo \"export JAVA_HOME=$(/usr/libexec/java_home)\" >> ~/.bash_profile;source ~/.bash_profile"};

java exec attempting to untar a file

I have been trying to untar a .tbz file without a lot of success in a java app. I have now decided to try and hit the command line to get the job done, and it currently doesn't through any errors but it doesn't untar the file, either. Can anyone see an issue with this?
String[] cmd = { "tar", "-xjf", "/var/tmp/filename.tbz"};
Process p =Runtime.getRuntime().exec(cmd, null);
EDIT, this works:
List<String> commands = new ArrayList<String>();
commands.add("tar");
commands.add("-xvjf");
commands.add("/var/tmp/filename.tbz");
ProcessBuilder pb = new ProcessBuilder(commands);
Process p = pb.start();
BufferedReader reader = new BufferedReader(new InputStreamReader(
p.getInputStream()));
String readline;
while ((readline = reader.readLine()) != null) {
System.out.println(readline);
}
What is a cd command doing there in the middle? Perhaps you meant this:
String[] cmd = { "tar", "-xjf", "/var/tmp/filename.tbz"};
If instead of the above, you really want to do this:
cd /var/tmp
tar -xjf filename.tbz
In this case you can use ProcessBuilder.

How to execute an exe in java on eclipse and pass intput through a variable and get output on console?

I have an exe file and i want to execute it for a large number of times passing a variable as an input and print the output for each case..
Runtime runtime = Runtime.getRuntime();
for(int i=0;i<1000;i++)
{
Process p = runtime.exec("cmd /c start C:/Users/sbm/workspace/Codex/a.exe",i);
BufferedReader input = new BufferedReader(new InputStreamReader(p.getInputStream()));
String line;
while ((line = input.readLine()) != null)
{
System.out.println(line);
}
}
Even if i get the output in a file it will be helpful.
You can do something like this
for(int i=0;i<1000;i++) {
ProcessBuilder builder = new ProcessBuilder("urcmd","urarg");
builder.redirectOutput(new File("C:\\output\\process"+i+".txt"));
builder.start();
}

Java runtime's .exec() wont open executable

I am trying to execute another file using Runtime and Process
try
{
Runtime run = Runtime.getRuntime();
Process pro = run.exec("C:\\Users\\user\\Desktop\\file.exe");
}
catch(Exception a)
{
a.printStackTrace();
}
I can enter this command in either run or cmd and am able to open the file but running it through my program it won't open. There are no errors, it just doesn't open.
To better understand what is going on (and it is actually a requirement of the Process class), you need to redirect the input and error streams of your process - and using a ProcessBuilder is the recommended way to start processes:
public static void main(String[] args) throws Exception {
ProcessBuilder pb = new ProcessBuilder("C:\\Users\\user\\Desktop\\file.exe");
runProcess(pb)
}
private static void runProcess(ProcessBuilder pb) throws IOException {
pb.redirectErrorStream(true);
Process p = pb.start();
BufferedReader reader = new BufferedReader(new InputStreamReader(p.getInputStream()));
String line;
while ((line = reader.readLine()) != null) {
System.out.println(line);
}
}
You must do
Process pro = run.exec("C:\\Users\\user\\Desktop\\file.exe",null,"C:\\Users\\user\\Desktop\\");
Please see Run .exe file from Java from file location
Try this way:
String []cmdarray = new String[4];
cmdarray[0] = "cmd";
cmdarray[1] = "/c";
cmdarray[2] = "start";
cmdarray[3] = "C:\\Users\\user\\Desktop\\file.exe";
Runtime.getRuntime().exec(cmdarray);
Try this one, create a batch file ,like start_file.bat.
The content like this:
cd C:\Users\user\Desktop ----- Goto this directory
C: ----- This line is very important
file.exe
Both the two approaches work well.
Runtime r = Runtime.getRuntime();
String []cmdarray = new String[4];
cmdarray[0] = "cmd";
cmdarray[1] = "/c";
cmdarray[2] = "start";
cmdarray[3] = "C:/users/desktop/start_file.bat";
r.exec(cmdarray);
And this one:
r.exec("C:/users/desktop/start_file.bat");
You can read the output from this new process.

Categories

Resources