File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Java in General and the fly likes File extensions Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "File extensions" Watch "File extensions" New topic
Author

File extensions

Matthew Middleton
Greenhorn

Joined: May 05, 2004
Posts: 23
I've written a program that you can open a file into, when you chose open it reads in the data and displays it to modify in text areas. I want to be able to make it so if you double click on a file of the certain extension it calls that open function. I know of a couple programs that can integrate a java app into windows so that you can make a file extension open the program, but it only opens the program, it doesn't open the file and read it into the text areas like i want, does any one have an idea on how do to that. I was thinking maybe a way to get the filename of the file that called it??
Thank you for your time and may GOD Bless you!

Matthew Middleton
Layne Lund
Ranch Hand

Joined: Dec 06, 2001
Posts: 3061
I only have experience writing applications like this in C++, not Java, but as far as I know, the file name is sent as a command-line argument. You should be able to pick it up in main() and send it to the correct object/method that opens the file.

HTH

Layne


Java API Documentation
The Java Tutorial
Stefan Wagner
Ranch Hand

Joined: Jun 02, 2003
Posts: 1923

After using Laynes suggestion, you have to modify the Windows - File-extensions, and bind your program to the extension: .

I'm not sure about the %1%, but have a look at similar programs for the exact syntax.

If your program is in a jar:

Of course you have to build the jar with the 'Main-Class: YourApp' -manifest entry.


http://home.arcor.de/hirnstrom/bewerbung
Ko Ko Naing
Ranch Hand

Joined: Jun 08, 2002
Posts: 3178
Originally posted by Stefan Wagner:

If your program is in a jar:

Of course you have to build the jar with the 'Main-Class: YourApp' -manifest entry.


What is it? %1% Kind of variable that we have to replace something into it?


Co-author of SCMAD Exam Guide, Author of JMADPlus
SCJP1.2, CCNA, SCWCD1.4, SCBCD1.3, SCMAD1.0, SCJA1.0, SCJP6.0
Jeff Walker
Ranch Hand

Joined: Apr 25, 2004
Posts: 116
Originally posted by Matthew Middleton:
I've written a program that you can open a file into, when you chose open it reads in the data and displays it to modify in text areas. I want to be able to make it so if you double click on a file of the certain extension it calls that open function. I know of a couple programs that can integrate a java app into windows so that you can make a file extension open the program, but it only opens the program, it doesn't open the file and read it into the text areas like i want, does any one have an idea on how do to that. I was thinking maybe a way to get the filename of the file that called it??
Thank you for your time and may GOD Bless you!

Matthew Middleton


Ok,
if your file type is *.txt for instance, when you associate it with a program in Windows (any version), Windows will be looking for an exe to associate it with. Now, your Java program is not an executable, it is a .class file that is run in a JVM.

So,
I think you need to build a program in C++ (we need an exe), let's call it JOpener. You need to associate it with files of your extension in Explorer. In JOpener, you will need to call some win32 functions
such as:
int _execle( const char *cmdname, const char *arg0, ... const char *argn, NULL, const char *const *envp);

to open a new executable in a separate thread, in a new 32bit memory space. JOpener will ultimatley call 'javaw' with a parameter, which is the filename passed to JOpener. The last step is to decide whether JOpener should stay up or shut down, since javaw will be started by now in a separate thread. I would shut JOpener down, its useless now.
I think this is the way to do it.
Jeff Walker
Stefan Wagner
Ranch Hand

Joined: Jun 02, 2003
Posts: 1923

You don't need a JOpener to write in C++.

If you call a java program from the commandline:

your main-Class (hopefully) is called, and get's the parameter 'data.txt' as first (and only) argument.


If you pull a the file 'data.txt' with the Mouse to the 'MyClass.class'-icon, windows looks for an association to '*.class'.
(analog: myJar.jar -> *.jar).
It start's your application with 'javaw' - if correctly configured, and passes the filename (I guess with complete path - test it yourself - I got rid of my windows ).
And "%1" means 'parameter one', so if you mark two files, the app will only get one of them.
Which? - Hm. I don't know. One of the reasons to avoid GUIs.

If you need two arguments (to compare two files for example) you would use "%1" "%2".
Similar (or perhaps exactly the same) as in a *.bat - file.
You know *.bat-files?

There is a pattern for 'all arguments' - I'm not sure if it is "%*".
Look at your windows-manual.

I left windows years ago, so I cannot test for the details.

If you use a c/c++ - Wrapper, you have the same problem, only one additional layer between the OS and your application.

If you found out the details, please correct me, because the question comes up from time to time, and very few of the windows-programers know their OS.

Ask linux-questions, and you get tested answers.
[ August 24, 2004: Message edited by: Stefan Wagner ]
Stefan Wagner
Ranch Hand

Joined: Jun 02, 2003
Posts: 1923

Did it work?
Yesterday, I had to work on a WinXP machine, and the extension-application coupling seems to be changed.
No possibility to specify parameters, no possibility to create an alias like 'open', 'excecute', and the everlooved 'foobar' to a way to open a file...

Is it hidden in wXP, or is it gone in general?
Do you have to edit the registry?

How do you handle arguments in wxp?
Not at all?
And the brave windows-users are sitting silently around?
[ August 25, 2004: Message edited by: Stefan Wagner ]
Matthew Middleton
Greenhorn

Joined: May 05, 2004
Posts: 23
Here's where i am at right now, your code for opening into by typing java -jar jarfile.jar openfile.txt worked, if i type that in the cmd prompt and they are in the same directory it works. now, where do i go from here, i had a couple of ideas but they seem a little bit of a hassle. One would be to write a small app that would write that into the cmd prompt, and create a temp .jar file so they are in the same directory. My friend suggested messing with the registry. I'll be honest, i have no idea how to do that, but i would love to learn. Any tutorials or webpages would be amazingly welcome. Thank you for everything and GOD Bless
Matthew Middleton
Greenhorn

Joined: May 05, 2004
Posts: 23
Batch files!!! Haha, I just made a batch file that called the program.
java -jar C:/j2sdk1.4.2_01/bin/Organizer.jar %1. Then just make it where the files are associated w/the batch file. It does call any annoying cmd window behind the app. Now to find out if i can find a way to get rid of that window in the back ground. Thank you agian for all of your help and GOD Bless.
[ August 27, 2004: Message edited by: Matthew Middleton ]
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: File extensions
 
Similar Threads
HelloWorld.java.txt
I/O and streams
What type of java object is this?
want to create a new file extension that can only be opened by creator program
pdc viewer software needed