简体   繁体   English

在Windows中使用“打开方式”时,如何将文件路径传递给命令行争论?

[英]How do I pass file path to command line arguements when using “open with” in Windows?

I'm new here so I'm not sure I'm doing anything wrong so please do tell me if I am. 我是新来的,所以不确定自己做错了什么,所以请告诉我我是否在。

I'm making a simple image/gif viewer in Java. 我正在用Java创建一个简单的图像/ gif查看器。 I want to be able to use the extracted application (which I will wrap in an .exe with launch4j) to open image files. 我希望能够使用提取的应用程序(该应用程序将与launch4j包装在.exe中)来打开图像文件。 When choosing the image to open with it, I want it to get the file path of the image and pass it to args[0] so that the application can use it. 选择要打开的图像时,我希望它获取图像的文件路径并将其传递给args [0],以便应用程序可以使用它。

I'm sensing this is something that needs to be done outside of my Java code. 我感觉到这是需要在Java代码之外完成的事情。 Maybe in an installer or the wrapper I'm using but I have no idea how to do it. 也许在我正在使用的安装程序或包装程序中,但我不知道该怎么做。

I've been searching the past 3 hours to no avail. 我一直在搜索过去3小时无济于事。 Any help is appreciated. 任何帮助表示赞赏。 My code so far: 到目前为止,我的代码:

public class MainWindow {

private JFrame frame1;
public static String imageLocation = null;

public static void main(String[] args) {
        try {
            UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
        } catch (Throwable e) {
            e.printStackTrace();
        }

        try {
            imageLocation = args[0];
        } catch (Exception e1) {
            e1.printStackTrace();
            System.exit(0);
        }

        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    MainWindow window = new MainWindow();
                    window.frame1.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    public MainWindow() {
        initialize();
    }

    private void initialize() {

        BufferedImage img = null;
        try{
            img = ImageIO.read(new File(imageLocation));
        }catch (IOException e) {
            e.printStackTrace();
        }
        int imgWidth = img.getWidth();
        int imgHeight = img.getHeight();

        frame1 = new JFrame();
        frame1.getContentPane().setBackground(Color.WHITE);
        frame1.setResizable(false);
        frame1.setTitle(imageLocation);
        frame1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame1.getContentPane().setLayout(new BorderLayout(0, 0));
        frame1.setBounds(200, 200, imgWidth+18, imgHeight+40);

        JLabel imgLabel = new JLabel("");
        imgLabel.setToolTipText(imageLocation);
        imgLabel.setHorizontalAlignment(SwingConstants.CENTER);
        imgLabel.setIcon(new ImageIcon(imageLocation));
        imgLabel.setBounds(0, 0, frame1.getWidth(), frame1.getWidth());
        frame1.getContentPane().add(imgLabel);
    }
}

Thanks in advance. 提前致谢。

I don't see any form elements to upload files. 我看不到任何要上传文件的表单元素。 Not an expert in JForms but I believe you need a file upload control that will pass its value to the args array in your code. 不是JForms的专家,但我相信您需要一个文件上传控件,该控件会将其值传递给代码中的args数组。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在Java中,如何找到打开以命令行方式提供的名称的txt文件的正确路径? - In Java, how do I find the proper path to open a txt file that's name is provided as a command line arguement? 如何通过 Windows 命令提示符使用 Java Keytool 指定现有 SSL 证书的文件路径? - How do I specific a file path to an existing SSL certificate using Java Keytool via Windows Command Prompt? Java命令行参数 - Java Command Line Arguements 无法使用Java程序执行jar文件。 我需要将文件路径作为命令行参数传递给jar文件 - unable to execute the jar file using java program. i need to pass the file path as a command line argument to the jar file 如何使用命令行在Windows上找到JRE路径 - How to find the JRE path on Windows using command line 有一个带有文件夹路径的字符串,如何将其转换为File以便能够使用getDesktop.open()打开它? - Having a string with a path to a folder, how do I convert it into File to be able to open it using getDesktop.open()? 如何在 Windows 命令行中为 Android Studio 生成密钥库签名的 a.jpk 签名文件? - How do I generate a .jpk signature file for keystore signing for Android Studio in Windows Command Line? 使用Java执行Windows命令行命令 - Using Java to do a WIndows command line command 如何在单独的文件中传递Java命令行选项? - How can I pass Java command line options in a separate file? 如何在Windows 7命令行上执行.jar java程序? - How do I execute .jar java program on Windows 7 command line?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM