在Windows上通过java程序运行c程序

时间:2012-08-27 14:28:52

标签: c windows

我有一个用于图形用户界面的Java程序。这个Java程序运行一个已编译的C程序。我创建一个JAR文件,以使我的程序可执行。因此,我的C程序包含在JAR文件中。

我使用这些行:

String[] Tab_arg =new String[6];

Tab_arg[0]="./src/generalisation.exe"; 
Tab_arg[1]=fileM.getAbsolutePath(); 
Tab_arg[2]=fileG.getAbsolutePath(); 
Tab_arg[3]=fichGA_absolutePath; 
Tab_arg[4]=fichGO_absolutePath; 
Tab_arg[5]=fileR.getAbsolutePath();

  try 
 {
    Process p =Runtime.getRuntime().exec(Tab_arg);
     BufferedReader in = new BufferedReader(new InputStreamReader(p.getInputStream()));
     String inputLine;
     while ((inputLine = in.readLine()) != null) System.out.println(inputLine);
} 
catch (IOException e) 
{
    e.printStackTrace();
}

问题是JAR文件在Ubuntu上运行正常但在Windows上运行不正确。

1 个答案:

答案 0 :(得分:1)

当您为Windows编译它时,可以将两个版本(Linux和Windows)添加到JAR文件中。在您的代码中,您可以添加此

if(System.getProperty("os.name").startsWith("Windows"))
    Tab_arg[0]=".\src\generalisation.exe";
else
    Tab_arg[0]="./src/generalisation";

如果Linux版本没有扩展名,这应该可以解决问题。

相关问题