Java: ProcessBuilder Changing the path
I'm writing a Java program that's supposed to be the GUI frontend that utilizes a tertiary C program to generate some val开发者_开发知识库ues for various labels.
But I don't want have to hard code the path to the C program. I just want the Java Program to execute the C program on the assumption that it'll be in the same directory that I had run the Java program from (otherwise have an error message prompt).
I've never used processbuilder before so I'll appreciate extensive examples :)
One way to get the path of the jar containing the current Java code is the following (where "THISCLASS" is the name of a class):
URL jarURL = THISCLASS.class.getProtectionDomain().getCodeSource().getLocation();
String jarPath = jarURL.getPath();
File file = new File( jarPath );
Given the path to the jar file, you can use the java.io.File API to travel a relative path to the executable.
精彩评论