Page 1 of 2 12 LastLast
Results 1 to 20 of 22
  1. #1
    totalspelnerd is offline Member
    Join Date
    Mar 2012
    Location
    Sweden
    Posts
    88
    Rep Power
    0

    Default Open a file with my application

    I've been looking around quite some time now for a thread where someone wants to open a special file (for example *.png) with my own application. And I do not mean Using a file chooser and using the Desktor.getDesktop().open(File) method. I mean double clicking on a png file and making it launch my application and doing something with the image. Just like GIMP,paint.net and notepad++ (and so on) does. I've heard that this is platform dependent, but I only need it for myself, I'm running windows 7. I know how to set the default opening program but how do I get the path to the file I'm trying to open. Can I get this information in the String[] args parameter from the main method somehow.

    I might just do the Google searching thing wrong (using the wrong words for what I'm trying to do), if so, what exactly should I try to search for.

    Thanks for any help!

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,397
    Rep Power
    25

    Default Re: Open a file with my application

    double clicking on a png file and making it launch my application
    That is handled by the OS. Windows has registry entries that list the commandline to be used to Open a file.
    A common one for java is for the .jar file. From a registry export:
    \"C:\\Program Files\\Java\\jre6\\bin\\javaw.exe\" -jar \"%1\" %*"


    The command line args are passed to the String[] in the main() method.
    Last edited by Norm; 11-25-2013 at 09:04 PM.
    If you don't understand my response, don't ignore it, ask a question.

  3. #3
    totalspelnerd is offline Member
    Join Date
    Mar 2012
    Location
    Sweden
    Posts
    88
    Rep Power
    0

    Default Re: Open a file with my application

    Thanks for the quick response

    \"C:\\Program Files\\Java\\jre6\\bin\\javaw.exe\" -jar \"%1\" %*"
    Could you go though a little bit more on what everything here is?
    what is \"%1\" %*" ?

    How do I use this commandline?

  4. #4
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,397
    Rep Power
    25

    Default Re: Open a file with my application

    How do I use this commandline?
    Write a small test program that prints out the array passed to main:
    System.out.println("an ID "+ java.util.Arrays.toString(theArrayName));
    Execute the program with a command line and some args on the commandline and see what is printed by the above.


    What I posted was a "command" for a .jar file from my Windows registry. Use the regedit program to look at your registry.
    If you don't understand my response, don't ignore it, ask a question.

  5. #5
    totalspelnerd is offline Member
    Join Date
    Mar 2012
    Location
    Sweden
    Posts
    88
    Rep Power
    0

    Default Re: Open a file with my application

    You mean typing the command line like this:

    java -jar *PathToJar* arg1 arg2
    correct?
    This types out "an ID [arg1, arg2]"
    But what help is this?
    What I need help with is how I can parse the file path that I'm double clicking into the command line / batch file or however this is done.

  6. #6
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,397
    Rep Power
    25

    Default Re: Open a file with my application

    If you know how to have the OS call your program when a file is double clicked, chose an unused extension for testing (for example: atst) and add an entry to the registry that will execute the command line something like this:
    java YourClassName "%1"

    When YourClassName is executed it should print out the contents of the String[] array so you can see what is passed to your class. The console window will close quickly so your code should call a read method to block the exit or
    use a JOptionPane method to show what's in the array.
    If you don't understand my response, don't ignore it, ask a question.

  7. #7
    totalspelnerd is offline Member
    Join Date
    Mar 2012
    Location
    Sweden
    Posts
    88
    Rep Power
    0

    Default Re: Open a file with my application

    But how do I set it to the path to the class if it is inside a jar file?
    If I just set it to the main class in the project\bin\Main.class I get an error:
    Error: could not find or load main class ...project\bin\Main.class
    It has a main method so I have no idea what is happening here.

  8. #8
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,397
    Rep Power
    25

    Default Re: Open a file with my application

    In post #2 I posted a command line entry from my registry that called a class in a jar file and passed it the path to the file that was doubled clicked.

    If you get errors, you need to copy the full contents of the command prompt window and pasted it here.
    If you don't understand my response, don't ignore it, ask a question.

  9. #9
    totalspelnerd is offline Member
    Join Date
    Mar 2012
    Location
    Sweden
    Posts
    88
    Rep Power
    0

    Default Re: Open a file with my application

    I have this inside my Main.class file:
    Java Code:
    import javax.swing.JOptionPane;
    
    public class Main{	
    	public static void main(String[] args) {
    		JOptionPane.showMessageDialog(null, "an ID "+ java.util.Arrays.toString(args));
    	}
    }
    In my command prompt I have this right now:

    C:\Users\User> java E:\pgrm\Java\OpenWith\bin\Main.class arg1 arg2
    Error: Could not find or load main class E:\pgrm\Java\OpenWith\bin\Main.class



    I have found the regedit thing and the extension .atst (inside HKEY_CLASS_ROOT) but when I add the code I typed into the command prompt (java E:\pgrm\Java\OpenWith\bin\Main.class "%1") I just get an error like "*.atst is not a valid Win32-application." <-- Translated that from my language might not be 100% correct.

    The path to the file is correct and the file exists and have a main method.

    Do you need any more information?

  10. #10
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,397
    Rep Power
    25

    Default Re: Open a file with my application

    C:\Users\User> java E:\pgrm\Java\OpenWith\bin\Main.class arg1 arg2
    Error: Could not find or load main class
    The java command takes the name of the class, not the name of the file. Leave off the .class
    If you don't understand my response, don't ignore it, ask a question.

  11. #11
    totalspelnerd is offline Member
    Join Date
    Mar 2012
    Location
    Sweden
    Posts
    88
    Rep Power
    0

    Default Re: Open a file with my application

    It still gives me the same error as before though. I did not change the path or anything, just removed the .class extension.

  12. #12
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,397
    Rep Power
    25

    Default Re: Open a file with my application

    Please copy the full contents of the command prompt window and paste it here.

    Can you change directories so the window is in the same folder with the class file and try the java command there?
    Do a dir command to show what is in the folder before doing the java command.
    If you don't understand my response, don't ignore it, ask a question.

  13. #13
    totalspelnerd is offline Member
    Join Date
    Mar 2012
    Location
    Sweden
    Posts
    88
    Rep Power
    0

    Default Re: Open a file with my application

    Okay, so changing the directory to the E:\pgrm...\bin and typing java Main worked.

    E:\pgrm\Java\OpenWith\bin>java Main arg1 arg2

    It starts up the file main (Opening the JOptionPane with text "an ID [arg1, arg2]).

    But where do I go from here?
    How do I get to the path and performing the code in one line in the regedit thing?

  14. #14
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,397
    Rep Power
    25

    Default Re: Open a file with my application

    Here is an entry I exported from my registry with some sample commandlines. It is for executing a jar file when a file with the extension: NEDP is double clicked.
    Windows Registry Editor Version 5.00

    [HKEY_CLASSES_ROOT\NormsEDPFile]
    "EditFlags"=dword:00000000
    "BrowserFlags"=dword:00000008
    @="Norms Enc-Dec File"

    [HKEY_CLASSES_ROOT\NormsEDPFile\shell]
    @="Open"

    [HKEY_CLASSES_ROOT\NormsEDPFile\shell\Open]

    [HKEY_CLASSES_ROOT\NormsEDPFile\shell\Open\command]
    @="javaw.exe -jar D:\\JavaDevelopment\\runtime\\NormsEncDec.jar \"%1\""

    [HKEY_CLASSES_ROOT\NormsEDPFile\shell\Open_with_win dow]

    [HKEY_CLASSES_ROOT\NormsEDPFile\shell\Open_with_win dow\command]
    @="java.exe -jar D:\\JavaDevelopment\\runtime\\NormsEncDec.jar \"%1\""
    The OS substitutes the file's path for the %1 entry in the command line. Similar to what goes in a batch file.
    If you don't understand my response, don't ignore it, ask a question.

  15. #15
    totalspelnerd is offline Member
    Join Date
    Mar 2012
    Location
    Sweden
    Posts
    88
    Rep Power
    0

    Default Re: Open a file with my application

    So I've done some changes to your file and now I've got this:

    Windows Registry Editor Version 5.00

    [HKEY_CLASSES_ROOT\.atst]
    @="A Test File"
    "BrowserFlags"=dword:00000008
    "EditFlags"=dword:00000000

    [HKEY_CLASSES_ROOT\.atst\shell]
    @="Open"

    [HKEY_CLASSES_ROOT\.atst\shell\Open]

    [HKEY_CLASSES_ROOT\.atst\shell\Open\command]
    @="javaw.exe -jar C:\Users\Tim\Desktop\Programs\openWith.jar "%1""

    [HKEY_CLASSES_ROOT\.atst\shell\Open_with_win dow]

    [HKEY_CLASSES_ROOT\.atst\shell\Open_with_win dow\command]
    @="java.exe -jar C:\Users\Tim\Desktop\Programs\openWith.jar "%1""

    I've imported it and when I double click the *.atst file it just says that *.atst is an invalid Win32 application, still. The paths are all right and all so that is not the problem

  16. #16
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,397
    Rep Power
    25

    Default Re: Open a file with my application

    did you make an entry in the registry for the .atst extension that points to the registry entry that you show in the last post? This is my entry for .NEDP
    Windows Registry Editor Version 5.00

    [HKEY_CLASSES_ROOT\.NEDP]
    @="NormsEDPFile"
    If you don't understand my response, don't ignore it, ask a question.

  17. #17
    totalspelnerd is offline Member
    Join Date
    Mar 2012
    Location
    Sweden
    Posts
    88
    Rep Power
    0

    Default Re: Open a file with my application

    No I didn't but it still doesn't work.
    This is what refers to the other registry entry and it should work, correct?
    Windows Registry Editor Version 5.00

    [HKEY_CLASSES_ROOT\.atst]
    @="tsnfiles"
    This is the registry entry it refers to:
    Windows Registry Editor Version 5.00

    [HKEY_CLASSES_ROOT\tsnfiles]
    @="My File"
    "BrowserFlags"=dword:00000008
    "EditFlags"=dword:00000000

    [HKEY_CLASSES_ROOT\tsnfiles\shell]
    @="Open"

    [HKEY_CLASSES_ROOT\tsnfiles\shell\Open]

    [HKEY_CLASSES_ROOT\tsnfiles\shell\Open\command]
    @="\"javaw.exe -jar C:\\\\Users\\\\Tim\\\\Desktop\\\\Programs\\\\openW ith.jar \\\"%1\\\"\""

    [HKEY_CLASSES_ROOT\tsnfiles\shell\Open_with_win dow]

    [HKEY_CLASSES_ROOT\tsnfiles\shell\Open_with_win dow\command]
    @="\"java.exe -jar C:\\\\Users\\\\Tim\\\\Desktop\\\\Programs\\\\openW ith.jar \\\"%1\\\"\""
    I do not know why it creates so many \ when I export it though. Does that have anything to do with it?

  18. #18
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,397
    Rep Power
    25

    Default Re: Open a file with my application

    I'm not a registry expect.
    Compare your exported registry entry with the one i posted. Try to make yours look like mine.
    If you don't understand my response, don't ignore it, ask a question.

  19. #19
    totalspelnerd is offline Member
    Join Date
    Mar 2012
    Location
    Sweden
    Posts
    88
    Rep Power
    0

    Default Re: Open a file with my application

    Everything is just like your code right now. One thing I've noticed is that if I change the default program to open the .atst to lets say notepad++ the registry entry isn't changed to C:\...\notepad++.exe it still says tsnfiles (the entry I'm referring it to ). It's like the regedit isn't even working like it should.

  20. #20
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,397
    Rep Power
    25

    Default Re: Open a file with my application

    There is a lot of magic in the registry. Windows has several places it stores the command line to use when opening a file.
    I don't have any idea how all that works.
    If you don't understand my response, don't ignore it, ask a question.

Page 1 of 2 12 LastLast

Similar Threads

  1. Open/Launch an XML File in Excel from Java Application
    By Redefine12 in forum New To Java
    Replies: 10
    Last Post: 09-12-2012, 09:42 AM
  2. using open source project in our application
    By rajinder5 in forum Web Frameworks
    Replies: 1
    Last Post: 09-21-2011, 04:11 PM
  3. Open Source Web Application Framework
    By ideyatech in forum Java Software
    Replies: 0
    Last Post: 04-29-2010, 07:54 AM
  4. Different Files Open in File -> Open Workspace
    By mgm2010 in forum JCreator
    Replies: 0
    Last Post: 04-11-2009, 02:14 PM
  5. Check if Windows application is already open
    By mpiaser in forum Advanced Java
    Replies: 1
    Last Post: 12-07-2007, 09:56 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •