简体   繁体   中英

java.lang.ClassNotFoundException when loading class with URLClassLoader

I have a JAR file that looks like this:

MacBook@ ~/workspace-sync/mparnisa_dynamodb2/out/artifacts/dynamodb $ jar tf dynamodb.jar | head
META-INF/MANIFEST.MF
mparnisa-dynamodb-table.json
com/
com/mparnisa/
com/mparnisa/dynamodb/
com/mparnisa/dynamodb/table/
com/mparnisa/dynamodb/table/ResourceModel$ResourceModelBuilder.class
com/mparnisa/dynamodb/table/ResourceModel.class
com/amazonaws/AmazonServiceException.class
// and many more...

From an IntelliJ project, I want to create an instance of ResourceModel , which looks like this:

package com.mparnisa.dynamodb.table;

@com.fasterxml.jackson.annotation.JsonAutoDetect(fieldVisibility = com.fasterxml.jackson.annotation.JsonAutoDetect.Visibility.ANY, getterVisibility = com.fasterxml.jackson.annotation.JsonAutoDetect.Visibility.NONE, setterVisibility = com.fasterxml.jackson.annotation.JsonAutoDetect.Visibility.NONE)
public class ResourceModel {

So in my code I wrote this:

File file  = new File("/Users/mparnisa/workspace-sync/mparnisa_dynamodb2/out/artifacts/dynamodb/dynamodb.jar");

URL url = file.toURI().toURL();
URL[] urls = new URL[]{url};

ClassLoader urlClassLoader = new URLClassLoader(urls);

String resourceModelClassName = "com.mparnisa.dynamodb.table.ResourceModel";

Class<?> resourceModelClass = urlClassLoader.loadClass(resourceModelClassName);

but the last line throws an exception:

java.lang.ClassNotFoundException: com.mparnisa.dynamodb.table.ResourceModel

NoClassDefFound is also thrown when the class is found but static initialization fails. In this case there should be a more informative exception in the log at program start.

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