如何将我的 java 程序复制到另一台计算机?

How can I copy my java program to another computer?

我写了一个简单的 java 程序来模拟一个神奇的 8 球,我想我儿子会喜欢玩它。我使用 Netbeans 作为我的 IDE,但我希望能够复制该程序并将其放在我让他使用的旧笔记本电脑上。问题是我不知道如何复制程序并将其放在闪存驱动器上以将其复制到他的笔记本电脑。

我开始学习 C++,第二学期后就停了,现在我决定自学 Java。在 C++ 中,我只需要将它保存为 .exe 文件,我就可以 运行 它在任何地方。我知道 java 做事的方式不同(尽管我也不完全理解该主题)并且我已经看到了类似问题的其他答案,但我想知道是否有一种方法可以加载程序到他不需要编译的计算机,然后 运行 程序。理想情况下,他只需单击桌面上的图标即可启动应用程序。

如果有帮助,我复制了下面的代码。我提前道歉,因为我从来没有在这里发布过问题,所以我确定我违反了某种礼节。

public static void main(String[] args) {

    Random randInt = new Random();
    int num = randInt.nextInt(20) + 1;
    String input;
    char choice = 'n';
    boolean end = false;

    do{
        JOptionPane.showInputDialog (null, "Ask me a question. I know all things.\nWhat is your question?");
        num = randInt.nextInt(20) + 1;

        switch (num){

            case 1 : 
                JOptionPane.showMessageDialog(null, "Outlook is not good.");
                break;
            case 2 : 
                JOptionPane.showMessageDialog(null, "The answer you're looking for is not in here.");
                break;    
            case 3 : 
                JOptionPane.showMessageDialog(null, "Most definitely yes.");
                break;    
            case 4 : 
                JOptionPane.showMessageDialog(null, "It is decidedly true.");
                break;
            case 5 : 
                JOptionPane.showMessageDialog(null, "It is certainly so.");
                break;
            case 6 : 
                JOptionPane.showMessageDialog(null, "The stars tell me it is so.");
                break;
            case 7 : 
                JOptionPane.showMessageDialog(null, "That's a stupid question. Pick another.");
                break;
            case 8 : 
                JOptionPane.showMessageDialog(null, "The answer will come to you in time.");
                break;
            case 9 : 
                JOptionPane.showMessageDialog(null, "Yes - without a doubt.");
                break;
            case 10 : 
                JOptionPane.showMessageDialog(null, "It is a certainty written in prophecy.");
                break;
            case 11 : 
                JOptionPane.showMessageDialog(null, "Chances are good.");
                break;
            case 12 : 
                JOptionPane.showMessageDialog(null, "Most likely.");
                break;
            case 13 : 
                JOptionPane.showMessageDialog(null, "Count on it.");
                break;
            case 14 : 
                JOptionPane.showMessageDialog(null, "Play the lottery instead. There is a better chance of that happening.");
                break;
            case 15 : 
                JOptionPane.showMessageDialog(null, "LOL! Absolutely not!");
                break;
            case 16 : 
                JOptionPane.showMessageDialog(null, "Not a chance");
                break;
            case 17 : 
                JOptionPane.showMessageDialog(null, "The stars are not aligned. No.");
                break;
            case 18 : 
                JOptionPane.showMessageDialog(null, "I don't know. Try asking a Ouija board.");
                break;
            case 19 : 
                JOptionPane.showMessageDialog(null, "It is so.");
                break;
            case 20 : 
                JOptionPane.showMessageDialog(null, "I'll get back to you on that.");
                break;
        }
        input = JOptionPane.showInputDialog (null, "Would you like to ask another question? Y/N");
        choice = input.charAt(0);

            if (choice == 'n' || choice == 'N'){
                end = true;                    
            }
            if (choice == 'y' || choice == 'Y'){
                end = false;
            }
            else{
                do{
                    input = JOptionPane.showInputDialog(null, "I didn't understand. Would you like to ask another question?\nPlease enter\nY for yes\nor\nN for no");
                    choice = input.charAt (0);
                    if (choice == 'n' || choice == 'N'){
                        end = true;                            
                    }
                    if (choice == 'y' || choice == 'Y'){
                        end = false;
                    }
                }while (choice!='n'&& choice!='N'&&choice!='y'&&choice!='Y');
            }
    }while(!end);                       
}

您需要 JRE(Java 运行 时间环境)才能 运行 java 程序。如果目标计算机中有 JRE,则构建 JAR 文件。参考官方Doc。之后,您可以使用以下命令运行 jar 文件。

java -jar JarExample.jar
  1. https://docs.oracle.com/javase/tutorial/deployment/jar/build.html

刚回到 Whosebug 寻找其他东西,发现我从未真正回答过这个问题。对于可能正在寻找同样东西的任何人,我会 post 对我有用的东西(使用 NetBeans IDE):

转到项目 window (Ctrl + 1) 找到您想要制作成 .jar 的项目,然后右键单击它 属性>>打包>>选中所有三个框并点击确定 找到您的 NetBeansProjects 文件夹(我相信默认在文档中) 你的项目>> dist

您应该会在那里看到您的 .jar 文件。 我相信您还需要包括 lib 文件夹(如果有的话)才能正确 运行 项目