How to restrict directory with ProcessBuilder? - java

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.

Related

Error when creating zstd file with ProcessBuilder on Java

I'm trying to use ProcessBuilder in order to create a tar.zst file that compresses a folder. However, I'm getting the following error after running this code:
try {
ProcessBuilder pb = new ProcessBuilder("tar","--zstd","-cf","info-x.tar.zst","tstpkg");
pb.redirectOutput(ProcessBuilder.Redirect.INHERIT);
pb.redirectError(ProcessBuilder.Redirect.INHERIT);
Process process = pb.start();
process.waitFor();
} catch (Exception e){
e.printStackTrace();
}
Error:
tar: Can't launch external program: zstd --no-check -3
And the tar file is created, but is empty.
If I run the same command from the terminal
tar --zstd -cf info-x.tar.zst tstpkg
then it works fine.
When you find a command that works "from the terminal" but not from Java then you should assume that the PATH or other environment required with that command is only set correctly via that terminal, and not when called by JVM.
So, one possible fix is to run your terminal indirectly which may fix the environment needed - depending on your shell and it's setup:
ProcessBuilder pb = new ProcessBuilder("bash", "-c", "tar --zstd -cf info-x.tar.zst tstpkg");
You might also try to fix by setting the correct PATH for running zstd as a sub-process of tar. Check where it is using which zstd and try adding to the PATH before pb.start():
String path = pb.environment().get("PATH"); // use Path on Windows
pb.environment().put("PATH", "/path/to/dir/ofzstd"+File.pathSeparator+path);

Can't save mysql dump file to my hard drive from a java progam [duplicate]

Process p = Runtime.getRuntime().exec("sh somescript.sh &> out.txt");
I am running this command using Java. The script is running but it's not redirecting its stream to the file. Moreover, the file out.txt is not getting created.
This script runs fine if I run it on shell.
Any ideas?
You need to use ProcessBuilder to redirect.
ProcessBuilder builder = new ProcessBuilder("sh", "somescript.sh");
builder.redirectOutput(new File("out.txt"));
builder.redirectError(new File("out.txt"));
Process p = builder.start(); // may throw IOException
When you run a command, there is no shell running and any shell commands or functions are not available. To use something like &> you need a shell. You have one but you are not passing it to it. try instead.
Runtime.getRuntime().exec(new String[] { "sh", "somescript.sh &> out.txt" });

change workspace of a ProcessBuilder Java

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?

save the output of command prompt to a file [duplicate]

Process p = Runtime.getRuntime().exec("sh somescript.sh &> out.txt");
I am running this command using Java. The script is running but it's not redirecting its stream to the file. Moreover, the file out.txt is not getting created.
This script runs fine if I run it on shell.
Any ideas?
You need to use ProcessBuilder to redirect.
ProcessBuilder builder = new ProcessBuilder("sh", "somescript.sh");
builder.redirectOutput(new File("out.txt"));
builder.redirectError(new File("out.txt"));
Process p = builder.start(); // may throw IOException
When you run a command, there is no shell running and any shell commands or functions are not available. To use something like &> you need a shell. You have one but you are not passing it to it. try instead.
Runtime.getRuntime().exec(new String[] { "sh", "somescript.sh &> out.txt" });

Calling python script from Java using runtime.getruntime.exec

I have a java web development project, and want to call a python script to run in the background and then carry on with the java.
String command = "cmd.exe /c cd "C:\\path\\to\\py\\" && python script.py";
Process p = Runtime.getRuntime().exec(command);
Nothing seems to happen when i call this, but i need to change directory first as the script accesses files in its directory.
Thanks for your help
Edit:
Correct answer was adding start, this is my edited code
String command = "cmd.exe /c cd "C:\\path\\to\\py\\" && start python script.py";
Process p = Runtime.getRuntime().exec(command);
Rather than using cmd to change the directory, you can set a process's working directory from the Java side. For example
ProcessBuilder pb = new ProcessBuilder("python", "script.py");
pb.directory(new File("C:\\path\\to\\py"));
Process p = pb.start();
Did you configure your environment to support "executable" python scripts?
If not, you should call it like this:
String command = "cmd.exe /c start python path\to\script\script.py";
Process p = Runtime.getRuntime().exec(command);
The start command runs the appropriate executable (in this case python interpreter), with its supplied arguments (in this case the script itself).

Categories