Tech Off Thread

25 posts

Forum Read Only

This forum has been made read only by the site admins. No new threads or comments can be added.

hard problem. how to open a document from command line?

Back to Forum: Tech Off
  • User profile image
    anilp

    Hello,

    1) What command on the command line will open a document like how Windows opens it when one double-clicks its icon? Again, we do not know the application name, we simply know the full document path and are relying on the OS to open it.
    2) Is it portable? would it be very different in Macs, Unixes?

    In my program, I plan to pop up a File Chooser dialog so the user can browse to locate the document. Then I want to be able to open the document using the default application that was registered with Windows.

    For example, after he locates resume.rtf, I want to be able to open the document (in MSWord, if default) *without* knowing what application was used to open it.

    I need a single command line because I shall be calling it from Java.

    Appreciate any help!

    If you can copy your reply to goodnewsforyou AT yahoo DOT com, that will be great since there do not seem to be post notifications.

    thanks,
    Anil Philip

  • User profile image
    AndyC

    start documentName

    will do it on Windows, but it won't work on Mac or *nix. There is probably a Mac way of doing it but there won't be a unix one as it doesn't really have the concept of a "default" handler for documents.

  • User profile image
    anilp

    AndyC wrote:
    start documentName

    will do it on Windows


    Nope, it does not work. Actually I tried it before posting my qs.

    >start "printing photos.txt"

    simply opens another dos command window without opening the document.
    -
    Anil

  • User profile image
    DoomBringer

    anilp wrote:
    AndyC wrote: start documentName

    will do it on Windows


    Nope, it does not work. Actually I tried it before posting my qs.

    >start "printing photos.txt"

    simply opens another dos command window without opening the document.
    -
    Anil

    Works for me...

  • User profile image
    anilp

    I think the problem is with the double quotes. How does one open a document with spaces in the file name?
    thanx,
    Anil.

    DoomBringer wrote:
    anilp wrote:
    AndyC wrote: start documentName

    will do it on Windows


    Nope, it does not work. Actually I tried it before posting my qs.

    >start "printing photos.txt"

    simply opens another dos command window without opening the document.
    -
    Anil

    Works for me...

  • User profile image
    Sven Groot

    You're right that start doesn't work with the quotes for some reason.

    But you don't actually need the start. Just typing the name of the document on the command line is enough to get Windows to start the default application for that file. And that does work, even with spaces in the file name (using quotes, of course).

  • User profile image
    anilp

    Sven Groot wrote:
    You're right that start doesn't work with the quotes for some reason.

    But you don't actually need the start. Just typing the name of the document on the command line is enough to get Windows to start the default application for that file. And that does work, even with spaces in the file name (using quotes, of course).


    Sad You must be kidding.... here is the error!

    C:\Documents and Settings\juwo\My Documents\Letters>printing photos.txt
    'printing' is not recognized as an internal or external command,
    operable program or batch file.

  • User profile image
    anilp

    ok! I must put in the quotes.... but then am I correct that means I cannot automatically wrap the document name in quotes - but must search for a space in the file name before wrapping it? that is not so hard.



    anilp wrote:
    Sven Groot wrote: You're right that start doesn't work with the quotes for some reason.

    But you don't actually need the start. Just typing the name of the document on the command line is enough to get Windows to start the default application for that file. And that does work, even with spaces in the file name (using quotes, of course).


    Sad You must be kidding.... here is the error!

    C:\Documents and Settings\juwo\My Documents\Letters>printing photos.txt
    'printing' is not recognized as an internal or external command,
    operable program or batch file.

  • User profile image
    Cannot​Resolve​Symbol

    Sven Groot wrote:
    You're right that start doesn't work with the quotes for some reason.

    But you don't actually need the start. Just typing the name of the document on the command line is enough to get Windows to start the default application for that file. And that does work, even with spaces in the file name (using quotes, of course).

    Emphasis added.

  • User profile image
    anilp

    thanks for the solution - but do youknow if it works with all versions of Windows?
    -
    Anil

    anilp wrote:
    Sven Groot wrote: You're right that start doesn't work with the quotes for some reason.

    But you don't actually need the start. Just typing the name of the document on the command line is enough to get Windows to start the default application for that file. And that does work, even with spaces in the file name (using quotes, of course).


    Sad You must be kidding.... here is the error!

    C:\Documents and Settings\juwo\My Documents\Letters>printing photos.txt
    'printing' is not recognized as an internal or external command,
    operable program or batch file.

  • User profile image
    MarkThe​Viking

    anilp wrote:
    thanks for the solution - but do youknow if it works with all versions of Windows?
    -
    Anil


    If you put quotes round the first parameter, it makes that the title of the spawned window if it ends up running in a command prompt. IIRC, in earlier versions of Windows (98, NT) you need to put quotes round a path that contains spaces, so the command you need is START "" "my document.exe".

    Can't help with non-Windows platforms, I'm afraid!

  • User profile image
    Maurits

    For Windows, use start or ShellExecute

    For gory details:

    start /?

    But Microsoft recommends using ShellExecute (Java example: MS KB 283225)

    For OS X, use "open" (yes, this opens the default application, see link)

    OS X: open

    For Linux, here are some ideas (nautilus, kfmclient)

  • User profile image
    DCMonkey

    Mac OS X has an "open" command that is similar to Windows' "start" command.

    For Unix/Linux you'll probably have to find an equivalent command (if one exists) for each of the desktop environments you want to support (KDE, GNOME, CDE, XFCE, various other WM/DE combos that may have their own file association databases and APIs/tools to work with them). Or you could just call the programs indicated by the PAGER and EDITOR environment variables  Wink

  • User profile image
    anilp


    Maurits wrote:
    For Windows, use start or ShellExecute

    For gory details:

    start /?

    But Microsoft recommends using ShellExecute (Java example: MS KB 283225)

    For OS X, use "open" (yes, this opens the default application, see link)

    OS X: open

    For Linux, here are some ideas (nautilus, kfmclient)


    Why would one use that method in the link for ShellExecute when one can simply do a "start"? Also a poster (ChuckBing) reported using "cmd.exe /c resume.txt" successfully.

    http://www.gamedev.net/community/forums/topic.asp?topic_id=313980
    The last link with code by Da Wanderer (in Visual Basic?) to open a folder was helpful.  I am stumped by the code for Linux. I hope I can use the same options to open a document in Linux.

    I plan to use Runtime.exec in a similar manner in Java, to open a doc.
    - thanks!
    Anil

  • User profile image
    Cannot​Resolve​Symbol

    Not to mention that the JDK doesn't include the com.ms.* packages...  those are Microsoft proprietary extensions and not part of Java.

  • User profile image
    Cannot​Resolve​Symbol

    Okay, just did some real looking, and the answer is actually quite simple.

    Look here:  org.jdesktop.jdic.Desktop.open(java.io.File)

    The library you need can be downloaded from the main project page at https://jdic.dev.java.net/

    0.9.x and most of 0.8.x do NOT support MacOSX.  For OSX support, you need to download 0.8.6.  For Windows, Solaris, and Linux, JDIC allows you to use/change file associations, open the default browser, create tray icons (with associated Swing menus), and embed IE/Mozilla in Swing.

    I feel bad for not thinking of this earlier.  I am a regular visitor to the JavaDesktop site.  I'd recommend you check it out if you're interested in Java client programming (they do some impressive stuff with swing).

  • User profile image
    ploe

    i would suggest you post the linux question on java forums...most likely a solution is already on the forums. i had a similar problem when trying to open a browser in a java program. in windows you can do this easily...

    Process p = Runtime.getRuntime().exec("rundll32 url.dll,FileProtocolHandler " + url);

    in linux i had to do the "which" command ("which mozilla" etc) to figure out what browsers were installed in the system before i could open the url Sad

  • User profile image
    ploe

    i just found this anilp...worth looking at...
    http://download.java.net/jdk6/docs/api/java/awt/Desktop.html

    so it will be new to 1.6, but this would dissallow backwards compatiblity for users with an older JRE Sad

Conversation locked

This conversation has been locked by the site admins. No new comments can be made.