• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Rob Spoor
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Henry Wong
  • Liutauras Vilda
  • Jeanne Boyarsky
Saloon Keepers:
  • Jesse Silverman
  • Tim Holloway
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Mikalai Zaikin
  • Piet Souris

Start custom java application from file

 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hellow!

I am trying to implement Java application. Lets say i have a file "file.txt.r". Now i want to open that file in windows with my custom Java application. I set default program of "file.txt.r" to javaApp.jar. What kinda code must be writen in "javaApp.jar" to start "javaApp.jar" and execute code (lets say to read the content of "file.txt.r". Note that "file.txt.r" doesn't have to be text file).

Basically i want to start my java application with double click on different files what has ".r" extension

 
Marshal
Posts: 22444
121
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to the Ranch!

You will need to add a file association that will open any file with extension r with the following:
- java -jar <jarfile> "%1" for a command line application that's put into a JAR file
- java -classpath <mainClass> "%1" for a command line application that's stored as loose class files
- javaw -jar <jarfile> "%1" for a windowed application that's put into a JAR file
- javaw -classpath <mainClass> "%1" for a windowed application that's stored as loose class files
Replace <jarfile> and <mainClass> with your actual JAR file / main class name; you may need to add the full path to java / javaw

In your application, all you need to do is check the args argument to the main method. With the above file associations, the "%1" will pass the opened file as one argument to the JVM, which will then show up as args[0]. The quotes are required to prevent the JVM receiving multiple arguments if the file contains a space; that's an OS limitation, not a Java limitation. Also, be sure to make sure that args.length > 0 before accessing args[0] or you will get an ArrayIndexOutOfBoundsException if no file is given.
 
J Fae
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Tnx for the answer.
I also found another solution.... I wrote batch file (start JavaLauncher.jar %~f1). files ".r" start that batch file and batch file start "jar" file and pass file path as an argument.

 
Rob Spoor
Marshal
Posts: 22444
121
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That has one big drawback - the batch file will launch a command prompt, although only briefly.
 
Joel Salatin has signs on his property that say "Trespassers will be Impressed!" Impressive tiny ad:
Thread Boost feature
https://coderanch.com/t/674455/Thread-Boost-feature
reply
    Bookmark Topic Watch Topic
  • New Topic