我正在为Windows和Linux系统生成安装程序。在欢迎屏幕之后,我有一个CustomForm组件,其中包含用户详细信息,然后使用javax.ws.rs.client.ClientBuilder将其发送到服务器。但是在Windows中,当my Class尝试从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,以便安装程序可以混淆它。我对ClassLoader或其对Install4j的工作方式并不十分了解。我也尝试过通过直接在其中编写代码来使用Run Script动作,但最终却出现了相同的异常。

自从构建安装程序以来,我一直在使用相同的类和方法。直到今天都没有任何问题。在遇到这个问题之前,我什至退了旧的Commit。但是结果是一样的。而且仅Windows中的安装程序会出现此问题。

最佳答案

JAX-WS使用上下文类加载器进行某些操作。呼唤

 Thread.currentThread().setContextClassLoader(getClass().getClassLoader());


将上下文类加载器设置为与加载脚本相同的类加载器,这与从install4j 6.1开始的系统类加载器不同。

从install4j 6.1.2开始,将自动调整上下文类加载器,因此不再需要上述解决方法。

10-07 16:42