1

Suppose my project structure is:

/project
  /src
    /java
      Util.java
    /cpp
  /bin
    a.out

I'd like to execute a.out from within Util.java without hard-coding any absolute paths in my java file. What's the best way to go about doing this?

EDIT -- Here's what I ended up doing: I happen to be using autoconf as most of the code is c++. I defined a substitution variable like AC_SUBST([project_root], [$(pwd)]) in configure.ac and substituted it in a Config.java.in file.

Robert D
  • 137
  • 1
  • 8

3 Answers3

3

Perhaps using a properties file to be loaded on deployment/running time depending on the nature of your app.

More about its use in this thread How to use Java property files?

Community
  • 1
  • 1
Alfabravo
  • 7,493
  • 6
  • 46
  • 82
  • Don't I still need to store an absolute path in the property file? – Robert D Dec 13 '11 at 23:27
  • Perhaps you can get the context path when your app starts, so you add the relative route from the properties. The absolute path is also an option – Alfabravo Dec 14 '11 at 10:19
0

You're file path to a.out could be ../bin/a.out. And then execute the file using that path.

Jon Egeland
  • 12,470
  • 8
  • 47
  • 62
-1

Below is some pseudo code that might help you.

// look for the executable in the current working directory 
File executable = new File("a.out");
if( executable.exists()){
   System.exec() .... etc
} else {
   String location = YourMainClass.class.getProtectionDomain().getCodeSource().getLocation();
   // write code to form a path name to the a.out based on the location of .jar file 
}
ams
  • 60,316
  • 68
  • 200
  • 288