简体   繁体   English

将Java类文件关联在双击Windows上运行

[英]Associate Java class-files to run on double-click on Windows

If there's one thing that annoys me about Java it's that you can't double-click a class file so as to run. 如果有一件事让我厌烦Java,那就是你无法双击一个类文件来运行。 I assuming there's an entry in the registry that has to be edited to do this but I haven't a clue. 我假设在注册表中有一个条目,必须进行编辑才能做到这一点,但我没有任何线索。

So, as it says on the tin. 所以,正如它在锡上所说的那样。 Does anyone know how to associate Java class files to run on double-click on Windows (I aiming for Windows 7 here but I'm sure there'd be no difference in the three most latest releases)? 有没有人知道如何关联Java类文件以便在Windows上双击运行(我的目标是Windows 7,但我确定三个最新版本没有区别)? It would make my life (and I'm sure many other people's) much easier! 这将使我的生活(我相信很多其他人)更容易!

Udpate : I've seen answers relating to making a JAR out of the class in question and running it that way. Udpate :我已经看到了有关在课堂上制作JAR并以这种方式运行的答案。 However useful, that is not exactly what I'm looking for here. 无论多么有用,这都不是我在这里寻找的。 I'm effectively looking for Windows itself to invoke java with the class on double-click, with the correct arguments. 我有效地寻找Windows本身调用java与双击类,以正确的参数。

if classpath doesnt matter too much, easily done with a simple batch file runjava.bat or so that is associated with .class files in the explorer (via right click >> open with..) 如果classpath不重要,可以使用简单的批处理文件runjava.bat轻松完成,该文件与资源管理器中的.class文件相关联(通过右键单击>>打开...)

@echo off
REM change to folder where the class file resides
cd %~d1%~p1
REM execute the class by calling its name without file extension
start java %~n1

The double-clickable JAR solution is the most common plain Java distribution method. 可双击的JAR解决方案是最常见的普通Java分发方法。 There'd be a number of issues with trying to execute .class files directly, with the classpath the one that pops first to mind. 尝试直接执行.class文件会遇到很多问题,类路径首先会弹出。

That said, if you wanted to support the very simplest possibilities in your development environment , you could conceivably implement a script that 也就是说,如果您想支持开发环境中最简单的可能性,您可以想象实现一个脚本

  • inspected the .class file for the full class name (including package and inner class name) 检查了.class文件中的完整类名(包括包和内部类名)
  • walked up the directory tree to the root of the file's class path 将目录树向上移动到文件类路径的根目录
  • (optionally included any common lib directories in the classpath) (可选地包括类路径中的任何常见lib目录)
  • invoked Java for the determined class 为确定的类调用Java

Then you could register your shiny script as a handler for .class files. 然后,您可以将您的闪亮脚本注册为.class文件的处理程序。 But since you're in the development environment, aren't you happier with your IDE doing that? 但是,由于您处于开发环境中,您对IDE的执行情况不满意吗?

When you install the Java Runtime Environment, it registers .jar files as an association in Windows. 安装Java Runtime Environment时,它会将.jar文件作为Windows中的关联进行注册。 If you double-click on a .jar file, it will open it using Java. 如果双击.jar文件,它将使用Java打开它。 For this to work, you need to make sure you have a manifest defined that points to the class to run. 为此,您需要确保定义了一个指向要运行的类的清单。 Your class file to be run must have a main method that will be called. 要运行的类文件必须具有将被调用的main方法。

Let's assume you have a class named 'com.TheClass.class' on disk. 假设您在磁盘上有一个名为“com.TheClass.class”的类。 If you want to have this able to run with double click, create a file in a new directory called META-INF/manifest.mf. 如果您希望能够双击运行,请在名为META-INF / manifest.mf的新目录中创建一个文件。 Put this into it: 把它放进去吧:

Manifest-Version: 1.2
Main-Class: com.TheClass

Zip (or use the jar command) both your class up with this manifest directory and file. Zip(或使用jar命令)您的类与此清单目录和文件。 Rename it to mine.jar. 将其重命名为mine.jar。 Double click on it and it should launch your class with the Java runtime. 双击它,它应该使用Java运行时启动您的类。

For a .class file to run, needs in first place to have "something" to do, that is, that .class should contain a main method. 要运行一个.class文件,首先需要有“东西”要做,即.class应该包含一个main方法。 Not all the .class do have one. 并非所有.class都有一个。

One thing you can do, is to wrap your app ( a number of .class files ) inside a jar file. 你可以做的一件事是将你的应用程序(一些.class文件)包装在一个jar文件中。

For short, you just need in addition to your classes a manifest file that says, where the main method is : 简而言之,除了您的类之外,您还需要一个清单文件, 其中主要方法是

jar -cmf yourmanifestfile.mf  doubleClickApp.jar  *.class 

And that's it, the doubleClickApp.jar is now executable with a "doubleClick" gesture. 就是这样,doubleClickApp.jar现在可以通过“doubleClick”手势执行。

http://justaddhotwater.webs.com/javaexec.htm http://justaddhotwater.webs.com/javaexec.htm

This software makes it possible to run your Java classes by double-clicking them.(Windows ONLY). 该软件可以通过双击来运行Java类。(仅限Windows)。

The easiest way that I have found was creating a shortcut on the same folder than the .class file. 我找到的最简单的方法是在.class文件的同一文件夹上创建一个快捷方式。 Then right click on it and go to properties. 然后右键单击它并转到属性。 Change the field Target to java NameOfClass , finally double click the shortcut :) 将字段Target更改为java NameOfClass ,最后双击快捷方式:)

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

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