My setup is as follows:
C: contains operating system and final release of programs
Z: contains code I am working on
I am using Netbeans, which is installed on C:\Program Files (x86)
My project folders are in Z:
I am trying to debug a project which needs to run a process where the file for the process directory is "C:\TaxiPIM"
I have tried:
ProcessBuilder pb = new ProcessBuilder("java", "-jar", "Pim_Update_Client.jar");
pb.directory(new File("/TaxiPIM"));
and:
ProcessBuilder pb = new ProcessBuilder("java", "-jar", "Pim_Update_Client.jar");
pb.directory(new File("c:/TaxiPIM"));
and ended up google-e-eyed with results explaining how to change the directory...
But I need to change the drive as well as the directory.
Thanks for reading - feedback is most appreciated!
Edit: ProcessBuilders directory(File) method returns a new ProcessBuilder so try pb=pb.directory(new File("...)
crude way would be to export the command to a batchfikle in the same dir as your project and putting the change drive code into the batch file, too and then run the batch file from your code.
Example that changes from a directory on C to a directory on D; (i have my NetBeans installation and the project-directory on the C-Drive)
ProcessBuilder pb = new ProcessBuilder("cmd.exe","/c","start","cmd");
pb=pb.directory(new File("D:\\src"));
pb.start();
And then after getting the new ProcessBuilder, just put in your command:
ProcessBuilder pb = new ProcessBuilder("cmd.exe","/c","start","cmd");
pb = pb.directory(new File("c:/TaxiPIM/"));
pb.command("java", "-jar", "Pim_Update_Client.jar");
Thanks again to #masterX244
Related
I've got a python file which opens a webpage with selenium.How can I run this file in java application ? I tried Runtime().exec("python main.py") and "python3 main.py" but it did't work.
Use ProcessBuilder.
String[] commands = { "python", "main.py" }; // Need to add a python or python3 to the system path
ProcessBuilder pb = new ProcessBuilder(commands);
pb.directory("Set the path of the main.py folder");
Process proc = pb.start();
I want a process to have write access to only one directory. If the process attempts to write to another directory, it should be restricted.
I tried directory() method, but this only sets default.
ProcessBuilder processBuilder = new ProcessBuilder();
File allowedDir = new File(System.getProperty("user.home") + "/allowedDirectory/");
// setting allowed directory
processBuilder.directory(allowedDir);
//expect this to fail
processBuilder.command("cmd.exe", "/c", "rmdir /s /q \"C:\\Users\\restrictedDirectory\\restrictedFolder\"");
Process process = processBuilder.start();
int errCode = process.waitFor();
However the command that I was expecting to fail, works and removes files in the restrictedDirectory. How to restrict ProcessBuilder to have write access to only a single directory?
That's not possible - ProcessBuilder simply executes programs on your computer, it doesn't run them in a sandbox.
ProcessBuilder#directory(File) sets the working directory for the process.
Im actually doing a Java Program that execute a shell script with this ProccesBuilder:
ProcessBuilder builder = newProcessBuilder("sh","script.sh",params);
builder.redirectOutput(new File("outNormal.txt"));
builder.redirectError(new File("outError.txt"));
Process p = builder.start();
p.waitFor();
The problem i have is that outNormal.txt and outError.txt, and another file my script creates are placed at the same level as /src and /bin, and i want to change this to a new directory.
is this possible?
I have a shell script in a different directory than my java files. This script contains only ls which prints the files in the current directory. When I run the java project it prints the files in the root of the java project not the root of shell script. I want it to print the files in the root of the shell script.
Java code:
ProcessBuilder pb = new ProcessBuilder("/home/omar/ros_ws/baxter3.sh");
Process p = pb.start();
BufferedReader reader = new BufferedReader(new InputStreamReader(p.getInputStream()));
String line = null;
while ((line = reader.readLine()) != null) {
System.out.println(line);
}
You then should set the working directory of your ProcessBuilder instance to the directory you want to watch. You can do is using its method directory(File directory).
See Javadoc of ProcessBuilder
so before pb.start(), define a File for your Directory and assign it to the instance of ProcessBuilder.
File myDir = new File("/home/omar/ros_ws");
pb.directory(myDir);
pb.start();
Use the ProcessBuilder.directory, you probably want
String path = "/home/omar/ros_ws/baxter3.sh";
ProcessBuilder pb = new ProcessBuilder(path);
pb.directory(new File(path).getParent());
A simple solution: The command ls can be run with multiple arguments. One of which is a file argument which can be a directory.
From the ls man page:
# List the contents of your home directory
$ ls ~
Thus, just pass your shell script the directory you have hard coded in your java code: "/home/omar/ros_ws/" and access it in your shell script via $1.
So your shell script will look something like:
#!/bin/bash
ls $1
And call your shell script from java via:
ProcessBuilder pb = new ProcessBuilder("/home/omar/ros_ws/baxter3.sh /home/omar/ros_ws/");
// other code omitted for brevity
I need to run an executable file from Java and have it open in a separate window. When I open the file without using Java, it works perfectly, but nothing seems to happen when I try to open it using Java code. I have tried using getRuntime and ProcessBuilder. The lines I used are below.
ProcessBuiler:
ProcessBuilder pb = new ProcessBuilder("C:\\Users\\Louis Windows\\workspace\\Py2exetest\\dist\\test.exe");
Process p = pb.start();
getRuntime:
Runtime.getRuntime().exec("C:\\Users\\Louis Windows\\workspace\\Py2exetest\\dist\\test.exe", null, new File("C:\\Users\\Louis Windows\\workspace\\Py2exetest\\dist\\"));
Both seemed to give the same result (nothing). Any ideas how I can make test.exe open in a new window?
On windows, you could try using cmd.exe to launch the process.
ProcessBuilder pb = new ProcessBuilder("cmd", "/c", "start", "C:\\Users\\Louis Windows\\workspace\\Py2exetest\\dist\\test.exe");
pb.start();