How do I use a file as argument?

C

Craig

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
 
B

Ben Phillips

Craig said:
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.
 
A

Andrew Thompson

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
 
R

Roedy Green

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
 
C

Craig

Craig said:
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
 
C

Craig

Craig wrote:

..


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
 
C

Craig

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
 
B

Brian

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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,766
Messages
2,569,569
Members
45,042
Latest member
icassiem

Latest Threads

Top