Running java in package from command line

user2756339 picture user2756339 · Oct 17, 2013 · Viewed 92.3k times · Source

I have read the previously posted questions. Some are vague and none solved my problem so I am forced to ask again.

I have two simple classes,

package One;
import One.Inner.MyFrame;
public class test
{
    public static void main(String args[])
    {
        MyFrame f= new MyFrame();
    }
}

And the other class is,

package One.Inner;
import java.awt.*;
import javax.swing.*;

public class MyFrame extends JFrame
{
    public MyFrame()
    {
        setPreferredSize(new Dimension(400,560));
        setVisible(true);
    }
}

I am at base folder "basic" in Windows cmd. I compile using

basic> javac *.java -d .

A folder and subfolder is created.

cd One
basic\One> java test

This generates a big set of errors. Many answers directed to specify the full path which didn't work. My classes are in One so specifying One using -cp didn't work either.

Answer

Jon Skeet picture Jon Skeet · Oct 17, 2013

You'd run it as:

java One.Test

... but from the root directory (basic), not from the One directory. You always specify the fully-qualified class name.

Oh, and package names in Java should be lower-case, so it should be one and one.inner, not One and One.Inner. Just a convention, but one which pretty much everyone follows.