2011-12-13 38 views
1

假設我的項目結構是:最好的方式執行內部文件到Java項目

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

我想沒有硬編碼任何絕對路徑從內部Util.java執行a.out的我java文件。要做到這一點,最好的方法是什麼?

編輯 - 這是我最終做的:我碰巧使用autoconf,因爲大部分代碼都是C++。我在configure.ac中定義了一個替換變量,如AC_SUBST([project_root], [$(pwd)]),並將其替換爲Config.java.in文件。

+0

您是否將項目部署爲JAR? – 2011-12-13 22:51:57

+0

不作爲JAR進行部署。 – 2011-12-13 22:54:33

回答

0

你是a.out的文件路徑可能是../bin/a.out。然後使用該路徑執行該文件。

-1

下面是一些僞代碼,可以幫助你。

// 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 
} 
相關問題