有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

将具有多个类的netbeans java应用程序转换为。exe

我在netbeans上制作了一个java应用程序。我试着把它转换成。exe4j和launch4j等各种软件的帮助下执行。但问题是,这些软件只占用一个主类。我的申请有3个。因此,我的应用程序没有连接到数据库


共 (2) 个答案

  1. # 1 楼答案

    我个人使用exe4j

    在exe4j中,当您继续向导时,您将获得此类窗口

    enter image description here

    然后,您可以单击绿色+按钮添加您的易激发jar文件,并在general区域指定主类。在这个例子中test

    假设您的应用程序名为newMDIApplication

    在netbeans中,您有3个类

     newMDIApplication.class
     newJframe.class
     newJframe2.class
    

    当应用程序启动时,newMDIApplication.class的主方法将被执行。所以,当您创建一个jar文件时,您应该填充它的主要方法

    在这个exe4j窗口中,您应该选择newMDIApplication作为主类

  2. # 2 楼答案

    你的问题有点令人困惑。。 Mainclass是指包含公共静态void main(字符串[]args)

    函数void main用于启动程序。您可以在void main中初始化程序

    执行开始后,JVM将调用void main函数。。 您不能使用multiplevoid mains,因为JVM会被它搞混

    编辑

    现在我明白你的问题了

    首先从所有gui类中删除此代码

    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(NewJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(NewJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(NewJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(NewJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>
    
        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new NewJFrame().setVisible(true);
            }
        });
    }
    

    在所有Gui类的类构造函数中查找并添加此代码,如果类名为NewJFrame,则类构造函数如下

     public NewJFrame() {
            initComponents();
            setVisible(true);; // add this code
    
        }
    

    如果您只有Gui类。。请添加一个Java类并在其上放置public static void main函数

    Class AppleClass{
    

    //在这里导入您的类

    //This is your main class
    
    public static void main(String[] a){ 
    
    //This function will execute first on program startup
    
    //put your look and feel code here (optional, codes given below)
    
    //Here you can put codes for which  GUI you need to show first
    
    }
    
    }
    

    您可以通过创建GUI对象来显示GUI

    语法

    ClassName objectname = new ClassName();

    ClassName—GUI类的名称

    objectname-任何名称

    例如:

    NewJFrame abc = new NewJFrame();

    外观代码

    //  write it inside public static void main
    
    
     try {
                UIManager.setLookAndFeel(new NimbusLookAndFeel());
            } catch (UnsupportedLookAndFeelException ex) {
                Logger.getLogger(JavaApplication10.class.getName()).log(Level.SEVERE, null, ex);
            }
    

    确保这些类已导入

    • 导入java。util。登录中。水平
    • 导入java。util。登录中。记录器
    • 导入javax。摆动UIManager;进口
    • javax。摆动不受支持的视觉感受;进口
    • javax。摆动普拉夫。灵气。NimbusLookAndFeel

    编辑3-转换为exe

    • 构建可执行jar,转到运行菜单->;清理和生成项目(项目名称)
    • 下载Launch4j并运行它
    • 设置输出exe文件位置
    • 设置可执行Jar位置(您可以在项目目录的dist文件夹中找到它)
    • 转到“类路径”选项卡取消选中“自定义路径”
    • 转到“单一继承”选项卡并取消选中“仅允许单个……”
    • 转到“JRE”选项卡设置最低版本-1.5.0_45,最高版本-1.8.0_45
    • 单击齿轮图标并保存配置
    • 单击运行按钮