我正在为Windows和Linux系统生成安装程序。在欢迎屏幕之后,我有一个CustomForm组件,我在其中获取用户详细信息,然后使用javax.ws.rs.client.ClientBuilder
将它们发送到服务器。但是在Windows中,当我的类尝试从ClientBuilder构建客户端时,安装程序会抛出java.lang.LinkageError: ClassCastException
。整个日志如下:
java.lang.LinkageError: ClassCastException: attempting to cast
jar:file:/C:/Users/lutful.kabir/AppData/Local/Temp/e4j363A.tmp_dir1460795758/user/javaee-api-7.0.jar!/javax/ws/rs/client/ClientBuilder.class to
jar:file:/C:/Users/lutful.kabir/AppData/Local/Temp/e4j363A.tmp_dir1460795758/user/javaee-api-7.0.jar!/javax/ws/rs/client/ClientBuilder.class
at javax.ws.rs.client.ClientBuilder.newBuilder(ClientBuilder.java:97)
at javax.ws.rs.client.ClientBuilder.newClient(ClientBuilder.java:114)
at install4j.AuthorizationHandler.verifyAuthorization(AuthorizationHandler.java:21)
at install4j.backendsetup.VerifyBackend.install(VerifyBackend.java:27)
at com.install4j.runtime.installer.InstallerContextImpl$2.fetchValue(InstallerContextImpl.java:167)
at com.install4j.runtime.installer.InstallerContextImpl$2.fetchValue(InstallerContextImpl.java:164)
at com.install4j.runtime.installer.helper.comm.actions.FetchObjectAction.execute(FetchObjectAction.java:14)
at com.install4j.runtime.installer.helper.comm.HelperCommunication.executeActionDirect(HelperCommunication.java:272)
at com.install4j.runtime.installer.helper.comm.HelperCommunication.executeActionInt(HelperCommunication.java:247)
at com.install4j.runtime.installer.helper.comm.HelperCommunication.executeActionChecked(HelperCommunication.java:185)
at com.install4j.runtime.installer.helper.comm.HelperCommunication.fetchObjectChecked(HelperCommunication.java:168)
at com.install4j.runtime.installer.InstallerContextImpl.performActionIntStatic(InstallerContextImpl.java:164)
at com.install4j.runtime.installer.InstallerContextImpl.performActionInt(InstallerContextImpl.java:152)
at com.install4j.runtime.installer.ContextImpl.performAction(ContextImpl.java:1099)
at com.install4j.runtime.installer.controller.Controller.executeAction(Controller.java:367)
at com.install4j.runtime.installer.controller.Controller.executeActions(Controller.java:333)
at com.install4j.runtime.installer.controller.Controller.handleCommand(Controller.java:194)
at com.install4j.runtime.installer.controller.Controller.start(Controller.java:94)
at com.install4j.runtime.installer.Installer.runInProcess(Installer.java:59)
at com.install4j.runtime.installer.Installer.main(Installer.java:46)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:606)
at com.exe4j.runtime.LauncherEngine.launch(LauncherEngine.java:62)
at com.exe4j.runtime.WinLauncher.main(WinLauncher.java:101)
at com.install4j.runtime.launcher.WinLauncher.main(WinLauncher.java:26)
AuthorizationHandler
的代码如下:
package install4j;
import java.net.ConnectException;
import javax.ws.rs.client.Client;
import javax.ws.rs.client.ClientBuilder;
import javax.ws.rs.client.Invocation;
import javax.ws.rs.client.Invocation.Builder;
import javax.ws.rs.client.WebTarget;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import com.install4j.api.Util;
import com.install4j.api.context.InstallerContext;
import installerconfigparams.utils.InstallerParameter;
public class AuthorizationHandler {
public static boolean verifyAuthorization(String authToken, InstallerContext context) throws ConnectException {
Client client = ClientBuilder.newClient();
WebTarget target = client.target(InstallerParameter.CENTRAL_SERVER_URL.getStringValue())
.path(InstallerParameter.PATH_BACKEND_VERIFICATION.getStringValue());
Builder builder = target.request(MediaType.APPLICATION_JSON);
builder.header(InstallerParameter.AUTHORIZATION_HEADER.getStringValue(), authToken);
Invocation invocation = builder.buildGet();
Response response;
response = invocation.invoke();
response.getStringHeaders();
int status = response.getStatus();
if (InstallerParameter.HTTP_200.getIntValue() != status) {
String args = response.getHeaderString("error");
Util.showErrorMessage(context.getMessage("installer.backend.login.invalid") + args);
return false;
}
Util.showMessage(context.getMessage("installer.backend.login.successful"));
return true;
}
现在,据我所知并基于我的互联网研究,当可能存在重复资源时会发生这种情况(从日志中您还可以看到它所使用的ClientBuilder
类也来自相同的路径) 。我确保没有添加重复的jar,以便安装程序可以混淆它。我并不熟悉ClassLoaders或者它对Install4j的工作原理。我还尝试使用Run Script
操作,直接在那里编写代码,但最后却遇到了相同的异常。
自从我构建安装程序以来,我一直在使用相同的类和方法。直到今天都没有任何问题。在遇到这个问题之前,我甚至检查过我的旧提交。但结果是一样的。此问题仅发生在Windows中的安装程序中。
答案 0 :(得分:2)
JAX-WS使用上下文类加载器进行某些操作。调用
Thread.currentThread().setContextClassLoader(getClass().getClassLoader());
将上下文类加载器设置为加载脚本的同一个类加载器,这与自install4j 6.1以来的系统类加载器不同。
从install4j 6.1.2开始,将自动调整上下文类加载器,因此不再需要上述解决方法。