Java | 通过程序代码打开EXE应用或者文件

通过Java后台调用本地exe程序

JAVA后台无法实现打开客户端上的应用程序以及文件,是由于JAVA本身的安全性限制,只能打开服务器本地的程序以及文件,直接上代码,测试运行即可。

import java.awt.Desktop;  
import java.io.File;  
import java.io.IOException;  
import java.util.ArrayList;  
import java.util.List;
public class exec {
    /**
     * @author Sunxy Create By 2017-9-18 上午11:49:38
     * @do TODO
     * @param args
     * @throws IOException 
     */
    public static void main(String[] args) throws IOException {
        useProcessBuilder();
        useAWTDesktop();
        useRuntimeExec();
    }
     
     /** 
     * 借助java.lang.ProcessBuilder打开 
     * @throws IOException 
     */ 
    private static void useProcessBuilder() throws IOException{  
        //new ProcessBuilder("notepad.exe", "C:/Users/Jadyer/Desktop/test file/readme.txt").start();  
        List<String> commands = new ArrayList<String>();  
        commands.add("D:/softWare/WeChat/WeChat.exe");  
        //commands.add("F:/C.Project/便签.txt");  
        new ProcessBuilder(commands).start();  
    }  
 
    /** 
     * 借助java.awt.Desktop打开 
     * @see 打开的目录或文件名中允许包含空格 
     */ 
    private static void useAWTDesktop() throws IOException{  
        Desktop.getDesktop().open(new File("F:/C.Project/便签.txt"));  
    }  
 
    /** 
     * 借助java.lang.Runtime打开 
     * @see WPS文字--------Runtime.getRuntime().exec("cmd /c start wps") 
     * @see WPS表格--------Runtime.getRuntime().exec("cmd /c start et") 
     * @see WPS演示--------Runtime.getRuntime().exec("cmd /c start wpp") 
     * @see Office Word---Runtime.getRuntime().exec("cmd /c start winword") 
     * @see Office Excel--Runtime.getRuntime().exec("cmd /c start excel") 
     */ 
    private static void useRuntimeExec() throws IOException{  
        /* 
         * 若打开的目录或文件名中不包含空格,就用下面的方式 
         */ 
        Runtime.getRuntime().exec("cmd /c start D:/softWare/WeChat/WeChat.exe");  
         
        Runtime.getRuntime().exec("cmd /c start F:/C.Project/便签.txt"); 
        /* 
         * (可以'运行'或'Win+R',然后输入'cmd /?'查看帮助信息) 
         */ 
        //Runtime.getRuntime().exec(new String[]{"cmd.exe", "/c", "D:/my local/测试用例.xls"});  
        /* 
         * 借助本地安装程序打开 
         * 若打开的目录或文件名中包含空格,它就无能为力了..不过本地程序的安装目录允许含空格 
         */ 
        String etCommand = "D:/Program Files/WPS/8.1.0.3526/office6/et.exe";  
        String filePath = "D:/mylocal/测试用例.xls";  
        Runtime.getRuntime().exec(etCommand + " " + filePath);  
    }  
     
    /**
     *  public static void main(String[] args) {
        String path = "D:\\public.bat";
        Runtime run = Runtime.getRuntime();
        try {
            // run.exec("cmd /k shutdown -s -t 3600");
            Process process = run.exec("cmd.exe /k start " + path);
            InputStream in = process.getInputStream();  
            while (in.read() != -1) {
                System.out.println(in.read());
            }
            in.close();
            process.waitFor();
        } catch (Exception e) {         
            e.printStackTrace();
        }
    }
     */
} 

JS打开客户端上的应用程序或者文件(注册表方式)

如果要实现通过点击页面上,打开本地程序,则使用js代码来是实现。

  1. 使用记事本(或其他文本编辑器)创建一个openWeChat.reg文件,并写入以下内容(还可以传递参数)
Windows Registry Editor Version 5.00 
[HKEY_CLASSES_ROOT\openWeChat]  
@="URL:openWeChat Protocol" 
"URL Protocol"="D:\\softWare\\WeChat\\WeChat.exe" 
[HKEY_CLASSES_ROOT\openWeChat\DefaultIcon]  
@="D:\\softWare\\WeChat\\WeChat.exe" 
[HKEY_CLASSES_ROOT\openWeChat\shell]  
[HKEY_CLASSES_ROOT\openWeChat\shell\open]  
[HKEY_CLASSES_ROOT\openWeChat\shell\open\command]  
#@="cmd /c set m=%1 & call set m=%%m:openNotepad:=%% & call \"D:\\softWare\\Notepad++\\notepad++.exe\" %%m%% & exit"
@="D:\\softWare\\WeChat\\WeChat.exe"
  1. 修改参数,按下图修改这几处后,保存为xxx.reg文件,双击执行,注册表注册成功。
    在这里插入图片描述
    在这里插入图片描述
  2. 调用,在html中引用 <a href="openWeChat:">微信</a> openWeChat 对应注册表中的名称,注意后面的冒号不要丢掉
<!-- 操作 菜单 -->
<li class="menu-item">
    <div class="menu">
        <a class="menu-hd"  style="width:68px;">操作<b class="bt"></b></a>
            <div class="menu-bd" style="width:80px;line-height:1.7;" role="menu" aria-hidden="true" id="menu-4">
                <div id = "oper">
                    <a href="openWeChat:">微信</a>
                </div>
            </div>
    </div>
</li>

在这里插入图片描述


  • 0
    点赞
  • 26
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值