5

Is there any way that I can open notepad or other application from shortcuts?

Here is my code:

import java.io.File;
import java.io.IOException;

public class acrobat {

    public static void main(String[] args) throws IOException, InterruptedException {
         String[] notepad = {"C:\\Users\\Desktop\\notepad.lnk"};

         Process p = Runtime.getRuntime().exec(notepad);
         p.waitFor();
    }
}

I want to open application from shortcut, but I am getting error..

    Exception in thread "main" java.io.IOException: Cannot run program "C:\Users\robert\Desktop\notepad.lnk": CreateProcess error=193, %1 is not a valid Win32 application
    at java.lang.ProcessBuilder.start(Unknown Source)
    at java.lang.Runtime.exec(Unknown Source)
    at java.lang.Runtime.exec(Unknown Source)
    at acrobat.main(acrobat.java:11)
Caused by: java.io.IOException: CreateProcess error=193, %1 is not a valid Win32 application
    at java.lang.ProcessImpl.create(Native Method)
    at java.lang.ProcessImpl.<init>(Unknown Source)
    at java.lang.ProcessImpl.start(Unknown Source)
    ... 4 more

If I only write notepad.exe than its working but, with path its not working. Is there any way that I can open with shortcuts?

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
Jayraj Patel
  • 79
  • 1
  • 10
  • 6
    Shortcut has extension `.lnk`, that's why the program can't find the file specified. The `.exe` is in the programs folder, the `.lnk` links the desktop shortcut to the executable in the programs folder, on the desktop you have to look for `notepad.lnk` – BackSlash Jul 25 '13 at 13:56
  • Open a console, go to the path, do a `dir` and you can see what files there are. – m0skit0 Jul 25 '13 at 14:00
  • 1
    so, i tried that, C:\\Users\\Desktop\\notepad.lnk Its not working please help me, getting same error!! – Jayraj Patel Jul 25 '13 at 14:01
  • BackSlash is right. An alternative would be to open the default application for the file you want to edit/produce, using the Desktop API. This way, the user gets the software he likes opened, for instance Notepad++ instead of Notepad. – Jannis Alexakis Jul 25 '13 at 14:02
  • `C:\Users\Desktop\robert` or `C:\Users\Desktop` ? Typically you'd have a username in such a path. – reto Jul 25 '13 at 14:02
  • C:\Users\Desktop\robert\notepad.lnk I tried that but not working!!:( – Jayraj Patel Jul 25 '13 at 14:06

2 Answers2

7

The shortcut you see in your desktop is actually a file with the extension .lnk. It's real full path is, then:

C:\Users\Desktop\notepad.exe.lnk

Trying to run it through exec() will yield a "CreateProcess error ... is not a valid Win32 application" error.


Fortunately, you can run those as well through the ProcessBuilder utility class.

public static void main(String[] args) throws Exception {
  ProcessBuilder pb = new ProcessBuilder("cmd", "/c",
                                      "C:\\Users\\robert\\Desktop\\notepad.lnk");
  Process p = pb.start();
  p.waitFor();
}



If you must use Runtime.getRuntime().exec(), you can open the lnk file through rundll32:

Process p = Runtime.getRuntime().exec("rundll32 SHELL32.DLL,ShellExec_RunDLL " +
                                      "C:\\Users\\robert\\Desktop\\notepad.lnk");
p.waitFor(); // watch out

But keep in mind, by this approach, the p.waitFor(); and similar method calls may not have the expected result: As you can see, the created process is the rundll32, not the shortcut's (notepad.exe).

acdcjunior
  • 132,397
  • 37
  • 331
  • 304
  • Actually this one is not working!! I didn't get any error, but its not opening notepad soo, please help me!! Thanks!! – Jayraj Patel Jul 25 '13 at 14:09
  • Which one did you try, exactly? – acdcjunior Jul 25 '13 at 14:12
  • I tried both actually!!!! haha.... but I don't why its not working!!:( it didn't give me any error!! – Jayraj Patel Jul 25 '13 at 14:13
  • 1
    I edited the question. Try it out again: `ProcessBuilder pb = new ProcessBuilder("cmd", "/c", "C:\\Users\\robert\\Desktop\\notepad.lnk"); Process p = pb.start(); p.waitFor();` – acdcjunior Jul 25 '13 at 14:16
  • I am trying to open Acrobat dist.. from desktop, than run all the text file in folder from acrobat dist that just open from desktop, after done, close that acrobat dist, that's what i am trying to do, I can open acrobat dist but not from desktop soo, please help me!! Thanks!! – Jayraj Patel Jul 25 '13 at 14:16
  • I edited the question to use your paths, as specified in the question. – acdcjunior Jul 25 '13 at 14:17
2

Look at Desktop.open(File) and associated methods.

  • There is more point to opening the default editor of a text file, than there is in loading a 'naked' program.
  • It will work for all files for which there is a file association.
  • It will work on OS X & *nix.
  • It won't alienate the user who prefers to edit text files in TextPad, or MS Word, or..

However, if you really must open links..

Read (and implement) all the recommendations of When Runtime.exec() won't. That might solve the problem. If not, it should provide more information as to the reason it failed. Then ignore that it refers to exec and build the Process using a ProcessBuilder. Also break a String arg into String[] args to account for arguments which themselves contain spaces.

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
  • @trashgod If I wanted to code for Windows exclusively, I'd use .Net. I'd be willing to bet it can achieve this in a reliable way, using 2-3 code lines. By the time the accepted answer were worked into a robust version, it would be at least 10-15 lines of Java. – Andrew Thompson Jul 26 '13 at 13:27
  • 1
    Or more, as I recall from this [forerunner](http://javadesktop.org/articles/jdic/). – trashgod Jul 26 '13 at 13:35
  • 1
    @trashgod That just reminded me of `BrowserLauncher` - people would try to achieve in 10-20 code lines, what it did *reliably* in ..around 20 KB of *compiled & Jar'd* code. – Andrew Thompson Jul 26 '13 at 13:46