2

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.

6
  • 1
    In PowerShell, start is an alias for invoke-item. start.exe will execute directly from cmd.exe and does essentially the same thing, so by invoking PowerShell from cmd you're just adding a layer of obfuscation that's completely unnecessary. Commented Nov 29, 2012 at 13:32
  • Hi alroc, Yeah, I have gotten rid of the cmd layer of this, and it is commented out in the code above (althought it looks messy), but I am having the same problem with or without the cmd layer. I have also tried using 'Invoke-Item' but to no avail. I am still not sure why this works in the IDE, but not from my Java, if I replace the start command with simply "explorer" it works. Commented Nov 29, 2012 at 14:23
  • 1
    start isn't a program, it's a command built into cmd.exe and you can't run it as a parameter of the program (try this in Start -> Run: cmd.exe start notepad.exe. Now try start notepad.exe from a command prompt.). cmd can process a BAT file though - put your start command in a BAT file and call cmd.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? Commented Nov 29, 2012 at 14:26
  • I just need to test if the permissions allow that specific file to be run via powershell, I also have to check via CMD, which I have already working. I just cant seem to figure out why powershell wont run the file. When the input string is {"powershell, "explorer"}; it runs fine but when I try to open the .LNK it fails to open. Commented Nov 29, 2012 at 15:44
  • I managed to get this to work. The problem was trying to pass the whitespace into 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. Commented Dec 3, 2012 at 11:33

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.