从另一个类调用方法时java.lang.NoClassDefFoundError

时间:2016-07-27 10:20:24

标签: java noclassdeffounderror

我从一个名为Task的类中调用一个swingworker线程,它正在给NoClassDefFoundError

Task.class和Install.class`都在jar的根目录中。

这是出错的代码:

@Override
public void actionPerformed(ActionEvent e){
    if(e.getActionCommand() == "install") {
        new Task().execute();
    }
}

new Task().execute();是第125行。

修改 这是任务类:

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.util.Enumeration;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;

import javax.swing.JOptionPane;
import javax.swing.SwingUtilities;
import javax.swing.SwingWorker;

import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;
import org.json.simple.parser.ParseException;

public class Task extends SwingWorker<Integer, String>{
    String v;
    String wf;
    String rp;
    String pn;
    String otxt;

    public void deleteb() {
        JOptionPane.showMessageDialog(null, "The delete button isnt working yet!");
    }
    public void installb() {
        try {
            SwingUtilities.invokeLater(new Runnable() {
                Install i = new Install();
                 public void run() {
                     i.setlptext("Getting map information.");
                 }
             });
        read();
        setvar();
        SwingUtilities.invokeLater(new Runnable() {
            Install i = new Install();
             public void run() {
                 i.setlversion(v);
                 i.setlmapname(pn);
                 i.setlptext("Creating file system.");
             }
         });
        } catch(FileNotFoundException e) {
            e.printStackTrace();
        } catch(IOException e) {
            e.printStackTrace();
        }
        try {
            createfilesystem();
            SwingUtilities.invokeLater(new Runnable() {
                Install i = new Install();
                 public void run() {
                     i.setlptext("Exporting world resources.");
                 }
             });
            ExportResource("/" + wf,true);
            SwingUtilities.invokeLater(new Runnable() {
                Install i = new Install();
                 public void run() {
                     i.setlptext("Copying world to profile folder.");
                 }
             });
            copyworld();
            if (rp.length() != 0) {
                SwingUtilities.invokeLater(new Runnable() {
                    Install i = new Install();
                     public void run() {
                         i.setlptext("exporting resource pack resources.");
                     }
                 });
                ExportResource("/" + rp,true);
                SwingUtilities.invokeLater(new Runnable() {
                    Install i = new Install();
                     public void run() {
                         i.setlptext("Copying resource pack to profile folder");
                     }
                 });
                copypack();
            }
            if (otxt.length() != 0) {
                SwingUtilities.invokeLater(new Runnable() {
                    Install i = new Install();
                     public void run() {
                         i.setlptext("Setting correct options.");
                     }
                 });
                ExportResource("/" + otxt,false);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        SwingUtilities.invokeLater(new Runnable() {
            Install i = new Install();
             public void run() {
                 i.setlptext("Getting temporary information.");
             }
         });
    if(temp.isDirectory() && temp.exists()) {
        try {
            SwingUtilities.invokeLater(new Runnable() {
                Install i = new Install();
                 public void run() {
                     i.setlptext("Deleting temporary information.");
                 }
             });
            delete(temp);
        } catch (IOException e) {
            SwingUtilities.invokeLater(new Runnable() {
                Install i = new Install();
                 public void run() {
                     i.setlptext("FAILED DELETING TEMPORARY INFORMATION!");
                 }
             });
            e.printStackTrace();
        }
    };
    SwingUtilities.invokeLater(new Runnable() {
        Install i = new Install();
         public void run() {
             i.setlptext("Creating profile.");
         }
     });
    setjson();
    SwingUtilities.invokeLater(new Runnable() {
        Install i = new Install();
         public void run() {
             i.setlptext("Map has succesfully installed!");
         }
     });
    }
//************************************************************

    public String getmc() {
        //Windows
        if(System.getProperty("os.name").toLowerCase().contains("windows"))
        {
            return System.getenv("APPDATA") + "\\.minecraft";
        }

        //Mac
        if(System.getProperty("os.name").toLowerCase().contains("mac os x"))
        {
            return System.getProperty("user.home") + "/Library/Application Support/minecraft";
        }

        //Linux(hopefully)
        if(System.getProperty("os.name").toLowerCase().contains("linux"))
        {
            return System.getProperty("user.home") + "/.minecraft";
}
        return null;
    }


    @SuppressWarnings("unchecked")
    public void setjson() {
        JSONParser parser = new JSONParser();
        Object obj;
        try {
            String mapversion = v;
            String mapname = pn;
            String mapdir = appdata() + File.separator + pn;
            mapdir = mapdir.replace("\\","\\\\");
            obj = parser.parse(new FileReader(getmc() + File.separator + "launcher_profiles.json"));
            if(obj.toString().length() != 0) {
            JSONObject jsonObject = (JSONObject) obj;

            JSONObject profiles = (JSONObject) jsonObject.get("profiles");
            JSONObject map;
                map = new JSONObject();
            map.put("name", mapname);
            map.put("lastVersionId", mapversion);
            map.put("gameDir", mapdir);
            profiles.put(mapname, map);
            jsonObject.put("profiles",profiles);
            jsonObject.remove("selectedProfile");
            jsonObject.put("selectedProfile", mapname);
            System.out.println(jsonObject.toJSONString());

            try (FileWriter file = new FileWriter(getmc() + File.separator + "launcher_profiles.json")) {
                File f = new File(getmc() + File.separator + "launcher_profiles.json");
                f.createNewFile();
                file.write(jsonObject.toJSONString());
            }



            } else {
                JOptionPane.showMessageDialog(null, "Your profile list is empty. run the minecraft launcher to fix this issue.");
            }


        } catch (FileNotFoundException e) {
            JOptionPane.showMessageDialog(null, "You must have launched minecraft at least once!");
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (ParseException e) {
            e.printStackTrace();
        }

    }

public boolean checkprofile(String jsonpn) {
    JSONParser parser = new JSONParser();
    Object obj;
    try {
        String mapdir = appdata() + File.separator + pn;
        mapdir = mapdir.replace("\\","\\\\");
        obj = parser.parse(new FileReader(getmc() + File.separator + "launcher_profiles.json"));
        if(obj.toString().length() != 0) {
        JSONObject jsonObject = (JSONObject) obj;

        JSONObject profiles = (JSONObject) jsonObject.get("profiles");
        if (profiles.get(jsonpn) != null) {
            return false;
        } else {
            return true;
        }
        } else {
            JOptionPane.showMessageDialog(null, "Your profile list is empty. run the minecraft launcher to fix this issue.");
        }


    } catch (FileNotFoundException e) {
        JOptionPane.showMessageDialog(null, "You must have launched minecraft at least once!");
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    } catch (ParseException e) {
        e.printStackTrace();
    }
    return false;
}


    public void setvar() {
        st = appdata() + File.separator + pn + File.separator + "options.txt";
    }


    //SET OPTIONS RESOURCEPACK
    String st;




    public String appdata() {
        if(System.getProperty("os.name").toLowerCase().contains("windows"))
        {
            return System.getenv("APPDATA");
        }
        if(System.getProperty("os.name").toLowerCase().contains("mac os x"))
        {
            return System.getProperty("user.home") + "/Library/Application Support";
        }
        //(hopefully)
        if(System.getProperty("os.name").toLowerCase().contains("linux"))
        {
            return System.getProperty("user.home") + "";
}
        return null;
    }


    public void read() throws FileNotFoundException, IOException 
    {
        InputStream in = this.getClass().getResourceAsStream("/info.txt");
        BufferedReader reader = new BufferedReader(new InputStreamReader(in));
        StringBuilder out = new StringBuilder();
        String line;
        while ((line = reader.readLine()) != null) {
            if(line.trim().contains("v:")) {
                v = line.replace("v:", "");
             }
            if(line.trim().contains("wf:")) {
                wf = line.replace("wf:", "");
             }
            if(line.trim().contains("rp:")) {
                rp = line.replace("rp:", "");
             }
            if(line.trim().contains("otxt:")) {
                otxt = line.replace("otxt:", "");
             }
            if(line.trim().contains("pn:")) {
                testprofile(line.replace("pn:", ""));
             }
            out.append(line);
            System.out.println(line);
        }      
        System.out.println(out.toString());
        reader.close();
        in.close();
    }

public boolean testprofile(String pnf) {
    if (!checkprofile(pnf)) {
        pnf = pnf + "-";
    }
    File folder = new File(appdata()+File.separator+pnf);
    if(folder.exists()) {
        if (testprofile(pnf + "-")) {
            return true;
        }
    } else {
        pn = pnf;
        return true;
    }
    return false;
}

public static void copyFolder(File source, File destination)
{
    if (source.isDirectory())
    {
        if (!destination.exists())
        {
            destination.mkdirs();
        }

        String files[] = source.list();

        for (String file : files)
        {
            File srcFile = new File(source, file);
            File destFile = new File(destination, file);

            copyFolder(srcFile, destFile);
        }
    }
    else
    {
        InputStream in = null;
        OutputStream out = null;

        try
        {
            in = new FileInputStream(source);
            out = new FileOutputStream(destination);

            byte[] buffer = new byte[1024];

            int length;
            while ((length = in.read(buffer)) > 0)
            {
                out.write(buffer, 0, length);
            }
        }
        catch (Exception e)
        {
            try
            {
                in.close();
            }
            catch (IOException e1)
            {
                e1.printStackTrace();
            }

            try
            {
                out.close();
            }
            catch (IOException e1)
            {
                e1.printStackTrace();
            }
        }
        try
        {
            in.close();
        }
        catch (IOException e1)
        {
            e1.printStackTrace();
        }

        try
        {
            out.close();
        }
        catch (IOException e1)
        {
            e1.printStackTrace();
        }
        try
        {
            in.close();
            in = null;
            out.flush();
            out.close();
            out = null;
            System.gc();
        }
        catch (IOException e)
        {
            e.printStackTrace();
        }
    }
}
private static File traverse(File dir, String name) {
    if (dir.isDirectory()) {
        String[] children = dir.list();
        for (int i = 0; children != null && i < children.length; i++) {
            File tmp = traverse(new File(dir, children[i]),name.trim());
            if( tmp != null ) {
              return tmp;
            }
        }
    }
    if (dir.isFile()) {
        if (dir.getName().trim().equalsIgnoreCase(name.trim())) {
            return dir;
        }
    }
    return null;
}
    public void copyworld() {
        //extract zip
        System.out.println(wftemp.getAbsolutePath());
        String wfpath = appdata()+ File.separator+pn+File.separator+"temp"+File.separator+wf;
        try {
            openZipFile(wfpath,wftemp.getAbsolutePath().toString());
        } catch (IOException e) {
            e.printStackTrace();
        }
        //search for level.dat
        File leveldat = traverse(wftemp,"level.dat");
        File dir = new File(appdata()+ File.separator+pn+File.separator+"saves"+File.separator+leveldat.getParentFile().getName());
        dir.mkdirs();
        //copy to world folder
        copyFolder(leveldat.getParentFile(),dir);
    }
    public void copypack() {
        //extract zip
        String rppath = appdata()+ File.separator+pn+File.separator+"temp"+File.separator+rp;
        try {
            openZipFile(rppath,rptemp.getAbsolutePath().toString());
        } catch (IOException e) {
            e.printStackTrace();
        }
        //search for level.dat
        File leveldat = traverse(rptemp,"pack.mcmeta");
        //copy to world folder
        File dir = new File(appdata()+ File.separator+pn+File.separator+"resourcepacks"+File.separator+leveldat.getParentFile().getName());
        dir.mkdirs();
        copyFolder(leveldat.getParentFile(),dir);
    }



    File rptemp;
    File wftemp;
    File temp;
    public void createfilesystem() {
        File saves = new File(appdata()+ File.separator+pn+File.separator+"saves");
        File resourcepacks = new File(appdata()+ File.separator+pn+File.separator+"resourcepacks");
        saves.mkdirs();
        temp = new File(appdata()+ File.separator+pn+File.separator+"temp");
        wftemp = new File(appdata()+ File.separator+pn+File.separator+"temp"+File.separator+wf.replace(".zip", ""));
        resourcepacks.mkdirs();
        wftemp.mkdirs();
        if (rp.length() != 0) {
            String randompath = rp.substring(0, rp.length() - 4);
            rptemp = new File(appdata()+ File.separator+pn+File.separator+"temp"+File.separator+randompath);
            if (rptemp.mkdirs()) {
                System.out.println("COULDNT CREATE DIR OF RESOURCEPACK");
                System.out.println(rptemp.getAbsolutePath());
                if(rptemp.isDirectory()) {
                    System.out.println("true");
                } else {
                    System.out.println("false");
                }
                if(rptemp.canWrite()) {
                    System.out.println("true");
                } else {
                    System.out.println("false");
                }
            }
        }
    }

    @SuppressWarnings("resource")
    public void openZipFile(String zipFilename, String destinationDirname) throws IOException {
        byte[] buffer = new byte[1024];
        int bytesRead = 0;
        File zipFile = new File(zipFilename);
        File destinationDir = new File(destinationDirname);
        ZipFile zip = new ZipFile(zipFile);
        Enumeration<? extends ZipEntry> zipEntries = zip.entries();
        while (zipEntries.hasMoreElements()) {
          ZipEntry entry = zipEntries.nextElement();
          if (entry.isDirectory()) {
            File newDir = new File(destinationDir, entry.getName());
            newDir.mkdirs();
          } else {
            BufferedInputStream inputStream = new BufferedInputStream(zip.getInputStream(entry));
            File outputFile = new File(destinationDir, entry.getName());
            BufferedOutputStream outputStream = new BufferedOutputStream(new FileOutputStream(outputFile));
            while ((bytesRead = inputStream.read(buffer)) != -1) {
              outputStream.write(buffer, 0, bytesRead);
            }
            try {
            inputStream.close();
            outputStream.close();
            } finally {
                 try
                    {
                        inputStream.close();
                        inputStream = null;
                        outputStream.flush();
                        outputStream.close();
                        outputStream = null;
                        System.gc();
                    }
                    catch (IOException e)
                    {
                        e.printStackTrace();
                    }
            }
          }

        }

      }

    public void ExportResource(String resourceName, boolean temp) throws Exception {
        InputStream stream = null;
        OutputStream resStreamOut = null;
        String path = "";
        if (temp) {
            path = File.separator + "temp";
        } else {
            path = "";
        }
        try {
            stream = this.getClass().getResourceAsStream(resourceName);
            if(stream == null) {
                throw new Exception("Cannot get resource \"" + resourceName + "\" from Jar file.");
            }

            int readBytes;
            byte[] buffer = new byte[4096];
            resStreamOut = new FileOutputStream(appdata() + File.separator + pn + path + resourceName);
            while ((readBytes = stream.read(buffer)) > 0) {
                resStreamOut.write(buffer, 0, readBytes);
            }
        } catch (Exception ex) {
            throw ex;
        } finally {
            stream.close();
            resStreamOut.close();
        }

        stream.close();
        resStreamOut.close();
    }




    public static void delete(File file)
            throws IOException{

            if(file.isDirectory()){

                //directory is empty, then delete it
                if(file.list().length==0){

                   if(file.delete()) {
                   System.out.println("Directory is deleted : " 
                                                     + file.getAbsolutePath());
                   }else {
                       System.out.println("FAILED TO DELETE");
                   }

                }else{

                   //list all the directory contents
                   String files[] = file.list();

                   for (String temp : files) {
                      //construct the file structure
                      File fileDelete = new File(file, temp);

                      //recursive delete
                     delete(fileDelete);
                   }

                   //check the directory again, if empty then delete it
                   if(file.list().length==0){
                        file.delete();
                     System.out.println("Directory is deleted : " 
                                                      + file.getAbsolutePath());
                   }
                }

            }else{
                //if file, then delete it
                file.delete();
                System.out.println("File is deleted : " + file.getAbsolutePath());
            }
    }

    @Override
    protected Integer doInBackground() throws Exception {
        installb();
        return null;
    }
}

这是错误:

    Exception in thread "AWT-EventQueue-0" java.lang.NoClassDefFoundError: Task$1
        at Task.installb(Task.java:38)
        at Install.actionPerformed(Install.java:125)
        at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2022)
        at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2348)
        at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:402)
        at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:259)
        at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:252)
        at java.awt.Component.processMouseEvent(Component.java:6535)
        at javax.swing.JComponent.processMouseEvent(JComponent.java:3324)
        at java.awt.Component.processEvent(Component.java:6300)
        at java.awt.Container.processEvent(Container.java:2236)
        at java.awt.Component.dispatchEventImpl(Component.java:4891)
        at java.awt.Container.dispatchEventImpl(Container.java:2294)
        at java.awt.Component.dispatchEvent(Component.java:4713)
        at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4888)
        at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4525)
        at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4466)
        at java.awt.Container.dispatchEventImpl(Container.java:2280)
        at java.awt.Window.dispatchEventImpl(Window.java:2750)
        at java.awt.Component.dispatchEvent(Component.java:4713)
        at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:758)
        at java.awt.EventQueue.access$500(EventQueue.java:97)
        at java.awt.EventQueue$3.run(EventQueue.java:709)
        at java.awt.EventQueue$3.run(EventQueue.java:703)
        at java.security.AccessController.doPrivileged(Native Method)
        at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:76)
        at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:86)
        at java.awt.EventQueue$4.run(EventQueue.java:731)
        at java.awt.EventQueue$4.run(EventQueue.java:729)
        at java.security.AccessController.doPrivileged(Native Method)
        at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:76)
        at java.awt.EventQueue.dispatchEvent(EventQueue.java:728)
        at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:201)
        at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:116)
        at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:105)
        at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101)
        at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:93)
        at java.awt.EventDispatchThread.run(EventDispatchThread.java:82)
Caused by: java.lang.ClassNotFoundException: Task$1
        at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
        at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:331)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
        ... 38 more
Exception in thread "AWT-EventQueue-0" java.lang.NoClassDefFoundError: Task$1
        at Task.installb(Task.java:38)
        at Install.actionPerformed(Install.java:125)
        at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2022)
        at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2348)
        at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:402)

0 个答案:

没有答案