I am using Netbeans and upgraded to JDK 15. Since then, I am having problems with reflection. The very same reflection works fine if I do the same code as application. However, if I want to use the same code in servlet init then it failes. Throwing an exception of TargetInvocation exception.
(These codes works perfectly fine with JDK 8)
My code is below (servlet init)
package servlet;
import java.io.IOException;
import java.lang.reflect.Constructor;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
*
* @author Administrator
*/
public class core extends HttpServlet
{
@Override
public void init() throws ServletException
{
Object id = null;
try
{
Class<?> MyPackageClass = Class.forName("webapi.UI");
boolean rc = MyPackageClass.isAssignableFrom(MyPackageClass);
Constructor<?> constructor = MyPackageClass.getDeclaredConstructor();
Object MyClassInstance = constructor.newInstance();
String s = "";
}
catch(Exception e)
{
Throwable cause = e.getCause();
String s1 = cause.getMessage();
String s2 = backTrace(cause);
String s = "";
}
}
public String backTrace(Throwable e)
{
StackTraceElement[] stack = e.getStackTrace();
String trace = "";
for (int i=0; i<stack.length; ++i) {
trace += stack[i].toString() + "\n";
}
return trace;
}
The exception stack trace
s2 = (java.lang.String) "webapi.UI.<init>(UI.java:1)
java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:64)
java.base/jdk.internal.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
java.base/java.lang.reflect.Constructor.newInstanceWithCaller(Constructor.java:500)
java.base/java.lang.reflect.Constructor.newInstance(Constructor.java:481)
servlet.core.init(core.java:31)
javax.servlet.GenericServlet.init(GenericServlet.java:158)
org.apache.catalina.core.StandardWrapper.initServlet(StandardWrapper.java:1134)
org.apache.catalina.core.StandardWrapper.loadServlet(StandardWrapper.java:1089)
org.apache.catalina.core.StandardWrapper.load(StandardWrapper.java:983)
org.apache.catalina.core.StandardContext.loadOnStartup(StandardContext.java:4864)
org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5173)
org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:183)
org.apache.catalina.core.ContainerBase.addChildInternal(ContainerBase.java:717)
org.apache.catalina.core.ContainerBase.addChild(ContainerBase.java:690)
org.apache.catalina.core.StandardHost.addChild(StandardHost.java:706)
org.apache.catalina.startup.HostConfig.deployDescriptor(HostConfig.java:631)
org.apache.catalina.startup.HostConfig.deployApps(HostConfig.java:487)
org.apache.catalina.startup.HostConfig.check(HostConfig.java:1642)
jdk.internal.reflect.GeneratedMethodAccessor45.invoke(Unknown Sourc...
Exception Details
e = (java.lang.reflect.InvocationTargetException) java.lang.reflect.InvocationTargetException
Cannot suppress a null exception
And the class I am trying to create the instance of
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package webapi;
/**
*
* @author Administrator
*/
public class UI
{
public int x = 0;
public UI()
{
}
public String test()
{
return "oops";
}
}
I found the problem. The problem was with my build-impl.xml file under nbproject folder. The following link worked for me to fix the issue. JDK 15 disabled xbootclasspath/p option. error: option -Xbootclasspath/p: not allowed with target 12 (Netbeans 11.0)
EDIT: Even though it fixed the compile problems, there was still issues related to the reflection. One other issue I came accross was the difference between using x64 and 32 bit versions of Netbeans. So I switched using 32 bit versions and the problems fixed. Currently I am dialed down to NB 11.3 + JDK 13
Note: Interestingly the issue with x64 NB, only occurs when you are using Servlet library of my own and a web project. Somehow it was working fine until the instantiation step. At that step was throwing, targetinvocationexception with x64 version. If I tried the same thing with NOT servlet + web project combination, in other words, an executable app + library it works perfectly fine.
Note2: I managed my web + servlet project work with Netbeans 12.1 as well. I followed previous Note addition to that I needed to update build-impl.xml under nbproject folder. Find -Xbootclasspath/p place in the xml and add -J before it. So the final value should be like -J-Xbootclasspath/p:'${toString:endorsed.classpath.path}'