0

I am new to Java, and have run into an issue that has confounded me. I have an app, MyApp, that I wrote in IntelliJ. The app uses an external library, MyLib.jar that talks to a database, using sqljdbc42.jar from Microsoft. MyLib.jar and sqljdbc.jar are both set as External Libraries in IntelliJ.

The issue I have is that my app runs perfectly fine when I run it in the IDE, but when I use Generate Artifact to make a jar, the jar errors out when trying to connect to the database.

The error: class java.lang.ClassNotFoundException | com.microsoft.sqlserver.jdbc.SQLServerDriver

This is a class in sqljdbc42.jar. I am assuming this means that MyApp.jar can't see the classes in sqljdbc42.jar.

sqljdbc42.jar can't be included into MyApp.jar because of the way it is signed. Instead, I have the file in the same folder as MyApp.jar, and this is my MANIFEST.MF:

Manifest-Version: 1.0
Class-Path: sqljdbc42.jar
Main-Class: com.mycompany.myapp.Main

And this is the file layout:

- MyApp_jar/
  - sqljdbc42.jar
  - MyApp.jar

I have another app set up the same way as far as I can tell, and that .jar works fine using the same class.

UPDATE - The .jar file I am attempting to include is a signed jar. Any other jar file I try to include works just fine. Here is a sample app to test:

import com.microsoft.sqlserver.jdbc.SQLServerDriver;
public class Main
{
public static void main(String[] args)
{
    try {
        System.out.println(SQLServerDriver.class);
    } catch (Exception e) {
        e.printStackTrace();
    }

}
}

And this is the jar I can't get to work: https://www.microsoft.com/en-us/download/details.aspx?id=11774 (4.2 version)

Wige
  • 3,788
  • 8
  • 37
  • 58
  • apparently it is all correct. Have you checked the content of the MANIFEST.MF that is included in the JAR? I mean, open the JAR with WinZip or similar (the jar command will do too) and extract the MANIFEST.MF file to see its content. https://docs.oracle.com/javase/tutorial/deployment/jar/view.html – user85421 Jan 20 '17 at 17:33
  • Yes, the content of the META-INF/MANIFEST.MF file is exactly as shown above. – Wige Jan 20 '17 at 18:55

2 Answers2

0

Run the jar file and not the class:

java -jar MyApp.jar

and it should add the sqljdbc42.jar to the classpath.

garyl
  • 41
  • 2
0

I downloaded the archive sqljdbc_4.2.6420.100_enu.tar.gz from your given URL and extracted the sqljdbc42.jar.

Following steps work as expected

Main.java

package com.mycompany.myapp;
import com.microsoft.sqlserver.jdbc.SQLServerDriver;
public class Main {
    public static void main(String[] args) {
        System.out.println(SQLServerDriver.class);
    }
}

manifest.mf

Manifest-Version: 1.0
Class-Path: sqljdbc42.jar
Main-Class: com.mycompany.myapp.Main

files in current directory

Main.java
manifest.mf
sqljdbc42.jar

compile

javac -d . -cp sqljdbc42.jar Main.java

Which creates the class file .\com\mycompany\myapp\Main.class.

create JAR

jar vcmf manifest.mf MyApp.jar com/

output

added manifest
adding: com/(in = 0) (out= 0)(stored 0%)
adding: com/mycompany/(in = 0) (out= 0)(stored 0%)
adding: com/mycompany/myapp/(in = 0) (out= 0)(stored 0%)
adding: com/mycompany/myapp/Main.class(in = 466) (out= 321)(deflated 31%)

run JAR

java -jar MyApp.jar

output

class com.microsoft.sqlserver.jdbc.SQLServerDriver
SubOptimal
  • 22,518
  • 3
  • 53
  • 69
  • Yargh. It works for me too when creating the jar from the command line. The only difference I could see was when I created the it from the command line, the Manifest had this line added: `Created-By: 1.8.0_102 (Oracle Corporation)` If I delete that line, the error happens. This line is not added when creating the jar with IntelliJ. It seems to be an issue exclusively with this one library? (I can include other jars just fine without this line.) – Wige Jan 23 '17 at 16:40
  • @Wige You mean when you create the `MyApp.jar` with the same `Main.class` with IntelliJ then `java -jar MyApp.jar` doesn't work (assuming MyApp.jar and sqljdbc42.jar are in the same directory). If so, could you please post the output of `jar -tvf MyApp.jar`. – SubOptimal Jan 24 '17 at 09:46