运行在Java从文件位置的.exe文件 [英] Run .exe file from Java from file location

查看:145
本文介绍了运行在Java从文件位置的.exe文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我必须从我的Java程序中打开.exe文件。所以,我想下面的code首先

 工艺过程=的Runtime.exec(C:\\\\ Program Files文件\\\\ \\\\测试TEST.EXE);

但我收到的一些错误。后来我发现,该EXE必须从该位置即是C推出://程序文件/测试/只,那么它会用了错误的开启。所以我决定写一个bat文件并执行,这样它会cd到该位置并执行.exe文件。

以下是我的code:

 的BufferedWriter FILEOUT;字符串itsFileLocation =C:\\\\ Program Files文件\\\\ \\\\测试
 的System.out.println(itsFileLocation);
 尝试{
 FILEOUT =新的BufferedWriter(新FileWriter的(C:\\\\ test.bat的));
 fileOut.write(CD \\\\+\\ n);
 fileOut.write(CD+ itsFileLocation +\\ n);
 fileOut.write(test.exe的+\\ n);
 fileOut.write(退出+\\ n);
 
 
 fileOut.close(); //所有输出完成后关闭输出流。
 }赶上(IOException异常E1){
 // TODO自动生成catch块
 e1.printStackTrace();
 } //创建缓冲Writer对象写入到一个名为FILENAME.TXT
 运行运行时间=调用Runtime.getRuntime();
 尝试{
 工艺过程=的Runtime.exec(CMD / C开始C:\\\\ test.bat的);
 }赶上(IOException异常五){
 // TODO自动生成catch块
 e.printStackTrace();
 }

以上code完美的作品。然而,在命令提示符下也是我的.exe文件(应用程序)的后面打开。它仅关闭.exe文件后退出。

我要我的CLSE命令时提示我的应用程序的统计信息。任何帮助是极大的AP preciated ..在此先感谢..

我的.bat文件会像下面它是由程序写入之后。

  CD \\
CD C:\\ Program Files文件\\测试\\
TEST.EXE
出口


解决方案

您不需要控制台。您可以使用工作目录执行的过程:

执行exec(字符串命令,字符串[] envp,文件目录)

在一个单独的进程中执行指定的字符串命令
指定环境和工作目录。


  • 命令是.exe文件
  • 的位置
  • envp可以为空

  • 目录,是您的.exe的目录

对于您的code应该是...

 调用Runtime.getRuntime()EXEC(C:\\\\ Program Files文件\\\\ \\\\测试TEST.EXE,空,新的文件(C:\\\\ Program Files文件\\ \\测试\\\\));

I have to open a .exe file from my Java Program. So I tried following code First.

Process process =runtime.exec("c:\\program files\\test\\test.exe");

But I was getting some error. Then I found out that the exe has to be launched from that location that is c://program files/test/ only then it will open with out errors. So I decided to write a .bat file and execute so that it will cd to that location and execute the .exe file.

Following is my code:

BufferedWriter fileOut;

String itsFileLocation = "c:\\program files\\test\\"
    System.out.println(itsFileLocation);
    try {
     fileOut = new BufferedWriter(new FileWriter("C:\\test.bat"));
     fileOut.write("cd\\"+"\n");
     fileOut.write("cd "+ itsFileLocation +"\n");
     fileOut.write("test.exe"+"\n");
     fileOut.write("exit"+"\n");
     
     
     fileOut.close(); // Close the output stream after all output is done.
    } catch (IOException e1) {
     // TODO Auto-generated catch block
     e1.printStackTrace();
    } // Create the Buffered Writer object to write to a file called filename.txt
    Runtime runtime = Runtime.getRuntime();
    try {
     Process process =runtime.exec("cmd /c start C:\\test.bat");
    } catch (IOException e) {
     // TODO Auto-generated catch block
     e.printStackTrace();
    }

The above code works perfectly. However, the command prompt is also opened at the back of my .exe (Application). It closes only after the .exe file exits..

I need to clse my command prompt when my application stats. Any help is greatly appreciated.. Thanks in advance..

My .bat file will be like following after it is written by the program.

cd\
cd C:\Program Files\test\
test.exe
exit

解决方案

You don't need a console. You can execute a process using a working directory:

exec(String command, String[] envp, File dir)

Executes the specified string command in a separate process with the specified environment and working directory.

  • command is the location of the .exe
  • envp can be null
  • dir, is the directory of your .exe

With respect to your code it should be...

Runtime.getRuntime().exec("c:\\program files\\test\\test.exe", null, new File("c:\\program files\\test\\"));

这篇关于运行在Java从文件位置的.exe文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆