views:

46

answers:

2

I'm writing a Java program that's supposed to be the GUI frontend that utilizes a tertiary C program to generate some values 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 :)

A: 

Check this tutorial out.

Boris Pavlović
+1  A: 

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.

Andy Thomas-Cramer
Thank you very much :)