Windows has "Open With" functionality

alt text

I'm wondering, is it possible to create an analogue of "Open With" in Java? Suppose we have a file.

  • You need to get a list of "recommended" programs to open the file.
  • It is necessary to leave the possibility to the user to choose an arbitrary program.
  • Otherwise, suggest the user to download the program via the Internet.
  • Attempt to open the file (as specified by the user).

    4 answers 4

    Cross-platform is / was a JDIC project - but the last commit they had there was a year ago and the download page is empty.

    To call directly under Windows, you can use the function from shell32.dll , calling it from the command line or via JNI . In the simplest case:

     Runtime.getRuntime().exec("rundll32.exe shell32.dll,OpenAs_RunDLL c:\\file.txt"); 

      For Windows you can create from exe Java program. So users will be much easier. The Internet is full of programs that make Java programs exe -shniki. There was already a discussion.

      • I do not need to create an exe-shnik. - angry
      • Then I think that it can be set in the settings of Windows. After all, it is she who decides how to open any file types. And another idea, in which direction you can skip: When installing any program, the installer somehow sets a rule for Windows with which program to open a certain file resolution. - Anton Mukhin
      • Anton, you probably did not understand the question. The author wants to open a similar window in his Java program. @ Zohan, right? - yozh
      • Exactly. In my program I work with files. - angry

      I myself just recently started to learn it, the code below works:

       JFileChooser chooser = new JFileChooser(); // Note: source for ExampleFileFilter can be found in FileChooserDemo, // under the demo/jfc directory in the Java 2 SDK, Standard Edition. ExampleFileFilter filter = new ExampleFileFilter(); filter.addExtension("jpg"); filter.addExtension("gif"); filter.setDescription("JPG & GIF Images"); chooser.setFileFilter(filter); int returnVal = chooser.showOpenDialog(parent); if(returnVal == JFileChooser.APPROVE_OPTION) { System.out.println("You chose to open this file: " + chooser.getSelectedFile().getName()); } 

      I'm sorry, I can't chew on what I'm responsible for, because I don't know yet, but I think there will be guys who can.

      • @arachnoden, this is not at all. Look at the answer of @yozh. - angry

      this is not exactly what you need, but it may be useful:

      Create a Test.java file with this content:

      public class Test { public static void main (String []args) { System.out.println("hello world"); } }

      1) create a binding .java file to some program for example. eclipse
      1.1) for this purpose in the explorer we choose to open with the help ... 1.2) we specify the full path to the program and put a tick to remember (so that it always opens with this program)

      2.1) run regedit
      2.2) open the HKEY_CLASSES_ROOT\.java branch
      2.3) we look at the value of the key (Default), I have it 'java_auto_file'
      2.4) we are looking for 'java_auto_file' in the same thread, for me this key has approximately the following contents

       java_auto_file-->shell-->open-->command: (Default)="D:\programming\java\eclipse12\eclipse.exe" "%1" 

      3) Create your own line in the context menu
      3.1) Create another branch with the same content:

      java_auto_file-->shell-->Compile-->command (Default)="D:\programming\java\eclipse12\eclipse.exe" "%1"

      3.2) Replace the path to the program with the path to the java compiler:

      (Default)="C:\Program Files\Java\jdk1.7.0\bin\javac.exe" "%1"

      4) compile the class
      4.1) in the explorer from the context menu of our file, select the item "compile" (the one we added)
      4.2) an existing open item can be deleted if desired

      similarly for the .class file, the default action can be made to start it