How do I use a file as argument?

Discussion in 'Java' started by Craig, Sep 22, 2007.

  1. Craig

    Craig Guest

    Heck, I can't even think of good search terms for this problem (maybe
    that's the real problem).

    I have a java program that's been developed on Linux but that I will
    distribute on Windows and Mac.
    I am to the point on the Windows version where my setup program will
    create a custom file type that will lead to my program being executed
    with a double click on the given file.

    I'm guessing that Window's shell or command interpreter is trying to
    pass my file to my program as an argument.

    What I'm missing is how to load the file when the program is called. I
    guess I need some init stuff like:

    If (*.myFiletype is given as an argument)
    {Start program and jump to "file-open" method;
    Load File.myFiletype}

    Can anyone give me some help on this? Even search terms that will net
    results would be good. A
    link to good reading material would be good help too.

    Regards,
    Craig
     
    Craig, Sep 22, 2007
    #1
    1. Advertising

  2. Craig

    Ben Phillips Guest

    Craig wrote:
    > What I'm missing is how to load the file when the program is called. I
    > guess I need some init stuff like:
    >
    > If (*.myFiletype is given as an argument)
    > {Start program and jump to "file-open" method;
    > Load File.myFiletype}
    >
    > Can anyone give me some help on this? Even search terms that will net
    > results would be good. A
    > link to good reading material would be good help too.


    The file path should appear as a commandline argument. So in your main
    method,

    public static void main (String[] args)

    you should look at the contents of the "args" array.

    First thing to do is experiment: modify your main method to add a "for
    (String s : args) System.out.println(s);" and see what it outputs at
    startup a) when you just plain run it and b) when you try to launch one
    of its files.
     
    Ben Phillips, Sep 22, 2007
    #2
    1. Advertising

  3. Craig wrote:
    ...
    >I am to the point on the Windows version where my setup program will
    >create a custom file type that will lead to my program being executed
    >with a double click on the given file.

    ...
    >What I'm missing is how to load the file when the program is called. I
    >guess I need some init stuff like:


    Java web start offers the option to associate a filetype
    with a JWS application (or at least, prompt the user to
    allow the association).

    The end result is that the program is called with
    arguments like "-open filename".

    Here is an example of using the JNLP API's
    FileOpenService to gain the file itself.
    <http://www.physci.org/jws/#fs>
    filetest.jnlp is an 'all-permissions' version of
    the demo, while filetest-sandbox.jnlp demonstrates
    the same in a sandboxed app.

    Note that an all-permissions app. could avoid using
    the FOS, and simply establish a "new File(filename)".

    HTH

    --
    Andrew Thompson
    http://www.athompson.info/andrew/

    Message posted via JavaKB.com
    http://www.javakb.com/Uwe/Forums.aspx/java-general/200709/1
     
    Andrew Thompson, Sep 22, 2007
    #3
  4. Craig

    Roedy Green Guest

    On Sat, 22 Sep 2007 08:22:19 -0700, Craig <>
    wrote, quoted or indirectly quoted someone who said :

    >I am to the point on the Windows version where my setup program will
    >create a custom file type that will lead to my program being executed
    >with a double click on the given file.


    The only simple Java tool for creating that association is part of the
    latest version of Java Web Start. See Java Web Start.

    If you don't want to do that, you will have to poke the registry
    directly yourself to create the association using JNI.

    Another option is to use an installer program written in C++ that
    builds associations.

    See http://mindprod.com/jgloss/javawebstart.html
    http://mindprod.com/jgloss/jni.html
    http://mindprod.com/jgloss/installer.html
    --
    Roedy Green Canadian Mind Products
    The Java Glossary
    http://mindprod.com
     
    Roedy Green, Sep 22, 2007
    #4
  5. Craig

    Craig Guest

    On Sep 22, 11:25 am, Ben Phillips <>
    wrote:
    > Craig wrote:
    > > What I'm missing is how to load the file when the program is called. I
    > > guess I need some init stuff like:

    >
    > > If (*.myFiletype is given as an argument)
    > > {Start program and jump to "file-open" method;
    > > Load File.myFiletype}

    >
    > > Can anyone give me some help on this? Even search terms that will net
    > > results would be good. A
    > > link to good reading material would be good help too.

    >
    > The file path should appear as a commandline argument. So in your main
    > method,
    >
    > public static void main (String[] args)
    >
    > you should look at the contents of the "args" array.
    >
    > First thing to do is experiment: modify your main method to add a "for
    > (String s : args) System.out.println(s);" and see what it outputs at
    > startup a) when you just plain run it and b) when you try to launch one
    > of its files.


    Yes, this sounds right. I ran up against the clock this morning, but
    I'll try to set this up when I have some time and report back. Thanks
    for the help.

    Regards,
    Craig
     
    Craig, Sep 23, 2007
    #5
  6. Craig

    Craig Guest

    On Sep 22, 12:26 pm, "Andrew Thompson" <u32984@uwe> wrote:
    > Craig wrote:
    >
    > ..
    >
    > >I am to the point on the Windows version where my setup program will
    > >create a custom file type that will lead to my program being executed
    > >with a double click on the given file.

    > ..
    > >What I'm missing is how to load the file when the program is called. I
    > >guess I need some init stuff like:

    >
    > Java web start offers the option to associate a filetype
    > with a JWS application (or at least, prompt the user to
    > allow the association).
    >
    > The end result is that the program is called with
    > arguments like "-open filename".
    >
    > Here is an example of using the JNLP API's
    > FileOpenService to gain the file itself.
    > <http://www.physci.org/jws/#fs>
    > filetest.jnlp is an 'all-permissions' version of
    > the demo, while filetest-sandbox.jnlp demonstrates
    > the same in a sandboxed app.
    >
    > Note that an all-permissions app. could avoid using
    > the FOS, and simply establish a "new File(filename)".
    >

    Thank you for this direction, and the prompt reply. When I have a
    minute I'll check this out.

    Regards,
    Craig
     
    Craig, Sep 24, 2007
    #6
  7. Craig

    Craig Guest

    On Sep 22, 3:34 pm, Roedy Green <>
    wrote:
    > On Sat, 22 Sep 2007 08:22:19 -0700, Craig <>
    > wrote, quoted or indirectly quoted someone who said :
    >
    > >I am to the point on the Windows version where my setup program will
    > >create a custom file type that will lead to my program being executed
    > >with a double click on the given file.

    >
    > The only simple Java tool for creating that association is part of the
    > latest version of Java Web Start. See Java Web Start.
    >
    > If you don't want to do that, you will have to poke the registry
    > directly yourself to create the association using JNI.
    >
    > Another option is to use an installer program written in C++ that
    > builds associations.
    >
    >

    Thank you for your suggestions, I'll take a look at all this stuff.
    Thanks also for your Java focused web pages. They are a great resource
    that I've used many times.

    Regards,
    Craig
     
    Craig, Sep 24, 2007
    #7
  8. Craig

    Brian Guest

    On Sat, 22 Sep 2007 08:22:19 -0700, Craig <>
    wrote:

    >What I'm missing is how to load the file when the program is called. I
    >guess I need some init stuff like:
    >
    >If (*.myFiletype is given as an argument)
    > {Start program and jump to "file-open" method;
    > Load File.myFiletype}


    You must do something like this:

    public static void main(String[] args){
    File myFile = new File(args[0]);
    }

    args[0] is at String object with the path and filename of your file
    ex.:

    "c\\myDirectory\\filename.dat"

    Hope this was any helP

    /Brian
     
    Brian, Sep 24, 2007
    #8
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Bhushit Joshipura

    defaulting argument to previous argument

    Bhushit Joshipura, Dec 29, 2003, in forum: C++
    Replies:
    5
    Views:
    428
  2. Ben Kial
    Replies:
    1
    Views:
    721
    Eric Enright
    Nov 15, 2004
  3. S?ren Gammelmark
    Replies:
    1
    Views:
    1,984
    Eric Sosman
    Jan 7, 2005
  4. nw
    Replies:
    0
    Views:
    338
  5. ±è ÁØ¿µ
    Replies:
    7
    Views:
    270
    Einar Magnús Boson
    Nov 27, 2008
Loading...

Share This Page