开发者

Java: ProcessBuilder Changing the path

开发者 https://www.devze.com 2023-01-05 02:30 出处:网络
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.

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.

0

精彩评论

暂无评论...
验证码 换一张
取 消