Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: Why does my ClassLoader only work in Debug Mode?

  1. #1
    Junior Member
    Join Date
    Jan 2021
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Why does my ClassLoader only work in Debug Mode?

    Hey ,
    My Problem is that the ClassLoader in my Programm only works when I debug my Programm but i don't know why?
    I tried so many Thinks but yeah nothing better.
    My Code:

     String userPath = System.getProperty("user.home");
        File clientLocation = new File(userPath + "\\AppData\\Roaming\\.minecraft\\versions\\MCP");
     
        File f2 = new File(clientLocation.getPath() + "\\plugins");
        if (!f2.exists()) {
          f2.mkdir();
        }
        File f1 = new File(clientLocation.getPath() + "\\plugins\\pluginMeta");
        if (!f1.exists()) {
          f1.mkdir();
        }
     
        File[] arrayOfFile;
        int j = (arrayOfFile = new File(clientLocation.getPath() + "\\plugins").listFiles()).length;
        for (int i = 0; i < j; i++)
        {
          File file = arrayOfFile[i];
          if (file.getName().endsWith(".jar"))
          {
            File authorizedJarFile = file;
            ClassLoader authorizedLoader = null;
            try
            {
              authorizedLoader = URLClassLoader.newInstance(new URL[] { authorizedJarFile.toURL() });
            }
            catch (MalformedURLException e)
            {
              e.printStackTrace();
            }
            try
            {
              File pluginYML = new File(clientLocation.getPath() + "\\plugins\\pluginMeta\\" + file.getName().replace(".jar", "") + ".yml");
              if (!pluginYML.exists()) {
                pluginYML.createNewFile();
              }
              JarFile jarFile = new JarFile(authorizedJarFile);
              ZipEntry entry = jarFile.getEntry("plugin.yml");
              InputStream is = jarFile.getInputStream(entry);
     
              Files.copy(
                is, 
                pluginYML.toPath(), new CopyOption[] {
                StandardCopyOption.REPLACE_EXISTING });
     
              IOUtils.closeQuietly(is);
     
              FileConfiguration cfg = new FileConfiguration(pluginYML);
     
              Class c = authorizedLoader.loadClass(cfg.getString("main"));
              Plugin authorizedPlugin = (Plugin)c.newInstance();
              plugins.add(authorizedPlugin);
              authorizedPlugin.setName(cfg.getString("name"));
              authorizedPlugin.setAuthor(cfg.getString("author"));
              authorizedPlugin.setVersion(cfg.getString("version"));
              authorizedPlugin.onEnable();
     
              System.out.println("[" + Date.getDate() + "] Enabling " + authorizedPlugin.getName() + " v" + authorizedPlugin.getVersion() + " by " + authorizedPlugin.getAuthor());
            }
            catch (IOException|InstantiationException|IllegalAccessException e1)
            {
     
            }
          }
        }

    So I mean this code works perfectly when I debug my Programm, but when i run it normal, it says 'ClassCastException'.
    Please help D:

  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Why does my ClassLoader only work in Debug Mode?

    Can you make a small, complete program that will compile and execute to show the problem?

    Also post the full error message that shows what statement the exception happened on.

    How are you trying to debug the program? Have you printed the name of the jar file and the name of the class that is being loaded? Also print out the value of c to see what class was loaded.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Replies: 6
    Last Post: August 4th, 2014, 05:04 AM
  2. question about debug mode
    By leonne in forum Java IDEs
    Replies: 5
    Last Post: May 15th, 2013, 06:18 AM
  3. question about debug mode
    By leonne in forum Java Theory & Questions
    Replies: 2
    Last Post: January 9th, 2013, 04:16 PM
  4. ClassLoader and inner classes
    By sidd in forum What's Wrong With My Code?
    Replies: 1
    Last Post: April 10th, 2011, 11:07 AM
  5. Works on debug mode but not on run mode
    By alfonsoraul in forum Member Introductions
    Replies: 0
    Last Post: April 14th, 2010, 02:58 PM