英文:
SpringBoot: How handle correctly this Linkage Error?
问题
@RestController
public class Controller {
public JarClassMethod getAllTests() throws IllegalArgumentException, NoSuchMethodException,
InvocationTargetException, IllegalAccessException, IOException, LinkageError, ClassNotFoundException {
JarClassMethod testClassObjMap = new JarClassMethod();
LoadLibrary loadLibrary = new LoadLibrary();
List<JarFile> jarList = loadLibrary.getListJar().stream().map(f -> {
try {
return new JarFile(f);
} catch (IOException e) {
e.printStackTrace();
}
return null;
})
.collect(Collectors.toList());
for (JarFile j : jarList) {
for (Enumeration<JarEntry> entries = j.entries(); entries.hasMoreElements(); ) {
JarEntry entry = entries.nextElement();
String file = entry.getName();
if (file.endsWith(".class")) {
String classname = file.replaceAll("/", ".")
.substring(0, file.lastIndexOf("."));
try {
Class<?> currentClass = Class.forName(classname);
List<String> testMethods = new ArrayList<>();
for (int i = 0; i < currentClass.getMethods().length; i++) {
Method method = currentClass.getMethods()[i];
Annotation annTest = method.getAnnotation(Test.class);
Annotation annTestFactory = method.getAnnotation(TestFactory.class);
if (annTest != null || annTestFactory != null) {
testMethods.add(method.getName());
}
} // end for methods
if (testMethods.size() >= 1) {
testClassObjMap.put(j.getName().substring(j.getName().lastIndexOf("\\") + 1), classname, testMethods);
System.out.format("%s %s %s", j.toString(), classname, testMethods);
}
} catch (NoClassDefFoundError e) {
System.out.println("WARNING: failed NoClassDefFoundError " + classname + " from " + file);
} catch (Throwable e) {
System.out.println("WARNING: failed to instantiate " + classname + " from " + file);
}
} // end if .class
} // end inner for
} // end outer for
return testClassObjMap;
}
@ResponseStatus(value = HttpStatus.OK)
@RequestMapping(value = "/getTestList", method = RequestMethod.GET)
public ResponseEntity<JarClassMethod> getTestList() throws IOException, ClassNotFoundException, NoSuchMethodException, IllegalAccessException, InvocationTargetException {
JarClassMethod testClassObjMap = this.getAllTests();
return new ResponseEntity<>(testClassObjMap, HttpStatus.OK);
}
}
Please note that I've only provided the translated code portion without any additional content.
英文:
I hope that you can help me..
I have a spring-boot application that loads some jar in a folder (with class and method with annotation @Test
or @TestFactory
) and loads in a JSON all jar name-class name-method Test
name.
When I call a get service /getTestList
I view this json like this for example:
> {jarFile:{classFileName:{methodTestName1:"xxxx",methodTestName2:"yyy"},{classFileName2:{methodTestName1:"xxxx",methodTestName2:"yyy"} }
This is Controller.class:
@RestController
public class Controller {
public JarClassMethod getAllTests() throws IllegalArgumentException, NoSuchMethodException,
InvocationTargetException, IllegalAccessException, IOException,LinkageError, ClassNotFoundException {
JarClassMethod testClassObjMap= new JarClassMethod();
LoadLibrary loadLibrary=new LoadLibrary();
List<JarFile> jarList= loadLibrary.getListJar().stream().map(f -> {
try {
return new JarFile(f);
} catch (IOException e) {
e.printStackTrace();
}
return null;
})
.collect(Collectors.toList());
for (JarFile j : jarList) {
for (Enumeration<JarEntry> entries = j.entries(); entries.hasMoreElements(); ) {
JarEntry entry = entries.nextElement();
String file = entry.getName();
if (file.endsWith(".class")) {
String classname = file.replaceAll("/", ".")
.substring(0, file.lastIndexOf("."));
try {
Class<?> currentClass = Class.forName(classname);
List<String> testMethods = new ArrayList<>();
for (int i = 0; i < currentClass.getMethods().length; i++) {
Method method = currentClass.getMethods()[i];
Annotation annTest = method.getAnnotation(Test.class);
Annotation annTestFactory = method.getAnnotation(TestFactory.class);
if (annTest != null || annTestFactory != null) {
testMethods.add(method.getName());
}
}//fine for metodi
if (testMethods.size() >=1) {
testClassObjMap.put(j.getName().substring(j.getName().lastIndexOf("\\")+1),classname,testMethods);
System.out.format("%s %s %s",j.toString(),classname,testMethods);
}
}catch (NoClassDefFoundError e) {
System.out.println("WARNING: failed NoClassDefFoundError " + classname + " from " + file);
}
catch (Throwable e) {
System.out.println("WARNING: failed to instantiate " + classname + " from " + file);
}
}//if .class
}//chiudo jarentry for
}//chiudo jarfile for
return testClassObjMap;
}
@ResponseStatus(value = HttpStatus.OK)
@RequestMapping(value = "/getTestList", method = RequestMethod.GET)
public ResponseEntity<JarClassMethod> getTestList() throws IOException, ClassNotFoundException, NoSuchMethodException, IllegalAccessException, InvocationTargetException {
JarClassMethod testClassObjMap=this.getAllTests();
return new ResponseEntity<>(testClassObjMap, HttpStatus.OK);
}
with method getAllTests()
I first load jar like external libraries and after with a for iterate all jarsFile, with another for iterate all classes and finally with another for iterate all method with annotation @Test
or @TestFactory
.
On my pc in windows it work correctly but when i package project and put this jar on a Linux Server i have this error:
There was an unexpected error (type=Internal Server Error, status=500).
loader constraint violation: when resolving method
"com.fasterxml.jackson.databind.ser.impl.ReadOnlyClassToSerializerMap.untypedValueSerializer(Ljava/lang/Class;)Lcom/fasterxml/jackson/databind/JsonSerializer;" the class loader (instance of
org/springframework/boot/loader/LaunchedURLClassLoader) of the current class,
com/fasterxml/jackson/databind/SerializerProvider, and the class loader (instance of
sun/misc/Launcher$AppClassLoader) for the method's defining class,
com/fasterxml/jackson/databind/ser/impl/ReadOnlyClassToSerializerMap, have different Class objects for
the type com/fasterxml/jackson/databind/JsonSerializer used in the signature
I tried to exclude Jackson from spring-boot dependency but Response of getTestList()
doesn't work. The other Jars files used also Jackson libraries.
this is my pom.xml: pomxml
Pls Help me.. I'm tired from 3 days.
Thanks and
Regards Friends..
专注分享java语言的经验与见解,让所有开发者获益!
评论