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

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" });

Related

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" });

How to restrict directory with ProcessBuilder?

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.

Running an EXE from Java

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();

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).

Java - Executing commands into a batch file

I have made a java script which uses the runtime.exec() to execute a batch file, and that works fine but when i get the output stream and use the write() function it does not execute the command i put into it.
Runtime runtime = Runtime.getRuntime();
Process p;
p = runtime.exec("cmd /c start batchfile.bat");
out = p.getOutputStream();
out.write("command".getBytes());
It displays the batch file but does not run the command, is there another way of entering a command into the cmd running the batch file so it displays it?
With the start command, a separate command window will be opened, and any output from the batch file will be displayed there. It should also work as just cmd /c build.bat, in which case you can read the output from the subprocess in Java if desired.
You're writing into an output stream. I think you mean to write to an input stream.
Try this:
Runtime runtime = Runtime.getRuntime();
Process p;
p = runtime.exec("cmd /c start batchfile.bat");
in = p.getInputStream();
in.write("command".getBytes());

Categories