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"};
Related
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);
How am I to execute a command in Java with parameters?
I've tried
Process p = Runtime.getRuntime().exec(new String[]{"php","/var/www/script.php -m 2"});
which doesn't work.
String[] options = new String[]{"option1", "option2"};
Runtime.getRuntime().exec("command", options);
This doesn't work as well, because the m parameter is not specified.
See if this works (sorry can't test it right now)
Runtime.getRuntime().exec(new String[]{"php","/var/www/script.php", "-m", "2"});
Use ProcessBuilder instead of Runtime#exec().
ProcessBuilder pb = new ProcessBuilder("php", "/var/www/script.php", "-m 2");
Process p = pb.start();
The following should work fine.
Process p = Runtime.getRuntime().exec("php /var/www/script.php -m 2");
Below is java code for executing python script with java.
ProcessBuilder:
First argument is path to virtual environment
Second argument is path to python file
Third argument is any argumrnt you want to pass to python script
public class JavaCode {
public static void main(String[] args) throws IOException {
String lines = null;
ProcessBuilder builder = new ProcessBuilder("/home/env-scrapping/bin/python",
"/home/Scrapping/script.py", "arg1");
Process process = builder.start();
BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream()));
while ((lines = reader.readLine())!=null) {
System.out.println("Line: " + lines);
}
}
}
First is virtual environment path
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.
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.
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
}