Running Powershell from Java

不打扰是莪最后的温柔 提交于 2020-01-15 08:11:28

问题


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.

来源:https://stackoverflow.com/questions/13623369/running-powershell-from-java

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!