Can't run program with ProcessBuilder, runs fine from command line
Looks like the wildcard character is not being expanded using a glob. You can use a shell instead:
ProcessBuilder pb =
new ProcessBuilder("bash", "-c", "java -jar ~/myjar.jar \".*\"");
or you can remove the double-quotes around the wildcard:
ProcessBuilder pb = new ProcessBuilder(java, "-jar", "~/myjar.jar", ".*");
Unable to run java commands using processbuilder
Yes, As @MichaelBerry said it is possible that you may not have permission to access it but other then that also I want to include,
Here you have started with very good ProcessBuilder you just need to modify small things like parameter -jar
in the constructor of processBuilder.
I've posted below sample code which may help you to understand how it will work.
ProcessBuilder pb = new ProcessBuilder("/path/to/java", "-jar", "your.jar");
pb.directory(new File("preferred/working/directory"));
Process p = pb.start();
ProcessBuilder won't run with arguments
The complete argument is being interpreted as the executable. Use
ProcessBuilder processBuilder = new ProcessBuilder("java", "-version");
Command Works Through Command Line, but Not When Using ProcessBuilder
Using this code, I was able to compile a Test.java file into a Test.class file on my Desktop.
import java.io.IOException;
public class App {
public static Process compile() throws IOException {
String myFilePath = "C:\\Users\\redacted\\Desktop\\Test.java";
String javacPath = "C:\\Program Files\\Java\\jdk1.8.0_171\\bin\\javac.exe";
ProcessBuilder processBuilder = new ProcessBuilder(javacPath, myFilePath);
return processBuilder.start();
}
public static void main(String[] args) throws IOException {
Process process = compile();
}
}
Using String javacPath = "javac.exe";
also worked, but that could be because my JDK bin is on my PATH variable.
There is something wrong with your paths or permissions in the ProcessBuilder constructor call.
Java ProcessBuilder is not executing command but also doesn't return an error
You are reading from the Process
's InputStream. You should be looking at Process.getErrorStream()
or Process.getOutputStream()
.
https://docs.oracle.com/javase/7/docs/api/java/lang/Process.html
You could also inspect the exitValue to see if the process successfully completed.
Java Process Builder - Cannot run a simple program
Your command must break all arguments into separate pieces - including thresholdFlag
. It is a good idea to check if the executable exists. If it does not you should check where it is located or fix your Path variable to ensure that it can be located:
File darkpath = new File(darknetNamePath);
String [] cmd = new String[] { darkpath.getAbsolutePath(), "detect", configurationFlag, weightsFlag, imageFlag, "-thresh", String.valueOf(thresholds.getValue()) };
System.out.println("Path: "+darkpath+ " exists="+darkpath.exists());
System.out.println("exec "+Arrays.toString(cmd));
processBuilder.command(cmd);
It is also worth handling STDERR, the easiest way is to redirect STDERR=>STDOUT before calling processBuilder.start()
processBuilder.redirectErrorStream(true);
If you want Java to launch the executable without prefixing the absolute path it needs to be in one of these directories:
System.out.println("PATH COMPONENTS FOR JAVA LAUNCH:");
Arrays.asList(System.getenv("PATH").split(File.pathSeparator)).forEach(System.out::println);
ProcessBuilder not executing command with wildcard
Use a command shell to interpret the wildcard
String[] command1 = {"bash", "-c", "/archive/scripts/grep.pl /apps/ws/logs/*"};
Related Topics
Connecting to Access Database from Linux
How to Run Java Application on Startup of Ubuntu Linux
Environmental Variables Not Seen in a Cron
Java Animation Programs Running Jerky in Linux
Accessing Linux Local File System from Java Web Application
Cannot Load R Xlsx Package on MAC Os 10.11
Wait Until Tomcat Finishes Starting Up
Android: Difference Between Onintercepttouchevent and Dispatchtouchevent
How to Make This Jbutton Visible? When I Have Progressive Scan Background Jwindow()
Inspecting Java Threads in Linux Using Top
How to Find a Java Thread Running on Linux with Ps -Axl
Run Custom Textsecure (Signal) Server
Create a New Folder Using Java Program on Windows and Linux MAChines
Serial Port Identification with Java on Ubuntu