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

时间:2021-07-17 11:04:15

I coded up 4 .java files. The thing is that I can only execute my .java files from my IDE, how do I execute the .class files like an application? I study at uni, and I was told that Java is platform independent. Any tutorial/book recommendations would be highly appreciated.

我编写了4个.java文件。问题是我只能从IDE执行我的.java文件,如何像应用程序一样执行.class文件?我在uni学习,有人告诉我Java是独立于平台的。任何教程/书籍建议将受到高度赞赏。

Thanks

8 个解决方案

#1


The basic idea (to give you some things to search for) is:

基本想法(给你一些搜索的东西)是:

  • Bundle your compiled .class files into a 'jar'.
  • 将已编译的.class文件捆绑到“jar”中。

  • Add a manifest to your jar specifying a main class to run.
  • 向jar中添加一个清单,指定要运行的主类。

You might find your IDE already creates this when you run a 'clean build'. Netbeans puts this into a 'dist' folder.

当您运行“干净构建”时,您可能会发现IDE已经创建了这个。 Netbeans将其放入'dist'文件夹中。

Modern JREs will allow you to run the jar by double clicking it etc.

现代JRE将允许您通过双击等运行jar。

You can also go a bit further by wrapping the jar in a native executable using a tool such as JSmooth.

您还可以使用JSmooth等工具将jar包装在本机可执行文件中。

#2


Look at executable jar. Here is the precise one, without any details, Creating executable jar files.

看看可执行jar。这是精确的,没有任何细节,创建可执行jar文件。

#3


What IDE are you using?

你在用什么IDE?

Depending on the IDE, some support Export features that will create the .jar executable for you. For example, in Eclipse, you've got that option. Plus there are additional plug-ins for Eclipse, such as Fat-Jar, that will include any additional libs you include that aren't part of Sun's standard libs.

根据IDE,某些支持导出功能将为您创建.jar可执行文件。例如,在Eclipse中,您有这个选项。此外,Eclipse还有其他插件,例如Fat-Jar,它将包含您包含的任何其他不属于Sun标准库的库。

#4


Java files have to run through the Java Virtual Machine, so you can run your class files from the command line.

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

If you have a file called filename.java you compile it to filename.class and then you can run it from a command line by typing java filename

如果您有一个名为filename.java的文件,则将其编译为filename.class,然后您可以通过键入java filename从命令行运行它

#5


If you want to distribute your application on Windows, look into JSmooth.

如果要在Windows上分发应用程序,请查看JSmooth。

#6


JNLP/Web Start

#7


"The basic idea (to give you some things to search for) is:

“基本想法(给你一些搜索的东西)是:

Bundle your compiled .class files into a 'jar'. Add a manifest to your jar specifying a main class to run. You might find your IDE already creates this when you run a 'clean build'. Netbeans puts this into a 'dist' folder." (by Cogsy)

将已编译的.class文件捆绑到“jar”中。向jar中添加一个清单,指定要运行的主类。当您运行“干净构建”时,您可能会发现IDE已经创建了这个。 Netbeans把它放到'dist'文件夹中。“(由Cogsy提供)

Plus to achieve this you can either choose:

另外,要实现这一目标,您可以选择:

Depending on the IDE, some support Export features that will create the .jar executable for you. For example, in Eclipse, you've got that option. Plus there are additional plug-ins for Eclipse, such as Fat-Jar, that will include any additional libs you include that aren't part of Sun's standard libs. (by kchau)

根据IDE,某些支持导出功能将为您创建.jar可执行文件。例如,在Eclipse中,您有这个选项。此外,Eclipse还有其他插件,例如Fat-Jar,它将包含您包含的任何其他不属于Sun标准库的库。 (由kchau)

Or if you going to to serious stuff, opt for a build script like Ant or Maven. Here's an example of an Ant build.xml script:

或者,如果你想要认真的东西,选择像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>

#8


Just get out of the IDE and familiarise yourself with the command line tools. The java tutorial has a trail on the subject here (pick the Windows or Solaris/Linux section).

只需离开IDE并熟悉命令行工具即可。 java教程在这里有一个关于这个主题的踪迹(选择Windows或Solaris / Linux部分)。

#1


The basic idea (to give you some things to search for) is:

基本想法(给你一些搜索的东西)是:

  • Bundle your compiled .class files into a 'jar'.
  • 将已编译的.class文件捆绑到“jar”中。

  • Add a manifest to your jar specifying a main class to run.
  • 向jar中添加一个清单,指定要运行的主类。

You might find your IDE already creates this when you run a 'clean build'. Netbeans puts this into a 'dist' folder.

当您运行“干净构建”时,您可能会发现IDE已经创建了这个。 Netbeans将其放入'dist'文件夹中。

Modern JREs will allow you to run the jar by double clicking it etc.

现代JRE将允许您通过双击等运行jar。

You can also go a bit further by wrapping the jar in a native executable using a tool such as JSmooth.

您还可以使用JSmooth等工具将jar包装在本机可执行文件中。

#2


Look at executable jar. Here is the precise one, without any details, Creating executable jar files.

看看可执行jar。这是精确的,没有任何细节,创建可执行jar文件。

#3


What IDE are you using?

你在用什么IDE?

Depending on the IDE, some support Export features that will create the .jar executable for you. For example, in Eclipse, you've got that option. Plus there are additional plug-ins for Eclipse, such as Fat-Jar, that will include any additional libs you include that aren't part of Sun's standard libs.

根据IDE,某些支持导出功能将为您创建.jar可执行文件。例如,在Eclipse中,您有这个选项。此外,Eclipse还有其他插件,例如Fat-Jar,它将包含您包含的任何其他不属于Sun标准库的库。

#4


Java files have to run through the Java Virtual Machine, so you can run your class files from the command line.

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

If you have a file called filename.java you compile it to filename.class and then you can run it from a command line by typing java filename

如果您有一个名为filename.java的文件,则将其编译为filename.class,然后您可以通过键入java filename从命令行运行它

#5


If you want to distribute your application on Windows, look into JSmooth.

如果要在Windows上分发应用程序,请查看JSmooth。

#6


JNLP/Web Start

#7


"The basic idea (to give you some things to search for) is:

“基本想法(给你一些搜索的东西)是:

Bundle your compiled .class files into a 'jar'. Add a manifest to your jar specifying a main class to run. You might find your IDE already creates this when you run a 'clean build'. Netbeans puts this into a 'dist' folder." (by Cogsy)

将已编译的.class文件捆绑到“jar”中。向jar中添加一个清单,指定要运行的主类。当您运行“干净构建”时,您可能会发现IDE已经创建了这个。 Netbeans把它放到'dist'文件夹中。“(由Cogsy提供)

Plus to achieve this you can either choose:

另外,要实现这一目标,您可以选择:

Depending on the IDE, some support Export features that will create the .jar executable for you. For example, in Eclipse, you've got that option. Plus there are additional plug-ins for Eclipse, such as Fat-Jar, that will include any additional libs you include that aren't part of Sun's standard libs. (by kchau)

根据IDE,某些支持导出功能将为您创建.jar可执行文件。例如,在Eclipse中,您有这个选项。此外,Eclipse还有其他插件,例如Fat-Jar,它将包含您包含的任何其他不属于Sun标准库的库。 (由kchau)

Or if you going to to serious stuff, opt for a build script like Ant or Maven. Here's an example of an Ant build.xml script:

或者,如果你想要认真的东西,选择像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>

#8


Just get out of the IDE and familiarise yourself with the command line tools. The java tutorial has a trail on the subject here (pick the Windows or Solaris/Linux section).

只需离开IDE并熟悉命令行工具即可。 java教程在这里有一个关于这个主题的踪迹(选择Windows或Solaris / Linux部分)。