如何通过cmd运行java程序

时间:2013-12-08 01:31:34

标签: java cmd

我完成了我的小应用程序。所以现在我想通过命令提示符查看结果。 (在日食中运作良好)。

第一步我决定(确定)通过命令提示符编译我的程序:

javac appNegozio.java

我编译没有错误实际上我有我的文件.class(所有程序只有1个类,但我有一些内部类,所以我有超过1个文件.class)

现在,如果我尝试运行我的程序:

java appNegozio

我在提示符上有这个问题:

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)

但我不明白为什么......以及我要做的......

这是我的代码: >

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


谢谢

4 个答案:

答案 0 :(得分:1)

我认为有两个可能的原因:

  1. 您的类名与您的文件名不同(自编译以来可能不是这种情况。)
  2. 执行编译代码时缺少包。如果是这样的话:
  3. java prgStore.appNegozio会做的。

    当/如果你添加一些代码我将尝试更新我的答案。

    除了;

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

    不是问题。

答案 1 :(得分:1)

从文件夹prgStore外部

使用: java prgStore.appNegozio

答案 2 :(得分:1)

appNegozio位于包内。编译Java程序时,javac只需要文件名,因此传递它appNegozio.java有效。但是,在运行程序时,java要求您将完整的类名,包和所有内容传递给它,并希望将此数据从包文件夹外部传递给它。尝试在cmd中使用以下代码:

cd ..
java prgStore.appNegozio

答案 3 :(得分:0)

当JVM在运行时未找到特定类时,会发生java.lang.NoClassDefFoundError。你可以生成所有类文件的相同位置,然后运行程序。