Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 12 of 12

Thread: Associating files to java application

  1. #1
    Junior Member
    Join Date
    Apr 2011
    Location
    Manipal, India
    Posts
    7
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Question Associating files to java application

    Hey!

    I want to know if it's possible to associate files of certain types with my java app.
    for example, would it be possible for a Java-based text editor to open every time a .txt file is double-clicked, or run in some other manner?
    what i mean is that on clicking the .txt file, my application should open, and the file's uri should be passed as an argument to it.
    any help would be appreciated. even platform-specific codes like shell-scripts/batch-scripts would do.


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,067
    Thanks
    47
    Thanked 2,146 Times in 2,119 Posts

    Default Re: Associating files to java application

    Yes you can on Windows and perhaps on linux.
    This isn't related to java programming. It's an OS setup problem.

    In windows you can specify the commandline you want executed when double click on a file with a given extension. This is controlled by entries in the Registry.

    With XP you can see/change the entries by (This is for jar files, change to extension to your choice)
    Open Windows Explorer
    Click on Tools menu
    Click on Folder Options
    Select File Types tab
    Scroll down in the "Registered file types:" list until you see an entry for JAR.
    Select the JAR entry.
    Click the Advanced button
    In the Edit File Type window scroll down the list of Actions until you see the one in bold.
    Select the bold one
    Click the Edit button
    Select, Copy and paste here what is in the "Application used to perform action:" window.

    Be careful not to change anything and Cancel your way out of the open windows.

  3. #3
    Junior Member
    Join Date
    Apr 2011
    Location
    Manipal, India
    Posts
    7
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Associating files to java application

    Sorry, but probably my question is not clear enough.

    I can always right click on the file in windows, and select OPEN WITH -> MyApp.jar but how do i pass the location and name of the file to the jar file?

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,067
    Thanks
    47
    Thanked 2,146 Times in 2,119 Posts

    Default Re: Associating files to java application

    In the command line in the Registry you use a place holder like: %1 or %*
    If you do the steps that I posted in #2 you will see what a command line looks like.

    Here is a sample command line from my registry:
    java.exe -cp D:\JavaDevelopment\runtime\CheckClassRefs.jar;. CheckClassRefs "%1"

  5. The Following User Says Thank You to Norm For This Useful Post:

    divs1210 (July 16th, 2011)

  6. #5
    Junior Member
    Join Date
    Apr 2011
    Location
    Manipal, India
    Posts
    7
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Associating files to java application

    thanks! i guess i get it!

    some more help with a batch file, though:


    could you please write a batch script for me that does the following work:
    1. takes a file as an argument
    2. stores the file's directory in a variable

    eg. input:
    bfile "C:\hello\hi\tada.txt"
    output(should be saved in a variable):
    C:\hello\hi

  7. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,067
    Thanks
    47
    Thanked 2,146 Times in 2,119 Posts

    Default Re: Associating files to java application

    Sorry, this is a java programming forum. Try a forum for the OS you are working on.

  8. #7
    Junior Member
    Join Date
    Apr 2011
    Location
    Manipal, India
    Posts
    7
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Associating files to java application

    is it my fault that java runs like a freaking interpreted language?
    should've opted for c/c++, and my life would have been a LOT easier. im not asking all these questions for teh lulz. my question here is bloody related to java. i dont see how it isn't.

  9. #8
    Junior Member
    Join Date
    Apr 2011
    Location
    Manipal, India
    Posts
    7
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Associating files to java application

    look. it's 2:00 am in my country right now. kindly help me if you can. im bloody stuck here, and its getting frustrating.

  10. #9
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,067
    Thanks
    47
    Thanked 2,146 Times in 2,119 Posts

    Default Re: Associating files to java application

    write a batch script
    Batch files are strictly an OS thing.

    Java is definitely interpreted. A benefit is that the same code can run on many OSs without any changes. Copy the jar file and the program runs.

  11. #10
    Junior Member
    Join Date
    Apr 2011
    Location
    Manipal, India
    Posts
    7
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Associating files to java application

    tell me how to write the batch fle to launch my java application! i am WILLING to sacrifice portability!

    running on ONE platform is better than not running at all!

  12. #11
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,067
    Thanks
    47
    Thanked 2,146 Times in 2,119 Posts

    Default Re: Associating files to java application

    You will not need a batch file to execute a jar file. The JDK and JRE installations make the Registry entries needed to execute a jar file. Put your class files in a jar file with a correct manifest and then double click the jar file to execute it.

  13. #12
    Super Moderator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,235
    Thanks
    176
    Thanked 817 Times in 760 Posts
    Blog Entries
    5

    Default Re: Associating files to java application

    divs1210, you are posting your way out of receiving any help from anyone. Contributors spend their free, unpaid time to help give advice such as Norm has provided. I recommend you try and show a little respect and refrain from posting obscenities directed at said contributors. I have deleted your discourteous post, and am locking this thread
    Last edited by copeg; July 16th, 2011 at 04:41 PM.

Similar Threads

  1. Seraching through files in a folder for a pattern match inside the files.
    By dazzabiggs in forum What's Wrong With My Code?
    Replies: 4
    Last Post: May 2nd, 2011, 08:35 AM
  2. how to run any installed application through my java application??
    By sgsamanthjain in forum Java Theory & Questions
    Replies: 1
    Last Post: April 1st, 2011, 08:17 AM
  3. Replies: 1
    Last Post: March 22nd, 2011, 06:59 PM
  4. Replies: 1
    Last Post: January 12th, 2011, 04:55 AM
  5. Replies: 0
    Last Post: December 3rd, 2009, 03:43 PM

Tags for this Thread