简体   繁体   English

用java编写的程序双击打开文件

[英]Open a file by double click with a program written in java

I want to know if it's possible to open a file in my program that is written in java just by doing a double click on the file?我想知道是否可以通过双击文件在我的程序中打开一个用 java 编写的文件?

For example: On my desktop is a file "test.dat" that was built by my program.例如:在我的桌面上有一个由我的程序构建的文件“test.dat”。 If I try to open this file my program shows up and asks me what I want to do with that file.如果我尝试打开这个文件,我的程序就会出现并询问我想用那个文件做什么。

Is it possible to implement that feature using java?是否可以使用 java 实现该功能?

It is the operating system that decides which applications are associated with a given extension.操作系统决定哪些应用程序与给定的扩展相关联。 You may configure your OS to open all .dat files with you program if that works for you.如果适合您,您可以将操作系统配置为使用您的程序打开所有.dat文件。 Alternatively create a shortcut or a launcher telling what to use.或者,创建一个快捷方式或启动器,告诉您要使用什么。

I understand what you are asking.我明白你在问什么。 You want to know how to get the parameter passed from the OS to your application and then call your open file method.您想知道如何将参数从操作系统传递到您的应用程序,然后调用您的打开文件方法。

Your java program has a class that has a main method that is getting called to start your application.您的 java 程序有一个类,该类有一个 main 方法,该方法被调用以启动您的应用程序。 This class is listed in your manifest file as Main-Class: com.your.package.MainClass.此类在清单文件中列为 Main-Class:com.your.package.MainClass。 The method signature looks something like:方法签名类似于:

public static void main(final String args[]) {

The String array args[] contains any parameters passed to your program from the command line.字符串数组 args[] 包含从命令行传递给您的程序的任何参数。 When you tell the OS to associate a file with an executable and you then double click on the file, the OS passes the filename (full path) to the executable as the first parameter in this String array.当您告诉操作系统将文件与可执行文件相关联,然后双击该文件时,操作系统会将文件名(完整路径)作为此字符串数组中的第一个参数传递给可执行文件。 The tricky part is that you can't just associate a file extension with your jar file because the jar file isn't an executable.棘手的部分是您不能只将文件扩展名与 jar 文件相关联,因为 jar 文件不是可执行文件。 The jar file is actually associated with java.exe or javaw.exe. jar 文件实际上与 java.exe 或 javaw.exe 相关联。 So to make this work you need to create a batch file (or a shell script depending on your OS) that calls java.exe or javaw.exe, sets the class path to your jar file, runs the main class and then passes the parameter to your program.因此,要完成这项工作,您需要创建一个批处理文件(或取决于您的操作系统的 shell 脚本),它调用 java.exe 或 javaw.exe,将类路径设置为 jar 文件,运行主类,然后传递参数到你的程序。 This is how it would be done in a batch file on windows.这是在 Windows 上的批处理文件中完成的方式。

"C:\Program Files\Java\jre1.8.0_25\bin\javaw.exe" -cp C:\Path\To\Your\Jar\File.jar com.your.package.MainClass %1

Then, instead of associating your .dat file with your jar file, you would associate it with this batch file.然后,不是将 .dat 文件与 jar 文件相关联,而是将它与此批处理文件相关联。 The %1 will cause the filename to be passed to your MainClass as args[0] which you can then pass to your openFile(arg[0]) method and voila, the file is open. %1 将导致文件名作为 args[0] 传递给您的 MainClass,然后您可以将其传递给您的 openFile(arg[0]) 方法,瞧,文件已打开。 You aren't limited to just %1 either.您也不仅限于 %1。 You can have %1 %2 %3, etc. if the OS is passing multiple files to your program, for example if you have selected multiple .dat files.如果操作系统将多个文件传递给您的程序,例如您选择了多个 .dat 文件,您可以使用 %1 %2 %3 等。 This would be done in a similar manner in a Unix shell script.这将在 Unix shell 脚本中以类似的方式完成。

/usr/bin/javac -cp /Path/To/Your/Jar/File.jar com.your.package.MainClass %1

"Opening" files by double clicking is a feature of windows OS that is controlled by mapping file extension to specific program.通过双击“打开”文件是 Windows 操作系统的一项功能,通过将文件扩展名映射到特定程序来控制。

If you want to run java program packed in jar file you have to create so called "runnable" jar and map jar extension to program named java or javaw .如果要运行打包在jar文件中的 java 程序,则必须创建所谓的“可运行”jar 并将 jar 扩展名映射到名为javajavaw程序。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM