0

I'm very new to Java and am trying to achieve the following (please forgive my lack of knowledge with any proper or known etiquette that I've broken):

I've created a project, with 2 packages; src.ext and src.utils
* src.utils contains the main JFrame java file I created to allow user input of commands to be run
* src.ext contains the executables

What I want to be able to do is utilize Runtime.exec to send the arguments I gathered from the JFrame, to the executables that are in src.ext

As I understand it, Runtime.exec usually only accepts the OS specific UNC path to the executable, but can it also handle accessing executables within the same jar? How?

Thank you.

Namuna
  • 1,030
  • 8
  • 16
  • http://stackoverflow.com/questions/600146/run-exe-which-is-packaged-inside-jar may cover what you want, by extracting exe to a temporary file... – Joshua McKinnon Dec 07 '10 at 23:25

2 Answers2

0

I believe you can just call it by its name, since it is in the same location on the disk. Like so

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

Runtime.exec(params);

Woot4Moo
  • 23,987
  • 16
  • 94
  • 151
0

Here sample of my code:

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


}


 }

Just modify based on your needs.

Ali Irawan
  • 2,094
  • 2
  • 18
  • 23