将文件扩展名与 Java 应用程序关联
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/22573064/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Associate File Extension with Java Application
提问by Ploug
Here is a picture of the 2 files in question, one .atb and one .jar
这是有问题的 2 个文件的图片,一个 .atb 和一个 .jar
If i just click the jar file, it opens my program smoothly, no questions asked. If i click the New Text Document and choose "y" as my default program, it says this:
如果我只是单击 jar 文件,它会顺利打开我的程序,没有任何问题。如果我单击“新建文本文档”并选择“y”作为我的默认程序,它会显示:
If i do all this with .txt as file extension it says the same, still doesn't work.
如果我用 .txt 作为文件扩展名做这一切,它说同样的,仍然不起作用。
If i do all this in Windows 7 with the same setup, it all works fine.
如果我使用相同的设置在 Windows 7 中执行所有这些操作,则一切正常。
Also i checked my event logs when this occurs and this pops up as a keyword "Audit Success" with the text: "An attempt was made to query the existence of a blank password for an account."
发生这种情况时,我还检查了我的事件日志,这会以关键字“审核成功”的形式弹出,文本为:“尝试查询帐户是否存在空白密码。”
Do you have any idea what might cause this?
你知道什么可能导致这种情况吗?
采纳答案by vallentin
You can't associate file extensions to trigger a .jar
file on Windows. The only file types you can trigger on Windows are .exe
, .pif
, .com
, .bat
, .cmd
, so instead of triggering the .jar
file, you can trigger a .bat
file, which then launches the .jar
file.
您不能关联文件扩展名来触发.jar
Windows 上的文件。您可以在 Windows 上触发的唯一文件类型是.exe
, .pif
, .com
, .bat
, .cmd
,因此.jar
您可以触发一个.bat
文件,然后启动该.jar
文件,而不是触发文件。
Create a y.bat
file and place it next to your y.jar
file and write the following code inside it:
创建一个y.bat
文件并将其放在您的y.jar
文件旁边,并在其中写入以下代码:
@echo off
title y
start javaw -jar "C:\Users\SomeUsername\Desktop\y.jar" %1
You can change the title
and y.jar
path as you please, just remember that the path need to be the absolute path. Though the real keyword here is %1
, that is the actual path, of the file you clicked.
您可以随意更改title
和y.jar
路径,请记住路径必须是绝对路径。虽然这里真正的关键字是%1
,这是您单击的文件的实际路径。
You can get the value of any parameter using a % followed by it's numerical position on the command line. The first item passed is always %1 the second item is always %2 and so on
%* in a batch script refers to all the arguments (e.g. %1 %2 %3 %4 %5 ...%255)
您可以使用 % 后跟它在命令行上的数字位置来获取任何参数的值。传递的第一项始终为 %1,第二项始终为 %2,依此类推
批处理脚本中的 %* 指的是所有参数(例如 %1 %2 %3 %4 %5 ...%255)
Now you can simply right-click on any .abt
file and press "Open With...", remember to check "Use this app for all .abt files" and then just browse for the y.bat
and click "Open". Now each time you double-click a .abt
file it will launch your .jar
program.
现在您只需右键单击任何.abt
文件并按“打开方式...”,记得选中“将此应用程序用于所有 .abt 文件”,然后浏览y.bat
并单击“打开”。现在,每次双击.abt
文件时,它都会启动您的.jar
程序。
Additionally I've written this post (Associate File Extension with Java Application)after writing this answer.
另外,我在写完这个答案后写了这篇文章(Associate File Extension with Java Application)。
回答by FunkyColorado
I had a silly issue with a .JAR opening. After running "java -jar MyApp.jar", I came to the realization that it was not launching the gui as my preferences file was not in the same path as the .jar file.
我在打开 .JAR 时遇到了一个愚蠢的问题。运行“java -jar MyApp.jar”后,我意识到它没有启动 gui,因为我的首选项文件与 .jar 文件不在同一路径中。
Adding a check for if(!file)then create new file... the app worked as expected and became easier to port/share.
添加对 if(!file) 的检查,然后创建新文件...该应用程序按预期工作,并且更容易移植/共享。