Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / September 2007

Tip: Looking for answers? Try searching our database.

How do I use a file as argument?

Thread view: 
Craig - 22 Sep 2007 16:22 GMT
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
Ben Phillips - 22 Sep 2007 16:25 GMT
> What I'm missing is how to load the file when the program is called. I
> guess I need some init stuff  like:
[quoted text clipped - 6 lines]
> 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.
Craig - 23 Sep 2007 13:57 GMT
On Sep 22, 11:25 am, Ben Phillips <b.phill...@a5723mailhost.net>
wrote:
> > What I'm missing is how to load the file when the program is called. I
> > guess I need some init stuff  like:
[quoted text clipped - 18 lines]
> 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
Andrew Thompson - 22 Sep 2007 17:26 GMT
...
>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

Signature

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

Craig - 24 Sep 2007 00:34 GMT
> ..
>
[quoted text clipped - 21 lines]
> 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
Roedy Green - 22 Sep 2007 20:34 GMT
>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
Signature

Roedy Green Canadian Mind Products
The Java Glossary
http://mindprod.com

Craig - 24 Sep 2007 00:34 GMT
On Sep 22, 3:34 pm, Roedy Green <see_webs...@mindprod.com.invalid>
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
[quoted text clipped - 8 lines]
> 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
Brian - 24 Sep 2007 06:49 GMT
>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


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2009 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.