linux Runtime

本文介绍如何使用Java程序调用Windows系统的EXE文件,包括系统自带的应用如记事本(notepad)和用户自定义的可执行文件。同时,还提供了获取当前Java程序运行路径的方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Runtime.getRuntime().exec("main.exe "你的字符"")

------------------------------------------------------------------------

Runtime   runtime   =   new   Runtime();   
  String   fileName   =   "c:\\test.txt";   
  String   commandStr   =   "rundll32   url.dll,FileProtocolHandler   "+fileName;   
  runtime.exe(commandStr   );   
  或者   
  Runtime   runtime   =   new   Runtime();   
  String   fileName   =   "c:\\test.txt";   
  String   commandStr   =   "notePad   "+fileName;   
  runtime.exe(commandStr   );   
Runtime.getRuntime().exec("test.exe   "+param1+"   "+param2+"   "+param3);



Runtime.getRuntime().exec("c:\\windows\\system32\\cmd echo abc");


public class transferExe {

    /**

     * @param args

     */

    public static void main(String[] args) {

       openWinExe();

       openMyExe();

    }

 

    // 用Java调用windows系统的exe文件,比如notepad,calc之类

    public static void openWinExe() {

       Runtime rn = Runtime.getRuntime();

       Process p = null;

       try {

           String command = "notepad";

           p = rn.exec(command);

       } catch (Exception e) {

           System.out.println("Error win exec ");

       }

    }

 

    // 2.0调用其他的可执行文件,例如:自己制作的exe,或是下载安装的软件

    public static void openMyExe() {

       Runtime rn = Runtime.getRuntime();

       Process p = null;

       try {

           p = rn.exec("\"E:/QQ2008Spring.exe\"");

       } catch (Exception e) {

           System.out.println("Error my exec ");

       }

    }

}

 


在Java程序中获取当前运行程序的路径

import java.io.*;

public class Test {

       public static void main(String[] args) {

              File directory  = new File(".");

              try {

                     File newPath = new File(directory.getCanonicalPath()+"NewFolder");

                     newPath.mkdir();

              }catch(Exception exp)

              {

                     exp.printStackTrace();

              }

       }

}

//File directory       = new File(".");

//directory.getCanonicalPath();取得当前路径




String[]   open={"NotePad.exe","C:\\1.txt"};   
  Runtime.getRuntime().exec(open);



  Runtime   rt=Runtime.getRuntimee();   
  rt.exec("C:\\Program   Files\\Accessories\\Wordpad   D:\\xxxx.txt");
Top


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值