2011-12-13 33 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 
} 
相关问题