简体   繁体   中英

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)

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.

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

Use: java prgStore.appNegozio

appNegozio is inside a package. When compiling a Java program, javac just requires the file name, so passing it appNegozio.java works. 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. Try using the following code in cmd:

cd ..
java prgStore.appNegozio

java.lang.NoClassDefFoundError occur when JVM did not find the particular class in runtime. you can generate all the class file same location and then run the program.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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