2009-01-19 63 views
2

我編寫了4個.java文件。問題是我只能從IDE執行我的.java文件,我該如何像應用程序一樣執行.class文件?我在uni學習,並被告知Java是平臺獨立的。任何教程/書籍的建議將不勝感激。如何將我的java文件轉換爲應用程序?

謝謝

+0

你的IDE是什麼?他們大多數有一個「運行」按鈕,允許您運行程序 – OscarRyz 2009-01-20 02:49:58

回答

9

基本思想從命令行運行文件(給你需要搜索的東西)是:

  • 將編譯後的.class文件打包到一個'jar'中。
  • 將清單添加到指定要運行的主類的jar中。

當您運行'clean build'時,您可能會發現您的IDE已經創建了這個。 Netbeans把它放到一個'dist'文件夾中。

現代的JRE將允許您運行通過雙擊罐子就等

您還可以走得更遠一點通過使用工具如JSmooth在本地可執行包裝罐。

+0

+1指出本機可執行文件和Netbeans jar的創建。 – 2009-01-19 03:49:58

3

Java文件必須通過Java虛擬機運行,因此您可以從命令行運行您的類文件。

如果你有一個名爲filename.java你把它編譯成filename.class,然後你可以通過鍵入的java文件名

4

你在用什麼IDE?

取決於IDE,一些支持Export功能將爲您創建.jar可執行文件。例如,在Eclipse中,您已有該選項。此外,還有其他針對Eclipse的插件,如Fat-Jar,它將包含您所包含的任何不屬於Sun標準庫的附加庫。如果你想在Windows上發佈應用程序,看看JSmooth

1

JNLP/Web Start的

1

「的基本思路(給你一些東西 搜索)是:

捆綁編譯的.class文件到 一個‘罐子’清單添加到您的罐子 指定一個主。 可能會發現你的IDE已經創建了 ,當你運行一個'clean build'。 Netbeans把它放到'dist' 文件夾中。「(由Cogsy)

Plus來實現這一目標,你既可以選擇:

根據不同的IDE,一些支持 導出功能,將創建 的.jar可執行您例如, 在Eclipse中,你有這樣的選擇。 再加上有額外的插件 Eclipse中,如脂肪-JAR,將 包括任何附加庫您 包括不是Sun的 標準庫的一部分。 (由kchau)

或者如果您要認真對待,請選擇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> 
1

剛剛走出IDE和使用命令行工具的熟悉。 java tutorial有關於主題here的路徑(選擇Windows或Solaris/Linux部分)。

相關問題