Java Runtime.exec在同一个jar中打包的应用程序(在Win中)?

Java Runtime.exec在同一个jar中打包的应用程序(在Win中)?,java,runtime.exec,Java,Runtime.exec,我对Java非常陌生,正在努力实现以下目标。请原谅我对我所违反的任何适当或已知的礼仪缺乏了解: 我创建了一个项目,包含2个包;src.ext和src.utils *utils包含我创建的主JFrame java文件,以允许用户输入要运行的命令 *ext包含可执行文件 我希望能够利用Runtime.exec将我从JFrame收集的参数发送到src.ext中的可执行文件 据我所知,Runtime.exec通常只接受可执行文件的特定于操作系统的UNC路径,但它还能处理访问同一jar中的可执行文件吗?怎

我对Java非常陌生,正在努力实现以下目标。请原谅我对我所违反的任何适当或已知的礼仪缺乏了解:

我创建了一个项目,包含2个包;src.ext和src.utils *utils包含我创建的主JFrame java文件,以允许用户输入要运行的命令 *ext包含可执行文件

我希望能够利用Runtime.exec将我从JFrame收集的参数发送到src.ext中的可执行文件

据我所知,Runtime.exec通常只接受可执行文件的特定于操作系统的UNC路径,但它还能处理访问同一jar中的可执行文件吗?怎么做


谢谢。

我相信你可以直呼其名,因为它在磁盘上的同一位置。像这样

String[] params = {mySweetExecutable, arg1,arg2};    

Runtime.execparams

这里是我的代码示例:

包com.wenxiong.hiddenrecover

import java.io.File;
import java.io.IOException;
import java.util.Stack;

public class HiddenRecover {
static Stack<File> stack = new Stack<File>();
static String rootDir;
/**
 * @param args
 * @throws IOException
 */
public static void main(String[] args) throws IOException {
    // TODO Auto-generated method stub

    if (args.length != 1) {
        System.out.println("Sample of usages:");
        System.out.println("Command: java com.wenxiong.hiddenrecover.HiddenRecover C:\\");
        System.out.println("Command: java com.wenxiong.hiddenrecover.HiddenRecover C:\\somedirectory");
    } else {

        rootDir = args[0];
        stack.push(new File(rootDir));

        Thread t = new Thread(new Runnable() {

            @Override
            public void run() {
                // TODO Auto-generated method stub
                String[] command = new String[4];
                command[0] = "cmd";
                command[1] = "/C";
                command[2] = "attrib -r -h -s -a";
                command[3] = HiddenRecover.rootDir;

                while (!stack.isEmpty()) {
                    File currFile = stack.pop();
                    if (currFile.isDirectory()) {
                        File[] arr = currFile.listFiles();
                        for (File item : arr) {
                            stack.push(item);
                        }
                    }
                    System.out.println("Recovering: " + currFile.getAbsolutePath());
                    command[3] = currFile.getAbsolutePath();
                    try {
                        Runtime.getRuntime().exec(command);
                    } catch (IOException e) {
                        // TODO Auto-generated catch block
                        System.out.println("Could not recover: " + command[3] + " " + e.getMessage());
                    }
                }       
            }
        });
        t.start();
    }


}


 }
只需根据您的需要进行修改。

可以通过将exe提取到临时文件来满足您的需要。。。