FAQ Database Discussion Community


Running CMD from Java

java,python,cmd,runtime.exec,processbuilder
public void run() { try { Runtime rt = Runtime.getRuntime(); Process pr = rt.exec("C:\\Windows\\System32\\cmd.exe"); stdin = pr.getOutputStream(); writer = new BufferedWriter(new OutputStreamWriter(stdin)); writer.write("python setup.py py2exe"); writer.close(); BufferedReader input = new BufferedReader(new InputStreamReader(pr.getInputStream())); String line=null; while((line=input.readLine()) != null) { System.out.println(line); } } catch (Exception e) { e.printStackTrace(); } } I am...

Using ProcessBuilder to find version of Java

java,processbuilder
I am trying to use Java to find out the versions of Java installed on a machine. I have: List<String> commands = new ArrayList<String>(); commands.add("java.exe"); commands.add("-version"); ProcessBuilder pb = new ProcessBuilder(commands); pb.directory(new File("C:\\Program Files\\Java\\jdk1.6.0_45\\bin")); Process p = pb.start(); BufferedReader stdInput = new BufferedReader(new InputStreamReader(p.getInputStream())); while ((s = stdInput.readLine()) != null)...

Running an EXE from Java

java,runtime.exec,processbuilder
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...

Using scala.sys.process with timeout

scala,processbuilder
I find it extreemly cool to use standard syntax like import scala.sys.process._ val countLogger = ProcessLogger(line => {println ("out line: " + line)}, line => {println ("err line: " + line)}) val exitCode = ("cat prog.c" #&& "gcc prog.c -o prog -lm" #&& "echo running, this may hang" #&& "prog.exe")...

ProcessBuilder won't properly run with unified arguments with spaces in a path

java,groovy,processbuilder
I am trying to pass ProcessBuilder several arguments which require tags at the beginning for parameters (-). My command-line command looks something like this, and runs just fine when run in cmd. msdeploy.exe -verb:sync -source:contentPath='\My\Folder with Space\Path' -dest:auto My project uses a groovy script and implements ProcessBuilder to run the...

Java ProcessBuilder Cannot Find File Specified

java,python,file,processbuilder
import java.io.*; class RunTest { public static void main(String a[]) { try { String prg = "import sys\nprint int(sys.argv[1])+int(sys.argv[2])\n"; BufferedWriter out = new BufferedWriter(new FileWriter("test1.py")); out.write(prg); int number1 = 1; int number2 = 2; ProcessBuilder pb = new ProcessBuilder("python","test1.py",""+number1,""+number2); Process p = pb.start(); BufferedReader in = new BufferedReader(new InputStreamReader(p.getInputStream())); int...

How to execute ant and close the command prompt using Runtime.getRuntime().exec()

java,batch-file,command-line,ant,processbuilder
I want to execute an ant file from java. So i decided to use Runtime.getRuntime().exec() to achieve this. My java file will looks something like below, Process p = Runtime.getRuntime().exec("cmd /c start ant mytarget -Darg1="+arg1+" -Darg2="+arg2+" ", null, new File("E:/ant_demo")); System.out.println("Ant file executed"); ... ... .. System.out.println("Completed"); My goal is...

java processbuilder ffmpeg pipe

java,ffmpeg,pipe,processbuilder
i try to run ffmpeg out java. here my code: String[] temp = {"ffmpeg\\ffmpeg.exe","-i","input_track.ac3","-threads","0","-af","volume=volume="0.0"dB","-acodec","pcm_s32le","-ac","6","-ar","48000","-f","wav","-","|","ffmpeg\\fdkaac","--ignorelength","-m","1","-o","ouput_track.aac","-"}; ProcessBuilder pb = new ProcessBuilder(temp); Process p = pb.start(); int ev = 0; if (p.waitFor() != 0) { ev = p.exitValue(); } i try the comand at windows cmd, here have a problem with "|" at...

Why does BufferedReader fail if BufferedWriter is still open?

java,windows,processbuilder
I am trying to run a .exe file from a java application and would like to be able to use it (for now) like a terminal where i can write to the .exe then read back from it before writing it again. My issue is that the code only works...

CURL in JAVA - Could not resolve host : POST

java,post,curl,httpclient,processbuilder
I have been successfully able to perform CURL commands from the CMD in my Windows PC by installing curl for Windows. Similarly, I have been able to get them working in my JAVA application by using ProcessBuilder and Process to create Operating System process. In particular, I need to execute...

One does not simply grep into ProcessBuilder

java,shell,grep,processbuilder,herestring
does anyone know how to use linux grep with the java ProcessBuilder? Why does this code return an empty string when it should return "sing" ? import java.io.*; import java.util.*; public class Test2 { public static void main(String[] args) throws InterruptedException,IOException{ String line; // Initiate grep process. ProcessBuilder pb =...

SFTP Processbuilder

java,unix,sftp,processbuilder,ssh2-sftp
I have to invoke unix commands from Java and I have to use ProcessBuilder. I want to login through SFTP using key authentication and I want to SFTP the file from local directory to remote location. ProcessBuilder pb = new ProcessBuilder("sftp","-oIdentityFile=privateKey","-b","commands.txt","[email protected]"); Content in commands.txt is put localFileDirectory remoteDirectory exit Is...

Java processbuilder and using environment variables

java,linux,process,environment-variables,processbuilder
What I want to do is I want to run a process, however because this process itself relies on environment variables, directly calling it causes error within the process. For those who are wondering what this is, it's rake tool. For this reason I thought maybe it's better to use...

How can I detect if file not found for execution by ProcessBuilder in Java?

java,filenotfoundexception,processbuilder
I have this part of code, supposed to start msg.exe subprocess on Windows: ProcessBuilder pb = new ProcessBuilder("msg.exe"); try { proc = pb.start(); } catch (IOException ex) { jLabel4.setText("Cannot launch message box process"); return; } Now I tried to delete/rename msg.exe and no exception thrown: both lines in catch clause...

The system cannot find the path specified when running a program from d: instead of c:

java,processbuilder
I got a folder for testing stuff on my c: disk but my main project is on my d: disk. But now i was getting the same error while the code was the same after some research i found out it was beacause of the location. But is there a...

Not able to run cmd command through java

java,cmd,processbuilder
I have a .bat file called galen.bat. I included the path to this file in the PATH environment variable. When I run: galen.bat --version in cmd, I get the following output: Galen Framework Version: 1.6.3 JavaScript executor: Rhino 1.7 release 5 2015 01 29 I have the following java code...

Java run async processes

java,multithreading,asynchronous,process,processbuilder
I am trying to run an async process and I do not want the program to wait until the end of these processes executions. I found this question how to run shell script asynchronously from within Java program but it doesn't have the answer that I am looking for. What...

ProcessBuilder or Runtime.getRuntime().exec won't complete jar execution

java,xml,xslt,cmd,processbuilder
I have a jar which converts one XML to other XML format using XSLT in Java. The jar copies the output to some folder. It is working absolutely fine when am running it on command prompt but running it via Runtime.getRuntime().exec or ProcessBuilder, doesn't complete the process. Just 25 files...