简体   繁体   中英

Mac JDK11: Exception in thread “main” java.lang.NoClassDefFoundError: java/sql/SQLException

New to Java here. Using IntelliJ. I wrote a simple "helloworld" Spring application that compiles just fine, but when I run it receive the following error:

/Library/Java/JavaVirtualMachines/jdk-11.0.1.jdk/Contents/Home/bin/java "-javaagent:/Applications/IntelliJ IDEA.app/Contents/lib/idea_rt.jar=51884:/Applications/IntelliJ IDEA.app/Contents/bin" -Dfile.encoding=UTF-8 -p /Users/winston.kotzan/Development/java-spring-helloworld/bin:/Users/winston.kotzan/java/spring-framework-5.1.3/libs/spring-aop-5.1.3.RELEASE.jar:/Users/winston.kotzan/java/spring-framework-5.1.3/libs/spring-aspects-5.1.3.RELEASE.jar:/Users/winston.kotzan/java/spring-framework-5.1.3/libs/spring-beans-5.1.3.RELEASE.jar:/Users/winston.kotzan/java/spring-framework-5.1.3/libs/spring-context-5.1.3.RELEASE.jar:/Users/winston.kotzan/java/spring-framework-5.1.3/libs/spring-context-indexer-5.1.3.RELEASE.jar:/Users/winston.kotzan/java/spring-framework-5.1.3/libs/spring-context-support-5.1.3.RELEASE.jar:/Users/winston.kotzan/java/spring-framework-5.1.3/libs/spring-core-5.1.3.RELEASE.jar:/Users/winston.kotzan/java/spring-framework-5.1.3/libs/spring-expression-5.1.3.RELEASE.jar:/Users/winston.kotzan/java/spring-framework-5.1.3/libs/spring-instrument-5.1.3.RELEASE.jar:/Users/winston.kotzan/java/spring-framework-5.1.3/libs/spring-jcl-5.1.3.RELEASE.jar:/Users/winston.kotzan/java/spring-framework-5.1.3/libs/spring-jdbc-5.1.3.RELEASE.jar:/Users/winston.kotzan/java/spring-framework-5.1.3/libs/spring-jms-5.1.3.RELEASE.jar:/Users/winston.kotzan/java/spring-framework-5.1.3/libs/spring-messaging-5.1.3.RELEASE.jar:/Users/winston.kotzan/java/spring-framework-5.1.3/libs/spring-orm-5.1.3.RELEASE.jar:/Users/winston.kotzan/java/spring-framework-5.1.3/libs/spring-oxm-5.1.3.RELEASE.jar:/Users/winston.kotzan/java/spring-framework-5.1.3/libs/spring-test-5.1.3.RELEASE.jar:/Users/winston.kotzan/java/spring-framework-5.1.3/libs/spring-tx-5.1.3.RELEASE.jar:/Users/winston.kotzan/java/spring-framework-5.1.3/libs/spring-web-5.1.3.RELEASE.jar:/Users/winston.kotzan/java/spring-framework-5.1.3/libs/spring-webflux-5.1.3.RELEASE.jar:/Users/winston.kotzan/java/spring-framework-5.1.3/libs/spring-webmvc-5.1.3.RELEASE.jar:/Users/winston.kotzan/java/spring-framework-5.1.3/libs/spring-websocket-5.1.3.RELEASE.jar -m SpringHelloWorld/com.wakproductions.MainApp
Exception in thread "main" java.lang.NoClassDefFoundError: java/sql/SQLException
    at spring.core@5.1.3.RELEASE/org.springframework.core.Constants.<init>(Constants.java:67)
    at spring.beans@5.1.3.RELEASE/org.springframework.beans.factory.xml.XmlBeanDefinitionReader.<clinit>(XmlBeanDefinitionReader.java:102)
    at spring.context@5.1.3.RELEASE/org.springframework.context.support.AbstractXmlApplicationContext.loadBeanDefinitions(AbstractXmlApplicationContext.java:83)
    at spring.context@5.1.3.RELEASE/org.springframework.context.support.AbstractRefreshableApplicationContext.refreshBeanFactory(AbstractRefreshableApplicationContext.java:133)
    at spring.context@5.1.3.RELEASE/org.springframework.context.support.AbstractApplicationContext.obtainFreshBeanFactory(AbstractApplicationContext.java:622)
    at spring.context@5.1.3.RELEASE/org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:518)
    at spring.context@5.1.3.RELEASE/org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:144)
    at spring.context@5.1.3.RELEASE/org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:85)
    at SpringHelloWorld/com.wakproductions.MainApp.main(MainApp.java:8)
Caused by: java.lang.ClassNotFoundException: java.sql.SQLException
    at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(BuiltinClassLoader.java:583)
    at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(ClassLoaders.java:178)
    at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:521)
    ... 9 more

I suspect that it may have to do with the CLASSPATH. I tried setting the CLASSPATH environment variable, as well as adding this to the java command

-cp /Library/Java/JavaVirtualMachines/jdk-11.0.1.jdk/Contents/Home

But that does not seem to work. Might I have the incorrect CLASSPATH?

The comments about the module-info file led me to some clues. I took notice that IntelliJ seems to have created a module-info.java file with the following contents:

module SpringHelloWorld {
    requires spring.context;
}

Did not realizing I was compiling a module. I deleted that file along with everything in the bin directory. Then I was able to run it by the Run menu using this option:

在此处输入图片说明

I can run it on the command line via:

java -Dfile.encoding=UTF-8 -classpath /Users/.../java-spring-helloworld/bin:/Users/.../<spring-framework-libs>... com.wakproductions.MainApp

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM