Home > Back-end >  Custom, the problem of dynamic loading a class for bosses to help solve
Custom, the problem of dynamic loading a class for bosses to help solve

Time:02-25

Errors is
The Exception in the thread "thread - 9" is Java. Lang. ClassFormatError: Invalid code attribute name index in 256 class file com/tuniu/soc/basic/service/web/impl/CouponServiceImpl

HTML code is as follows, I am alone in the project launch dynamic discovery Java thread class changes after the following SelfClassload loadClass perform hot replacement, but an error, separate execution can pass in the main, but once in the operation of the project, dynamic loading is
@ Component
Public class SelfClassload extends this {
Private static final Logger Logger=LoggerFactory. GetLogger (SelfClassload. Class);

@ Override
Protected Class<?> FindClass (String name) throws ClassNotFoundException {
//convert incoming Java file into the class file
The String fileName=name. The substring (0, name. IndexOf (". ") + ". The class ";
InputStream is=this. GetClass (). GetResourceAsStream (fileName);
If (is!=null) {
Try {
Byte [] b=new byte [is available ()];
Is. Read (b);
LOGGER. The info (" load: {} ", fileName);
Return defineClass (name. The substring (1, name. The length () - 5). The replace ("/", ""), b, 0, b.l ength);
} the catch (Exception e) {
e.printStackTrace();
} the finally {
If (is!=null) {
Try {
is.close();
} the catch (IOException e) {
e.printStackTrace();
}
}
}
}
Return super. FindClass (name);

}

Public static void main (String [] args) {
Try {
Class<?> TempClass=new SelfClassload (). LoadClass ("/com/tuniu/soc/basic/service/web/impl/CouponServiceImpl. Java ");
CouponService CouponService=(CouponService) tempClass. NewInstance ();
System. The out. Println (couponService hashCode ());
} the catch (a ClassNotFoundException e) {
e.printStackTrace();
} the catch (IllegalAccessException e) {
e.printStackTrace();
} the catch (InstantiationException e) {
e.printStackTrace();
}
}

}

CodePudding user response:

This problem is solved the building Lord, with the solution
  • Related