如何将java文件转换为应用程序?

如何将java文件转换为应用程序?,java,executable,executable-jar,Java,Executable,Executable Jar,我编写了4.java文件。问题是我只能从IDE执行.java文件,如何像应用程序一样执行.class文件?我在uni学习,有人告诉我Java是独立于平台的。如有任何教程/书籍推荐,将不胜感激 谢谢你看。这里是一个精确的例子,没有任何细节。Java文件必须通过Java虚拟机运行,因此您可以从命令行运行类文件 如果您有一个名为filename.java的文件,您可以将其编译为filename.class,然后通过键入java filename从命令行运行它。基本思想(为您提供一些搜索内容)是: 将

我编写了4.java文件。问题是我只能从IDE执行.java文件,如何像应用程序一样执行.class文件?我在uni学习,有人告诉我Java是独立于平台的。如有任何教程/书籍推荐,将不胜感激


谢谢你看。这里是一个精确的例子,没有任何细节。

Java文件必须通过Java虚拟机运行,因此您可以从命令行运行类文件

如果您有一个名为filename.java的文件,您可以将其编译为filename.class,然后通过键入java filename从命令行运行它。基本思想(为您提供一些搜索内容)是:

  • 将编译后的.class文件捆绑到一个“jar”中
  • 将清单添加到jar中,指定要运行的主类
在运行“干净构建”时,您可能会发现您的IDE已经创建了这个。Netbeans将其放入“dist”文件夹

现代JRE将允许您通过双击jar等方式运行它


您还可以更进一步,使用诸如JSmooth之类的工具将jar包装到本机可执行文件中。

您使用的是什么IDE


根据IDE的不同,一些支持特性将为您创建.jar可执行文件。例如,在中,您拥有该选项。此外,Eclipse还有其他插件,例如,它将包括Sun标准库以外的任何附加库。

JNLP/Web Start

如果您想在Windows上分发应用程序,请查看

“基本想法(给你一些 要搜索的内容)是:

将编译后的.class文件捆绑到 “jar”。将清单添加到您的jar中 指定要运行的主类。您可以 可能会发现您的IDE已经创建 当您运行“干净构建”时,会出现此问题。 Netbeans将其放入“dist” 文件夹。”(由Cogsy提供)

此外,要实现这一点,您可以选择:

根据IDE的不同,需要一些支持 导出将创建 .jar可执行文件。例如 在Eclipse中,您有这个选项。 此外,还有其他用于 Eclipse,如Fat Jar,将 包括您需要的任何附加LIB 包括不属于Sun的 标准库。(九广铁路局)

或者,如果你想做一些严肃的事情,选择像Ant或Maven这样的构建脚本。 以下是Ant build.xml脚本的示例:

<project name="jar with libs" default="compile and build" basedir=".">
    <!-- this is used at compile time -->
    <path id="example-classpath">
        <pathelement location="${root-dir}" />
        <fileset dir="D:/LIC/xalan-j_2_7_1" includes="*.jar" />
    </path>

    <target name="compile and build">
        <!-- deletes previously created jar -->
        <delete file="test.jar" />

        <!-- compile your code and drop .class into "bin" directory -->
        <javac srcdir="${basedir}" destdir="bin" debug="true" deprecation="on">
            <!-- this is telling the compiler where are the dependencies -->
            <classpath refid="example-classpath" />
        </javac>

        <!-- copy the JARs that you need to "bin" directory  -->
        <copy todir="bin">
            <fileset dir="D:/LIC/xalan-j_2_7_1" includes="*.jar" />
        </copy>

        <!-- creates your jar with the contents inside "bin" (now with your .class and .jar dependencies) -->
        <jar destfile="test.jar" basedir="bin" duplicate="preserve">
            <manifest>
                <!-- Who is building this jar? -->
                <attribute name="Built-By" value="${user.name}" />
                <!-- Information about the program itself -->
                <attribute name="Implementation-Vendor" value="ACME inc." />
                <attribute name="Implementation-Title" value="GreatProduct" />
                <attribute name="Implementation-Version" value="1.0.0beta2" />
                <!-- this tells which class should run when executing your jar -->
                <attribute name="Main-class" value="ApplyXPath" />
            </manifest>
        </jar>
    </target>
</project>


离开IDE,熟悉命令行工具即可。有关于这个主题的线索(选择Windows或Solaris/Linux部分)。

+1指出本机可执行文件和Netbeans jar创建。您的IDE是什么?大多数都有一个“运行”按钮,可以让你运行你的程序