用ClassLoader加载的类无权访问更改的变量

我有一个问题: 有一个从外部.java文件编译的类

    private static Class<?> LoadedClass;
    private static Object LoadedClassInstance;
    public static Method LoadedMethod;

[SKIP]

   private static Path compileSource(Path javaFile) {
        JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
        compiler.run(null,null,javaFile.toFile().getabsolutePath());
        return javaFile.getParent().resolve("LoadedClass.class");
    }

    private static void runClass(Path javaClass)
            throws MalformedURLException,ClassnotFoundException,IllegalaccessException,InstantiationException,IllegalArgumentException,invocationTargetException,NoSuchMethodException,SecurityException {
        URL classUrl = javaClass.getParent().toFile().toURI().toURL();
        URLClassLoader classLoader = URLClassLoader.newInstance(new URL[]{classUrl});
        LoadedClass = Class.forName("LoadedClass",true,classLoader);

在此类中,有一个方法:

        LoadedClassInstance = LoadedClass.getDeclaredConstructor().newInstance(); 
        LoadedMethod = LoadedClass.getDeclaredMethod("DoTheMethod");                
    }

这是调用已加载方法的静态方法:

    public static void CallLoadedMethod() throws IllegalaccessException,invocationTargetException {
        LoadedMethod.invoke(LoadedClassInstance);   
    }

已加载方法调用另一个方法:

    import MyApp.MyClass1;
[SKIP]
    public static void DoTheMethod() {
        System.out.println("Your number is : "+MyClass1.Number);    
    }

我还有另外两个课程可以参加:

// MyClass1
    public static int Number = 5;
  public class MyClass1 {
    // DoSomeStuff
  }

// MyClass2
  public class MyClass2 {
    public static void SetTheNumberTo(int number){
        MyClass1.Number = number;
        System.out.println("Your NEW number is : "+MyClass1.Number);
    }
  }

这里是Loaded方法调用示例:

    MyClass2.SetTheNumberTo(10);
    Class_Loader.DoTheMethod();

当我通过eclipse运行我的应用程序时,我得到:

您的新电话号码是:10

您的电话号码是:10

但是当我将其编译为可运行的.jar时,我得到了另一个结果:

您的新电话号码是:10

您的电话号码是:5

这是问题:我在做什么错了?

www274953474 回答:用ClassLoader加载的类无权访问更改的变量

如果两个类对象具有相同的名称和相同的ClassLoader,则它们是相同的。您已经加载了两个不同的类,每个类都名为describe('Test - ',function() { const util = require('./testUtil.js'); var siteUrl = 'http://localhost:3000'; var domainName = 'localhost'; var landingUrl = 'http://localhost:3001'; browser.ignoreSynchronization = true; var jwtToken = 'boblkja-lsdkfj-lasdkjfhioasjdhfijuahsoifuhaosiuhfoiahfiuhsaoiuhf'; beforeEach(function() { browser.waitForAngular(); }); /** * Logging in by manually by setting the JWT Token cookie. */ it('T1',function() { browser.driver.get(siteUrl).then(function() { browser.manage().addCookie({ name: 'siteJwt',value: jwtToken,domain: domainName }); }) browser.get(siteUrl); ... }); });

Message: Failed: invalid cookie domain (Session info: headless chrome=79.0.3945.117) (Driver info: chromedriver=79.0.3945.16 (93fcc21110c10dbbd49bbff8f472335360e31d05-refs/branch-heads/3945@{#262}),platform=Windows NT 10.0.17763 x86_64) Stack: InvalidCookieDomainError: invalid cookie domain (Session info: headless chrome=79.0.3945.117) (Driver info: chromedriver=79.0.3945.16 (93fcc21110c10dbbd49bbff8f472335360e31d05-refs/branch-heads/3945@{#262}),platform=Windows NT 10.0.17763 x86_64) at Object.checkLegacyResponse (C:\Install\node-v12.13.1-win-x64\node_modules\protractor\node_modules\selenium-webdriver\lib\error.js:546:15) at parseHttpResponse (C:\Install\node-v12.13.1-win-x64\node_modules\protractor\node_modules\selenium-webdriver\lib\http.js:509:13) at C:\Install\node-v12.13.1-win-x64\node_modules\protractor\node_modules\selenium-webdriver\lib\http.js:441:30 at processTicksAndRejections (internal/process/task_queues.js:93:5) From: Task: WebDriver.manage().addCookie(siteJwt=**********;domain=localhost) at Driver.schedule (C:\Install\node-v12.13.1-win-x64\node_modules\protractor\node_modules\selenium-webdriver\lib\webdriver.js:807:17) at Options.addCookie (C:\Install\node-v12.13.1-win-x64\node_modules\protractor\node_modules\selenium-webdriver\lib\webdriver.js:1243:25) 中的

MyClass1与主程序的ClassLoader中从MyClass2调用的MyClass1.Number不同。它们不是同一字段,因为它们不在同一类中。

本文链接:https://www.f2er.com/2777479.html

大家都在问