user2756339 user2756339 - 1 month ago 16
Java Question

Running java in package from command line

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 didnt work.
My classes are in One so specifing One using -cp didnt work either.

I am very confused. Please help.

Answer

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.