简体   繁体   English

如何通过cmd运行Java程序

[英]How to run a java program by cmd

I finished my little app. 我完成了我的小应用程序。 so now i would like to see the result by command prompt. 所以现在我想通过命令提示符查看结果。 (in eclipse works well). (在日食中效果很好)。

first step i decided (to be sure) to compile by command prompt my program: 我决定(确定)第一步要通过命令提示符编译我的程序:

javac appNegozio.java

i compiled without error in fact i have my files .class (all the program have just 1 class but i have some inner class and so i have more than 1 file .class) 我编译时没有错误,实际上我有我的文件.class(所有程序只有1个类,但是我有一些内部类,所以我有多个文件.class)

now if i try to run my program: 现在,如果我尝试运行程序:

java appNegozio

i have this problem on the prompt: 我在提示符下有这个问题:

Exception in thread "main" java.lang.NoClassDefFoundError: appNegozio (wrong nam
    e: prgStore/appNegozio)
    at java.lang.ClassLoader.defineClass1(Native Method)
    at java.lang.ClassLoader.defineClass(Unknown Source)
    at java.security.SecureClassLoader.defineClass(Unknown Source)
    at java.net.URLClassLoader.defineClass(Unknown Source)
    at java.net.URLClassLoader.access$100(Unknown Source)
    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)
    at sun.launcher.LauncherHelper.checkAndLoadMain(Unknown Source)

but i don't understand why... and what i have to do... 但我不明白为什么...以及我该怎么做...

this is my code: > 这是我的代码:>

package prgStore;

import java.awt.BorderLayout;
import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.SpringLayout;
import javax.swing.JTextField;
import javax.swing.JLabel;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

public class appNegozio extends JFrame {

private JPanel contentPane;
private JTextField textField;
private JTextField textField_1;

    /**
     * Launch the application.
     */
    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    appNegozio frame = new appNegozio();
                    frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    /**
     * Create the frame.
     */
    public appNegozio() {
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setBounds(100, 100, 450, 300);
        contentPane = new JPanel();
        contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
        setContentPane(contentPane);
        SpringLayout sl_contentPane = new SpringLayout();
        contentPane.setLayout(sl_contentPane);

        textField = new JTextField();
        sl_contentPane.putConstraint(SpringLayout.NORTH, textField, 10, SpringLayout.NORTH, contentPane);
        sl_contentPane.putConstraint(SpringLayout.WEST, textField, 62, SpringLayout.WEST, contentPane);
        contentPane.add(textField);
        textField.setColumns(10);

        textField_1 = new JTextField();
        sl_contentPane.putConstraint(SpringLayout.NORTH, textField_1, 16, SpringLayout.SOUTH, textField);
        sl_contentPane.putConstraint(SpringLayout.WEST, textField_1, 0, SpringLayout.WEST, textField);
        contentPane.add(textField_1);
        textField_1.setColumns(10);

        JLabel lblNome = new JLabel("Nome");
        sl_contentPane.putConstraint(SpringLayout.NORTH, lblNome, 10, SpringLayout.NORTH, contentPane);
        sl_contentPane.putConstraint(SpringLayout.EAST, lblNome, -7, SpringLayout.WEST, textField);
        contentPane.add(lblNome);

        JLabel lblCognome = new JLabel("Cognome");
        sl_contentPane.putConstraint(SpringLayout.NORTH, lblCognome, 0, SpringLayout.NORTH, textField_1);
        sl_contentPane.putConstraint(SpringLayout.EAST, lblCognome, -6, SpringLayout.WEST, textField_1);
        contentPane.add(lblCognome);

        JButton btnSubmit = new JButton("Submit");
        btnSubmit.addActionListener(new ActionListener() {
            int a, b;
            public void actionPerformed(ActionEvent e) {
                System.out.println();
            }
        });
        sl_contentPane.putConstraint(SpringLayout.NORTH, btnSubmit, 24, SpringLayout.SOUTH, textField_1);
        sl_contentPane.putConstraint(SpringLayout.WEST, btnSubmit, 10, SpringLayout.WEST, contentPane);
        contentPane.add(btnSubmit);
    }
}

    package prgStore;

import java.awt.BorderLayout;
import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.SpringLayout;
import javax.swing.JTextField;
import javax.swing.JLabel;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

public class appNegozio extends JFrame {

    private JPanel contentPane;
    private JTextField textField;
    private JTextField textField_1;

    /**
     * Launch the application.
     */
    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    appNegozio frame = new appNegozio();
                    frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    /**
     * Create the frame.
     */
    public appNegozio() {
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setBounds(100, 100, 450, 300);
        contentPane = new JPanel();
        contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
        setContentPane(contentPane);
        SpringLayout sl_contentPane = new SpringLayout();
        contentPane.setLayout(sl_contentPane);

        textField = new JTextField();
        sl_contentPane.putConstraint(SpringLayout.NORTH, textField, 10, SpringLayout.NORTH, contentPane);
        sl_contentPane.putConstraint(SpringLayout.WEST, textField, 62, SpringLayout.WEST, contentPane);
        contentPane.add(textField);
        textField.setColumns(10);

        textField_1 = new JTextField();
        sl_contentPane.putConstraint(SpringLayout.NORTH, textField_1, 16, SpringLayout.SOUTH, textField);
        sl_contentPane.putConstraint(SpringLayout.WEST, textField_1, 0, SpringLayout.WEST, textField);
        contentPane.add(textField_1);
        textField_1.setColumns(10);

        JLabel lblNome = new JLabel("Nome");
        sl_contentPane.putConstraint(SpringLayout.NORTH, lblNome, 10, SpringLayout.NORTH, contentPane);
        sl_contentPane.putConstraint(SpringLayout.EAST, lblNome, -7, SpringLayout.WEST, textField);
        contentPane.add(lblNome);

        JLabel lblCognome = new JLabel("Cognome");
        sl_contentPane.putConstraint(SpringLayout.NORTH, lblCognome, 0, SpringLayout.NORTH, textField_1);
        sl_contentPane.putConstraint(SpringLayout.EAST, lblCognome, -6, SpringLayout.WEST, textField_1);
        contentPane.add(lblCognome);

        JButton btnSubmit = new JButton("Submit");
        btnSubmit.addActionListener(new ActionListener() {
            int a, b;
            public void actionPerformed(ActionEvent e) {
                System.out.println();
            }
        });
        sl_contentPane.putConstraint(SpringLayout.NORTH, btnSubmit, 24, SpringLayout.SOUTH, textField_1);
        sl_contentPane.putConstraint(SpringLayout.WEST, btnSubmit, 10, SpringLayout.WEST, contentPane);
        contentPane.add(btnSubmit);
    }
}


thank you 谢谢

I see two possible reasons: 我看到两个可能的原因:

  1. Your classname is different than your file name (might not be the case since it is compiled.) 您的类名不同于文件名(由于已编译,所以可能不是这种情况)。
  2. You are missing package while executing your compiled code. 您在执行已编译的代码时缺少软件包。 If it is the case: 如果是这样:

java prgStore.appNegozio will do the thing. java prgStore.appNegozio将执行此操作。

when/if you add some code I will try to update my answer. 当/如果您添加一些代码,我将尝试更新我的答案。

Besides; 除了;

(all the program have just 1 class but i have some inner class and so i have more than 1 file .class)

is not a problem. 不是问题。

From outside the folder prgStore 从文件夹prgStore外部

Use: java prgStore.appNegozio 使用:java prgStore.appNegozio

appNegozio is inside a package. appNegozio在包装内。 When compiling a Java program, javac just requires the file name, so passing it appNegozio.java works. 编译Java程序时, javac仅需要文件名,因此将appNegozio.java传递给它appNegozio.java However, when running a program, java requires that you pass it the full class name, package and all, and wants this data passed to it from outside the package folder. 但是,在运行程序时, java要求您向其传递完整的类名,程序包及所有类,并希望将此数据从程序包文件夹外部传递给它。 Try using the following code in cmd: 尝试在cmd中使用以下代码:

cd ..
java prgStore.appNegozio

java.lang.NoClassDefFoundError occur when JVM did not find the particular class in runtime. 当JVM在运行时找不到特定的类时,将发生java.lang.NoClassDefFoundError。 you can generate all the class file same location and then run the program. 您可以在相同位置生成所有类文件,然后运行程序。

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

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