简体   繁体   中英

Error when i try to make executable file from .jar file

Exception in thread "main" java.lang.NoClassDefFoundError: org/netbeans/lib/awtextra/AbsoluteLayout

at ApplicationPackage.StartPage.initComponents(StartPage.java:300)
at ApplicationPackage.StartPage.<init>(StartPage.java:57)
at ApplicationPackage.Main.main(Main.java:30)

Caused by: java.lang.ClassNotFoundException: org.netbeans.lib.awtextra.AbsoluteLayout

at java.net.URLClassLoader$1.run(Unknown Source)
at java.net.URLClassLoader$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
... 3 more

My code:

try {

 UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
    } 
    catch (UnsupportedLookAndFeelException | ClassNotFoundException | InstantiationException | IllegalAccessException e) {
        JOptionPane.showMessageDialog(null, e.toString(), "Eroare la aplicarea stilului ferestrei", JOptionPane.ERROR_MESSAGE);
    }

    StartPage frame = new StartPage();
    frame.setVisible(true);      

How to solve this ?

Update: I solve this error. But now a have another question. How I can create a jar file with is in dependencies with a database ? My app write and read from a H2 database file.

NoClassDefFoundError generally means the class isn't found on the classpath.

Here's some simple steps and things to look for (Using Netbeans).

  • When you add the library take these steps

    1. Right click on the [Library] node in the project from the [Projects] explorer tab.
    2. Select [Add Library]. AbsoluteLayout should be the first one.

  • Build you project. All the libraries should be exported to the [lib] folder, if you haven't changed any default build settings. Then you need to check a couple things:

    1. Check to make sure the library is actually there. Go to the [Files] tab (if it's not open, go to [Window]->[Files]) and make sure the library is in the [libs] dir:

      在此处输入图片说明

    2. Then check to make sure the library is on the classpath. Check the MANIFIEST.MF .

      在此处输入图片说明

      You should see the libs\\AbsuluteLayout.jar on the classapth

      Manifest-Version: 1.0
      Ant-Version: Apache Ant 1.9.2
      Created-By: 1.8.0_20-b26 (Oracle Corporation)
      Class-Path: lib/AbsoluteLayout.jar
      X-COMMENT: Main-Class will be added automatically by build
      Main-Class: addjardemo.AddJarDemo

If all this is correct, what this means is that when you run your jar, the jar should be on the same level as the [lib] directory, as the application is dependent on that jar in that exact location (relative to the calling jar). Note: this is not the only way though. For more information, I'd do some research on "classpath"

If you want all the classes included into your jar, you will need to build an "Uber Jar". Not sure how to do this with Netbeans settings (if its even possible), but you can search for similar topics, like this one

this is my function to get data from db

 public Vector getData(String query)throws Exception
    {
        Vector<Vector<String>> dataVector = new Vector<>();

        try (Connection myConnection = ConectDataBase.ConectToBD()) {
            PreparedStatement pst = myConnection.prepareStatement(query);
            ResultSet rs = pst.executeQuery();

            while(rs.next())
            {
                Vector<String> films = new Vector<>();
                films.add(rs.getString(1)); 
                films.add(rs.getString(2)); 
                films.add(rs.getString(3)); 
                films.add(rs.getString(4)); 
                films.add(rs.getString(5));
                films.add(rs.getString(6));
                films.add(rs.getString(7));
                films.add(rs.getString(8));
                films.add(rs.getString(9));
                dataVector.add(films);
            }
        }
        return dataVector;
    }

this is in the frame which contains the extracdet data

public ListOfFilms() throws Exception {

        // extragerea datelor din baza de date
        PopulateDataTable dbengine = new PopulateDataTable();
        data = dbengine.getData("SELECT * FROM DataStructure ORDER BY id DESC");
        initComponents();

}

this is my class to connect to db

public class ConectDataBase {

Connection myConnection;

public static Connection ConectToBD(){

    try {
        Class.forName("org.h2.Driver");
        Connection myConnection = DriverManager.getConnection("jdbc:h2:~//RatedMovies", "", "");

        return myConnection;
    }
    catch (ClassNotFoundException | SQLException e)
    {
        return null;
    }                     
}

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM