简体   繁体   English

在Windows上通过Java程序运行C程序

[英]run c program by java program on windows

I have a Java program for the graphical user interface. 我有一个用于图形用户界面的Java程序。 This Java program runs a C program which is already compiled. 该Java程序运行已编译的C程序。 I create a JAR file in order to make my program executable. 我创建一个JAR文件以使程序可执行。 As a consequence my C program is included in the JAR file. 结果,我的C程序包含在JAR文件中。

I use those lines : 我用这些行:

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();
}

The trouble is that the JAR file operates correctly on Ubuntu but not on Windows. 问题是JAR文件在Ubuntu上可以正常运行,但在Windows上不能正常运行。

When you have compiled it for Windows, you could add the two versions (Linux and Windows) to the JAR file. 为Windows编译后,可以将两个版本(Linux和Windows)添加到JAR文件中。 In your code you could add this 在您的代码中,您可以添加

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

This should do the trick if the Linux version has no extension. 如果Linux版本没有扩展名,这应该可以解决问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM