I am currently trying to see if I can run a program shortcut in windows (.lnk) file, from powershell, within Java. I know there are better tools to use, and that I should just interact directly with the .exe, but please humor me, this is for testing purposes.
So essentially, I need to run the .lnk file, via powershell from java. The main predicament I am currently having is that the command which should work from within powershell
"start \"C:/Adobe Reader X.lnk\""
In the IDE this will run Adobe reader correctly, but in java after initializing the ProcessBuilder, and trying to pass through this argument, it does not work. It will however run the powershell process. Here is the code of what im passing to my method:
String[] command2 = { /*"cmd.exe", "/C",*/ "powershell", "-Command","&","start \"C:/Adobe Reader X.lnk\"" };
As you can see, I have also tried starting it from CMD. Here is my Run code. I read the output (which there is none of) I simply just want to start Adobe Reader in a thread, and then I can check to see if the process is running or not via Tasklist.
public void run() {
String line2;
ProcessBuilder probuilder = new ProcessBuilder(command);
Process process = null;
try {
process = probuilder.start();
} catch (IOException e2) {
// TODO Auto-generated catch block
e2.printStackTrace();
}
java.io.InputStream is = process.getInputStream();
InputStreamReader isr = new InputStreamReader(is);
BufferedReader br = new BufferedReader(isr);
// create a reader for the return data from cmd.
StringBuilder sb = new StringBuilder();
// create a string builder to automate the string addition
try {
while ((line2 = br.readLine()) != null) {// build the input
// string from
// cmd.
sb = sb.append(line2);
System.out.println(line2);
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
After passing this through, I simply get the powershell process running, but Adobe reader doesnt run, or even start. Any help would be greatly appreciated.
invoke-item.start.exewill execute directly fromcmd.exeand does essentially the same thing, so by invoking PowerShell fromcmdyou're just adding a layer of obfuscation that's completely unnecessary.startisn't a program, it's a command built intocmd.exeand you can't run it as a parameter of the program (try this in Start -> Run:cmd.exe start notepad.exe. Now trystart notepad.exefrom a command prompt.).cmdcan process a BAT file though - put yourstartcommand in a BAT file and callcmd.exe runme.bat, see if that works. But why are you invoking an LNK file in the first place, instead of just running Adobe Reader directly?powershell. I found that using the tilde delimiter for whitespace solved my problem. For instance:{"powershell", "ii", "C:/Program(tilde key here, damn formatting!) Files/insertexehere.exe"}This worked for me. Thanks though @Alroc.