简体   繁体   English

如何通过单击Windows上的图标来运行Java程序?

[英]How to run Java programs by clicking on their icon on Windows?

I have written a Java program that uses Java swing library. 我编写了一个使用Java swing库的Java程序。 Now I would like to execute this program by double clicking on the executable file on Windows just like any other program with a GUI. 现在我想通过双击Windows上的可执行文件来执行此程序,就像任何其他带有GUI的程序一样。 How do I do that? 我怎么做?

Since it is Java based and has a GUI, the obvious answer is to deploy it using Java Web Start . 由于它是基于Java的并且具有GUI,因此显而易见的答案是使用Java Web Start部署它。

Java Web Start (JWS) is the Oracle Corporation technology used to launch rich client (Swing, AWT, SWT) desktop applications directly from a network or internet link. Java Web Start(JWS)是Oracle Corporation用于直接从网络或Internet链接启动富客户端(Swing,AWT,SWT) 桌面应用程序的技术 It offers 'one click' installation for platforms that support Java. 它为支持Java的平台提供“一键式”安装。

JWS provides many appealing features including, but not limited to, splash screens, desktop integration, file associations, automatic update (including lazy downloads and programmatic control of updates), partitioning of natives & other resource downloads by platform, architecture or Java version, configuration of run-time environment (minimum J2SE version, run-time options, RAM etc.), easy management of common resources using extensions.. JWS提供了许多吸引人的功能,包括但不限于启动屏幕, 桌面集成,文件关联,自动更新(包括延迟下载和程序控制更新),按平台,体系结构或Java版本,配置对本机和其他资源下载进行分区运行时环境(最小J2SE版本,运行时选项,RAM等),使用扩展轻松管理公共资源。

By 'desktop integration' read desktop shortcuts and menu items on supported platforms. 通过“桌面集成”,在支持的平台上读取桌面快捷方式和菜单项。

Windows 7上的桌面图标

The 2 icons on the right ( JotPad & Star Zoom Animation) are both Java based apps., installed using Java Web Start. 右侧的2个图标( JotPad和Star Zoom Animation)都是基于Java的应用程序,使用Java Web Start安装。 Since JotPad is sand-boxed, the user will be prompted as to whether to create the shortcut. 由于JotPad是沙盒,因此将提示用户是否创建快捷方式。 That choice is not offered for apps. 应用程序不提供该选择。 with higher permission levels, so it would make more sense to install/remove the shortcuts and menu items using the IntegrationService - which allows an app. 具有更高的权限级别,因此使用IntegrationService安装/删除快捷方式和菜单项会更有意义 - 它允许应用程序。 (after prompting the user) to create/remove them at run-time. (在提示用户之后)在运行时创建/删除它们。

There are number of options: 有很多选择:

  1. Create an executable jar of your project. 创建项目的可执行jar。 for this jar to work you have to have javaw as default application to open it. 要使这个jar工作,你必须将javaw作为默认应用程序来打开它。
  2. Create an exe of your project. 创建项目的exe。
  3. Create a bat file which runs your jar file. 创建一个运行jar文件的bat文件。

Take a look at this: How can I convert my Java program to an .exe file? 看看这个: 我如何将我的Java程序转换为.exe文件?

you need to create exe from the java program. 你需要从java程序创建exe。

Creating executable jar files 创建可执行jar文件

  1. First, make sure you have installed Java 1.2 or above. 首先,确保已安装Java 1.2或更高版本。 This facility is not available in previous versions of Java. 此工具在以前版本的Java中不可用。
  2. Next, create your working java system. 接下来,创建您的java系统。 In general, you will want to put it into a package. 通常,您需要将其放入包中。 For this example, I created a trivial HelloWorld application that prints out "Hello World" plus the first command line argument, and placed it into the package "psae". 在本例中,我创建了一个简单的HelloWorld应用程序,它打印出“Hello World”加上第一个命令行参数,并将其放入包“psae”中。 Therefore, the HelloWorld files (HelloWorld.class, HelloWorld.java) were located in the directory psae. 因此,HelloWorld文件(HelloWorld.class,HelloWorld.java)位于目录psae中。 I tested the system to make sure it worked before going on to the next step. 我测试了系统以确保它在下一步之前有效。
  3. In the directory in which the psae is located, created a file called "mainClass". 在psae所在的目录中,创建了一个名为“mainClass”的文件。 This file contains a single line specifying where the main Class is to be found in the jar file. 此文件包含一行,指定在jar文件中找到主类的位置。 Note that I use the package specification. 请注意,我使用的是包规范。 Here is the single line: Main-Class: psae.HelloWorld Note: make sure you type a carriage return after this line; 这是单行:Main-Class:psae.HelloWorld注意:确保在此行之后键入回车符; some windows systems need it and will report a "Failed to load Main-Class manifest attribute" error. 某些Windows系统需要它并将报告“无法加载Main-Class清单属性”错误。
  4. Next, I create a jar file called psae.jar using the "jar" command in Java2. 接下来,我使用Java2中的“jar”命令创建一个名为psae.jar的jar文件。 I use the "m" command line argument to specify the manifest file mainClass, which adds information to the jar file on where the main class will be found. 我使用“m”命令行参数来指定清单文件mainClass,它将信息添加到将在其中找到主类的jar文件中。 Here is the jar command: bertha:~ > jar cmf mainClass psae.jar psae 这是jar命令:bertha:〜> jar cmf mainClass psae.jar psae
  5. Just for fun, and to check what's happened, I print the table of contents for the jar file I just created. 只是为了好玩,并检查发生了什么,我打印我刚刚创建的jar文件的目录。 Here's the command and its result: bertha:~ > jar tf psae.jar META-INF/ META-INF/MANIFEST.MF psae/ psae/HelloWorld.java psae/HelloWorld.class 这是命令及其结果:bertha:〜> jar tf psae.jar META-INF / META-INF / MANIFEST.MF psae / psae / HelloWorld.java psae / HelloWorld.class
  6. Having successfully created the jar file, I can now invoke java2 on it with the command line argument: 成功创建了jar文件之后,我现在可以使用命令行参数在其上调用java2:

     bertha:~ > java -jar psae.jar Philip Hello World Philip 

you can use something like Launch4j . 你可以使用像Launch4j这样的东西 also look at JSMooth . 还看看JSMooth

Hope it helps 希望能帮助到你

There are two ways. 有两种方法。 Both involve packaging your code in a .jar. 两者都涉及将代码打包在.jar中。

The first way is to build an actual .exe file using a tool like Launch4j . 第一种方法是使用Launch4j之类的工具构建一个实际的.exe文件。 It will require you to set up things like tell it which class to execute, which icon to use, which JRE is OK, what JRE parameters to use, etc. 它将要求您设置诸如告诉它要执行哪个类,使用哪个图标,哪个JRE正常,要使用哪些JRE参数等等。

The second option is to make the .jar itself executable. 第二个选项是使.jar本身可执行。 You do this by adding a manifest to the .jar. 您可以通过向.jar添加清单来完成此操作。 The manifest is a small configuration file that describes the jar. 清单是一个描述jar的小配置文件。 One of the attributes is Main-Class which defines the entry point. 其中一个属性是Main-Class ,它定义了入口点。 In other words, it says which class has the main function that should be called when the user double-clicks the file. 换句话说,它说当用户双击文件时哪个类具有应该调用的main函数。 Here's a basic tutorial about manifests: http://docs.oracle.com/javase/tutorial/deployment/jar/manifestindex.html 这是关于清单的基本教程: http//docs.oracle.com/javase/tutorial/deployment/jar/manifestindex.html

The 2nd option is easier to get going, but users will know what to do with a .exe far more often. 第二个选项更容易上手,但用户会更频繁地知道如何处理.exe。

Note that if either approach complains that it can't find the class, make sure to set the classpath manifest attribute to match your project. 请注意,如果任一方法都抱怨它找不到类,请确保设置classpath清单属性以匹配您的项目。

While the others mention excellent choices like creating a native executable, there is another useful method: creating a shortcut. 虽然其他人提到了创建本机可执行文件等优秀选择,但还有另一种有用的方法:创建快捷方式。

  • Right click your desktop, expand the "New" option, and click on "Shortcut". 右键单击桌面,展开“新建”选项,然后单击“快捷方式”。
  • Type "javaw.exe". 输入“javaw.exe”。 Click next. 点击下一步。
  • Name it whatever you want. 无论你想要什么,都可以命 Click done. 点击完成。
  • You'll notice the newly created shortcut on your desktop. 您会注意到桌面上新创建的快捷方式。 Right click it and choose "Properties" 右键单击它并选择“属性”
  • In the "Target" textfield, append "-jar path-to-your-jar.jar" where you replace "path-to-your-jar.jar" with the actual path to your jar 在“目标”文本字段中,添加“-jar path-to-your-jar.jar”,将“path-to-your-jar.jar”替换为jar的实际路径
  • You can also now optionally change the icon to whatever icon you want 您现在还可以选择将图标更改为您想要的任何图标

This shortcut can be pinned to the taskbar and be used from anywhere (considering you provided an absolute path to your JAR). 此快捷方式可以固定到任务栏并可以在任何地方使用(考虑到您提供了JAR的绝对路径)。

If you have an executable jar file, just shift-right click on your file and set it to be opened by javaw. 如果您有一个可执行jar文件,只需右键单击您的文件并将其设置为由javaw打开。 The other option (in case you want to pass in parameters to your application) is to create a .bat file where you spin off your application via java or javaw 另一个选项(如果你想将参数传递给你的应用程序)是创建一个.bat文件,你可以通过java或javaw分离你的应用程序

右键单击eclipse中的“项目”并选择“导出”,然后选择“Java-> Runnable Jar File”选择您的项目名称并完成。

Seems you want to deploy and run the standalone application of swings. 似乎您想要部署并运行独立的swings应用程序。 Being a java developer you should understand the power of jar files. 作为一名java开发人员,您应该了解jar文件的强大功能。 Those are executable in themselves {so no need to create .exe files :)} . 这些是可执行的{所以不需要创建.exe文件:)}。

The below code will help you to create a jar file. 以下代码将帮助您创建一个jar文件。

Creating a jar File in Command Prompt 在命令提示符中创建jar文件

Start Command Prompt.
Navigate to the folder that holds your class files:
C:\>cd \mywork
Set path to include JDK’s bin.  For example:
C:\mywork> path c:\Program Files\Java\jdk1.5.0_09\bin;%path%
Compile your class(es):
C:\mywork> javac *.java
Create a manifest file:
C:\mywork> echo Main-Class: NameOfProject >manifest.txt
Create a jar file:
C:\mywork> jar cvfm NameOfProject.jar manifest.txt *.class
Test your jar:
C:\mywork> DanceStudio.jar

After creating a jar just double click on it and you are done. 创建一个jar后,只需双击它就可以了。

You have to create an executable jar file. 您必须创建一个可执行jar文件。 For that you just simply add a META-INF folder to the jar, then add a MANIFEST.MF text file with two lines: 为此,您只需将一个META-INF文件夹添加到jar中,然后添加一个包含两行的MANIFEST.MF文本文件:

Manifest-Version: 1.0
Main-Class: your.package.YourMainClass

Here's how to run a Java program by RIGHT-CLICKING on it (in other words, from the Windows Explorer context menu). 以下是如何通过RIGHT-CLICKING运行Java程序(换句话说,从Windows资源管理器上下文菜单)。 This handy trick is great for beginners who need to test their simple programs on the fly. 这个方便的技巧非常适合那些需要动态测试简单程序的初学者。 Works on both Win7 and XP rigs. 适用于Win7和XP平台。

[ ATTN: Depending on the situation, you may need to remove the package directive from the top of your Java file.] [ ATTN:根据具体情况,您可能需要从Java文件的顶部删除package指令。]

Step 1. Create a batch file (eg, RWJ.bat ) inside a folder of your choice (say, in C:\\Program Files\\Java .) 步骤1.在您选择的文件夹内(例如,在C:\\Program Files\\Java )创建批处理文件(例如, RWJ.bat )。

Step 2. Fill RWJ.bat with the following commands (they will work just fine as is with simple classes but you can, of course, tweak them according to your particular needs by specifying compiler / interpreter switches, passing args, adding echo off , removing pause or whatever): 步骤2.使用以下命令填充RWJ.bat (它们将与简单类一样正常工作,但您可以根据您的特定需要通过指定编译器/解释器开关,传递args,添加echo off来调整它们,删除暂停或其他):

javac %1
java %~n1
pause

The first command passes the full name of your right-clicked file to the Java compiler; 第一个命令将右键单击文件的全名传递给Java编译器; the second one strips the file extension and feeds JVM with the class name only. 第二个剥离文件扩展名,仅为类JVM提供JVM。

Step 3. Add the following key to your Registry: HKEY_CLASSES_ROOT\\*\\shell\\Run With Java and then create its command (default value): C:\\Program Files\\Java\\RWJ.bat %1 . 步骤3.将以下密钥添加到您的注册表: HKEY_CLASSES_ROOT\\*\\shell\\Run With Java ,然后创建其命令(默认值): C:\\Program Files\\Java\\RWJ.bat %1

黑客注册表

Step 4. Run your Java class by right-clicking it and selecting Run with Java option. 步骤4.右键单击Java类并选择Run with Java选项,运行Java类。

右键单击Java类

Java类启动并运行

That's all there is to it. 这里的所有都是它的。

Another way to run Java programs by pointing and clicking is to use AOT compilers. 通过指向和单击来运行Java程序的另一种方法是使用AOT编译器。 For example, GCC has an entry point named GCJ, which can be used to compile the source code into both byte codes and standard executable file for your particular OS. 例如,GCC有一个名为GCJ的入口点,可用于将源代码编译为特定操作系统的字节代码和标准可执行文件。

And finally, instead ot batch files one can run WSH, etc. 最后,相反,批处理文件可以运行WSH等。

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

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