列出可以在java中打开特定文件类型的所有已安装程序

列出可以在java中打开特定文件类型的所有已安装程序,java,Java,是否可以从Java操作系统中获取可以打开特定文件类型的所有应用程序的列表 例如,列出可以打开.txt或.pdf文件的所有应用程序。您应该检查JFace Eclipse插件的示例,如下所示: import org.eclipse.swt.swt; 导入org.eclipse.swt.events.*; 导入org.eclipse.swt.layout.*; 导入org.eclipse.swt.program.program; 导入org.eclipse.swt.widgets.*; /** *此类

是否可以从Java操作系统中获取可以打开特定文件类型的所有应用程序的列表


例如,列出可以打开.txt或.pdf文件的所有应用程序。

您应该检查JFace Eclipse插件的示例,如下所示:

import org.eclipse.swt.swt;
导入org.eclipse.swt.events.*;
导入org.eclipse.swt.layout.*;
导入org.eclipse.swt.program.program;
导入org.eclipse.swt.widgets.*;
/**
*此类显示系统上的扩展及其相关程序。
*/
公开课表演节目{
/**
*运行应用程序
*/
公开募捐{
显示=新显示();
外壳=新外壳(显示);
shell.setText(“显示程序”);
createContents(shell);
shell.open();
而(!shell.isDisposed()){
如果(!display.readAndDispatch()){
display.sleep();
}
}
display.dispose();
}
/**
*创建主窗口的内容
* 
*@param shell打开主窗口
*/
私有void createContents(Shell){
shell.setLayout(新的GridLayout(2,false));
//为扩展创建标签和组合
新标签(shell,SWT.NONE).setText(“扩展名:”);
Combo extensionsCombo=新组合(shell,SWT.BORDER | SWT.READ|ONLY);
extensionsCombo.setLayoutData(新的GridData(GridData.FILL_HORIZONTAL));
//创建标签和
新标签(shell,SWT.NONE).setText(“程序:”);
最终标签程序名=新标签(外壳,SWT.NONE);
programName.setLayoutData(新的GridData(GridData.FILL_HORIZONTAL));
//用系统上的扩展名填充组合
String[]extensions=Program.getExtensions();
for(int i=0,n=extensions.length;i0){
字符串programName=list.getSelection()[0];
程序=(程序)list.getData(程序名);
program.execute(dataFile.getText());
}
}
});
//让他们使用启动而不是执行
按钮启动=新按钮(外壳,SWT.PUSH);
数据=新的GridData(GridData.FILL_HORIZONTAL);
data.horizontalSpan=2;
launch.setLayoutData(数据);
setText(“使用Program.launch()而不是Program.execute()”;
launch.addSelectionListener(新建SelectionAdapter(){
公共无效widgetSelected(SelectionEvent事件){
//使用发射
Program.launch(dataFile.getText());
}
});
}
/**
*应用程序入口点
* 
*@param指定命令行参数
*/
公共静态void main(字符串[]args){
新建ShowPrograms().run();
}
}

你在使用什么操作系统?如果我理解正确,你想要检索的信息在任何地方都不存在。我没有尝试过,但我认为你可以用JFace来做。嗯,这看起来很有希望。@luanjot如果你在回答中指出这一点,我可以接受。@Muhammad,luanjot:这段代码的核心是
org.eclipse.swt.program.program
类,它提供了获取扩展及其相关程序的API。也就是说,此解决方案需要SWT(或者您查看程序类的实现)。
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.*;
import org.eclipse.swt.layout.*;
import org.eclipse.swt.program.Program;
import org.eclipse.swt.widgets.*;

/**
* This class shows the extensions on the system and their associated programs.
*/
public class ShowPrograms {



    /**
    * Runs the application
    */
    public void run() {
        Display display = new Display();
        Shell shell = new Shell(display);
        shell.setText("Show Programs");
        createContents(shell);
        shell.open();
        while (!shell.isDisposed()) {
            if (!display.readAndDispatch()) {
                display.sleep();
            }
        }
        display.dispose();
    }

    /**
    * Creates the main window's contents
    * 
    * @param shell the main window
    */
    private void createContents(Shell shell) {
        shell.setLayout(new GridLayout(2, false));

        // Create the label and combo for the extensions
        new Label(shell, SWT.NONE).setText("Extension:");
        Combo extensionsCombo = new Combo(shell, SWT.BORDER | SWT.READ_ONLY);
        extensionsCombo.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));

        // Create the label and the
        new Label(shell, SWT.NONE).setText("Program:");
        final Label programName = new Label(shell, SWT.NONE);
        programName.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));

        // Fill the combo with the extensions on the system
        String[] extensions = Program.getExtensions();
        for (int i = 0, n = extensions.length; i < n; i++) {
            extensionsCombo.add(extensions[i]);
        }

        // Add a handler to get the selected extension, look up the associated
        // program, and display the program's name
        extensionsCombo.addSelectionListener(new SelectionAdapter() {
            public void widgetSelected(SelectionEvent event) {
                Combo combo = (Combo) event.widget;

                // Get the program for the extension
                Program program = Program.findProgram(combo.getText());

                // Display the program's name
                programName.setText(program == null ? "(None)" : program.getName());
            }
        });

        // Create a list box to show all the programs on the system
        List allPrograms = new List(shell, SWT.SINGLE | SWT.BORDER | SWT.H_SCROLL
            | SWT.V_SCROLL);
        GridData data = new GridData(GridData.FILL_BOTH);
        data.horizontalSpan = 2;
        allPrograms.setLayoutData(data);

        // Put all the known programs into the list box
        Program[] programs = Program.getPrograms();
        for (int i = 0, n = programs.length; i < n; i++) {
            String name = programs[i].getName();
            allPrograms.add(name);
            allPrograms.setData(name, programs[i]);
        }

        // Add a field for a data file
        new Label(shell, SWT.NONE).setText("Data File:");
        final Text dataFile = new Text(shell, SWT.BORDER);
        dataFile.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));

        // Double-clicking a program in the list launches the program
        allPrograms.addMouseListener(new MouseAdapter() {
            public void mouseDoubleClick(MouseEvent event) {
                List list = (List) event.widget;
                if (list.getSelectionCount() > 0) {
                    String programName = list.getSelection()[0];
                    Program program = (Program) list.getData(programName);
                    program.execute(dataFile.getText());
                }
            }
        });

        // Let them use launch instead of execute
        Button launch = new Button(shell, SWT.PUSH);
        data = new GridData(GridData.FILL_HORIZONTAL);
        data.horizontalSpan = 2;
        launch.setLayoutData(data);
        launch.setText("Use Program.launch() instead of Program.execute()");
        launch.addSelectionListener(new SelectionAdapter() {
          public void widgetSelected(SelectionEvent event) {
              // Use launch
              Program.launch(dataFile.getText());
          }
        });


    }



    /**
    * The application entry point
    * 
    * @param args the command line arguments
    */
    public static void main(String[] args) {
        new ShowPrograms().run();
    }
}