【问题标题】:how to build jar file from my project?如何从我的项目构建 jar 文件?
【发布时间】:2011-07-29 07:38:39
【问题描述】:

我用javamysql 编写了一个程序,现在我想要一个jar 文件,当我点击它时程序工作(without that mysql install on my system)。

我右键单击我的项目并按下 clean 并构建,但它没有构建 jar 文件及以下

写在输出中。

Updating property file: C:\Users\mehdi\Documents\NetBeansProjects\project1\build\built-clean.properties
Deleting directory C:\Users\mehdi\Documents\NetBeansProjects\project1\build
clean:
init:
deps-jar:
Created dir: C:\Users\mehdi\Documents\NetBeansProjects\project1\build
Updating property file: C:\Users\mehdi\Documents\NetBeansProjects\project1\build\built-jar.properties
Created dir: C:\Users\mehdi\Documents\NetBeansProjects\project1\build\classes
Created dir: C:\Users\mehdi\Documents\NetBeansProjects\project1\build\empty
Compiling 8 source files to C:\Users\mehdi\Documents\NetBeansProjects\project1\build\classes
C:\Users\mehdi\Documents\NetBeansProjects\project1\src\project1\NewUser.java:24: package sun.swing.table does not exist
import sun.swing.table.DefaultTableCellHeaderRenderer;

Note: Some input files use or override a deprecated API.
Note: Recompile with -Xlint:deprecation for details.
1 error
C:\Users\mehdi\Documents\NetBeansProjects\project1\nbproject\build-impl.xml:595: The following error occurred while executing this line:
C:\Users\mehdi\Documents\NetBeansProjects\project1\nbproject\build-impl.xml:276: Compile failed; see the compiler error output for details.
BUILD FAILED (total time: 0 seconds)

现在我想知道如何在没有安装 mysql 的情况下从我的程序中构建一个可以正常工作的 jar 文件。

I use net beans 6.9.1

and jdk-6u23-windows-i586

【问题讨论】:

    标签: java mysql netbeans jdbc


    【解决方案1】:

    这根本与mysql无关。编译的错误很明显:

    C:\Users\mehdi\Documents\NetBeansProjects\project1\src\project1\NewUser.java:24: package sun.swing.table does not exist
    import sun.swing.table.DefaultTableCellHeaderRenderer;
    

    您的构建失败,因为它无法在任何地方找到导入。修复您的导入并重新编译。

    【讨论】:

    • @mehdi,该错误日志不会说谎。它可以与 NetBeans 一起运行,很公平,尝试找出它在编译期间失败的原因。
    • @mehdi,导入来自 sun.* 包,通常不应该在代码中使用。您是否安装了多个 JDK?也许它可以在 Netbeans 中正确运行,因为它使用的是 Sun JDK,但是当您以其他方法运行它时,您使用的是非 Sun JDK,因此该类不存在。
    【解决方案2】:

    右键单击您的项目并转到 Properties-->Build-->Compiling 在底部使用“-Xlint:deprecation”填充 Additional Compiler Options 也不会有害。

    【讨论】:

    • 我用 -Xlint:deprecation 填充了附加编译器选项,但它没有字。
    • 您收到以下消息: 1. 注意:某些输入文件使用或覆盖了已弃用的 API。和 2. 注意:使用 -Xlint:deprecation 重新编译以获取详细信息。编译器选项解决了第二个问题。您必须尝试找到已弃用的 API 并将其删除。一般来说,如果输入正确,Netbeans 会创建完美运行的 jar。
    【解决方案3】:

    我敢说您正在尝试使用没有 sun.* 包的 OpenJDK 进行构建。由于Netbeans持续编译,它可能在netbeans中运行。检查您的设置以确保您使用的是 sun JDK,然后一切正常。

    【讨论】:

    • 或避免使用 sun.* 包!
    • 我通常会建议这样做,但这会假定他在没有任何真正理由的情况下使用它们。有时可能需要使用太阳包。例如,在使用 javax.sound api 时,我必须这样做。
    • 我同意,我只是想指出,如果有人在使用 sun.* 软件包,应该非常确定他这样做的原因,而不是意外使用它们(因为 eclipse 建议使用它! )
    【解决方案4】:

    实际上有两个问题我想分开: 1.如何克服编译错误? 2. 如何将你的项目打包成一个独立的可运行的jar文件?

    上面第一次说得够多了。我只想添加一些一般性建议:在导出项目之前仔细检查您的类路径:不同的 IDE 可能在默认设置的类路径上有不同的库,但是当您导出代码并在其他位置从命令行运行它时,事情会停止工作,因为以前可用的第 3 方不再可用。

    关于第二个问题:您需要将您正在使用的所有 3rd 方打包在您的 jar 中。我确信应该有一些 NetBeans 插件可以帮助您做到这一点。您可能会发现此链接很有帮助:

    http://dr.berkeley.edu/REM/wiki/index.php/Making_a_Java_executable_jar_in_Netbeans

    祝你好运!

    【讨论】:

      【解决方案5】:

      我遇到了同样的问题。为了使事情正常进行,我将其添加到 pom.xml 文件中...

      <plugin>
          <groupId>org.apache.maven.plugins</groupId>
          <artifactId>maven-compiler-plugin</artifactId>
          <version>2.3.2</version>
          <configuration>
              <source>1.6</source>
              <target>1.6</target>
              <compilerArguments>
                  <bootclasspath>${java.home}\lib\rt.jar</bootclasspath>
              </compilerArguments>
          </ configuration>
      </plugin>
      

      【讨论】:

        【解决方案6】:

        可能你在编译的时候要加上javac -XDignore.symbol.file,见Using internal sun classes with javac

        仅供参考 maven 应该是这样的:

        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>2.0.2</version>
            <configuration>
                <source>1.6</source>
                <target>1.6</target>
                <compilerArgument>
                    -XDignore.symbol.file
                </compilerArgument>
            </configuration>
        </plugin>
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2020-03-05
          • 1970-01-01
          • 2014-12-21
          • 2011-12-21
          • 1970-01-01
          • 2020-08-27
          • 2015-12-30
          • 2020-04-06
          相关资源
          最近更新 更多