简体   繁体   English

当我尝试从.jar文件制作可执行文件时出错

[英]Error when i try to make executable file from .jar file

Exception in thread "main" java.lang.NoClassDefFoundError: org/netbeans/lib/awtextra/AbsoluteLayout 线程“主”中的异常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 引起原因: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 ? 如何创建与数据库具有依赖关系的jar文件? My app write and read from a H2 database file. 我的应用程序从H2数据库文件读写。

NoClassDefFoundError generally means the class isn't found on the classpath. NoClassDefFoundError通常意味着在类路径上找不到该类。

Here's some simple steps and things to look for (Using Netbeans). 这是一些简单的步骤和需要寻找的东西(使用Netbeans)。

  • When you add the library take these steps 添加库时,请执行以下步骤

    1. Right click on the [Library] node in the project from the [Projects] explorer tab. 在[Projects]资源管理器选项卡中,右键单击项目中的[Library]节点。
    2. Select [Add Library]. 选择[添加库]。 AbsoluteLayout should be the first one. AbsoluteLayout应该是第一个。

  • Build you project. 建立您的专案。 All the libraries should be exported to the [lib] folder, if you haven't changed any default build settings. 如果尚未更改任何默认的构建设置,则所有库都应导出到[lib]文件夹。 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: 转到[Files]选项卡(如果未打开,请转到[Window]-> [Files]),并确保该库位于[libs]目录中:

      在此处输入图片说明

    2. Then check to make sure the library is on the classpath. 然后检查以确保库位于类路径中。 Check the MANIFIEST.MF . 检查MANIFIEST.MF

      在此处输入图片说明

      You should see the libs\\AbsuluteLayout.jar on the classapth 您应该在libs\\AbsuluteLayout.jar上看到libs\\AbsuluteLayout.jar AbsuluteLayout.jar

      Manifest-Version: 1.0 清单版本:1.0
      Ant-Version: Apache Ant 1.9.2 Ant版本:Apache Ant 1.9.2
      Created-By: 1.8.0_20-b26 (Oracle Corporation) 创建时间:1.8.0_20-b26(Oracle公司)
      Class-Path: lib/AbsoluteLayout.jar 类路径:lib / AbsoluteLayout.jar
      X-COMMENT: Main-Class will be added automatically by build X-COMMENT:Main-Class将通过构建自动添加
      Main-Class: addjardemo.AddJarDemo 主类: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). 如果所有步骤都正确,则意味着运行jar时,jar应该与[lib]目录位于同一级别,因为应用程序在那个确切位置(相对于调用jar而言)依赖于该jar。 )。 Note: this is not the only way though. 注意:这不是唯一的方法。 For more information, I'd do some research on "classpath" 有关更多信息,我将对“ classpath”进行一些研究

If you want all the classes included into your jar, you will need to build an "Uber Jar". 如果要将所有类都包含 jar中,则需要构建一个“ 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 不确定如何使用Netbeans设置执行此操作(如果可能的话),但是您可以搜索类似的主题,例如主题

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 这是在包含Extracdet数据的帧中

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;
    }                     
}

} }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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