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 executablesWhat 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 O开发者_JS百科S specific UNC path to the executable, but can it also handle accessing executables within the same jar? How?
Thank you.
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);
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.
精彩评论