运行JAR时ClassNotFoundException,在IntelliJ IDEA中运行时没有错误 [英] ClassNotFoundException upon running JAR, no errors while running in IntelliJ IDEA

查看:2500
本文介绍了运行JAR时ClassNotFoundException,在IntelliJ IDEA中运行时没有错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚开始构建Java应用程序(我确实有.NET经验),我正在尝试构建一个小型测试应用程序,其整个代码是:

I'm just starting to build Java apps (I do have .NET experience) and I was trying to build a small test app whose whole code is this :

package com.company;

import com.microsoft.sqlserver.jdbc.SQLServerDataSource;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class Main {

    public static void main(String[] args) throws SQLException {
        System.out.println("Buna lume!");

        SQLServerDataSource ds = new SQLServerDataSource();
        ds.setIntegratedSecurity(true);
        ds.setServerName("localhost");
        ds.setPortNumber(1433);
        ds.setDatabaseName("Test");
        Connection con = ds.getConnection();

        String SQL = "SELECT * FROM Test WHERE ID = ?";
        PreparedStatement stmt = con.prepareStatement(SQL);
        stmt.setInt(1, 2);
        ResultSet rs = stmt.executeQuery();

        while (rs.next()) {
            System.out.println(rs.getInt(1) + ", " + rs.getString(2));
        }
        rs.close();
        stmt.close();

        con.close();
    }
}

如果我在IDE中运行应用程序(IntelliJ IDEA) 12.1.6 Community Edition),有了SQL Server,应用程序可以正常运行。

If I run the app in the IDE (IntelliJ IDEA 12.1.6 Community Edition), having the SQL Server available, the app runs fine doing exactly what it should.

从Microsoft下载SQL Server驱动程序并添加为外部图书馆。我创建了一个工件作为JAR文件:

The SQL Server Driver is downloaded from Microsoft and added as an External Library. I have created an artifact as JAR file :

现在,如果我在cliTest.jar(我的JAR)中包含sqljdbc4.jar,结果JAR很胖(550kB并包含JAR也是)。或者我可以排除它。无论哪种方式,运行

Now, if I include the sqljdbc4.jar in the cliTest.jar (my JAR) the resulted JAR is fat (550kB and includes the classes in that JAR too). Or I can exclude it. Either way, running

java -jar cliTest.jar

结果

Buna lume!
Exception in thread "main" java.lang.NoClassDefFoundError: com/microsoft/sqlserv
er/jdbc/SQLServerDataSource
        at com.company.Main.main(Main.java:15)
Caused by: java.lang.ClassNotFoundException: com.microsoft.sqlserver.jdbc.SQLSer
verDataSource
        at java.net.URLClassLoader$1.run(Unknown Source)
        at java.net.URLClassLoader$1.run(Unknown Source)
        at java.security.AccessController.doPrivileged(Native Method)
        at java.net.URLClassLoader.findClass(Unknown Source)
        at java.lang.ClassLoader.loadClass(Unknown Source)
        at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
        at java.lang.ClassLoader.loadClass(Unknown Source)
        ... 1 more

我打赌我错过了一些非常基本但我无法弄清楚到底是什么导致这种情况。

I bet I'm missing something quite basic but I just can't figure out what exactly is causing this.

LE1:我尝试添加sqljdbc4.jar(尽管似乎没有必要)和sq ljdbc_auth.dll在包含JAR的目录中但仍然没有变化。

LE1 : I tried adding sqljdbc4.jar (although it didn't seem necessary) and sqljdbc_auth.dll in the directory containing the JAR but still no change.

稍后编辑2:根据Nikolay的回答我做了以下内容:

Later edit 2 : Based on Nikolay's answer I've done the following :


  1. 删除现有工件

  2. 像这样创建一个新工件:

..并且结果如此:


  1. 构建 - >构建工件 - > cliTest.jar - >重建

  1. Build -> Build artifacts -> cliTest.jar -> Rebuild

CMD提示包含以下文件夹:

CMD Prompt at the folder containing :

[cliTest。 jar 566 KB生成]

[cliTest.jar 566 KB was generated]

java -jar cliTest.jar

java -jar cliTest.jar

现在我得到:

Exception in thread "main" java.lang.SecurityException: Invalid signature file d
igest for Manifest main attributes
    at sun.security.util.SignatureFileVerifier.processImpl(Unknown Source)
    at sun.security.util.SignatureFileVerifier.process(Unknown Source)
    at java.util.jar.JarVerifier.processEntry(Unknown Source)
    at java.util.jar.JarVerifier.update(Unknown Source)
    at java.util.jar.JarFile.initializeVerifier(Unknown Source)
    at java.util.jar.JarFile.getInputStream(Unknown Source)
    at sun.misc.URLClassPath$JarLoader$2.getInputStream(Unknown Source)
    at sun.misc.Resource.cachedInputStream(Unknown Source)
    at sun.misc.Resource.getByteBuffer(Unknown Source)
    at java.net.URLClassLoader.defineClass(Unknown Source)
    at java.net.URLClassLoader.access$100(Unknown Source)
    at java.net.URLClassLoader$1.run(Unknown Source)
    at java.net.URLClassLoader$1.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(Unknown Source)
    at java.lang.ClassLoader.loadClass(Unknown Source)
    at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
    at java.lang.ClassLoader.loadClass(Unknown Source)
    at sun.launcher.LauncherHelper.checkAndLoadMain(Unknown Source)


推荐答案

我应该在没有嵌入sqljdbc4.jar的情况下构建JAR。

Well I should have built the JAR without the sqljdbc4.jar embedded.

第二件事我是应该像这样运行命令:

The second thing I should have run the command like so :

java -classpath sqljdbc4.jar;cliTest.jar com.company.Main

..然后全部工作!

这篇关于运行JAR时ClassNotFoundException,在IntelliJ IDEA中运行时没有错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆