Running java in package from command line

2019-01-04 12:35发布

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.

5条回答
聊天终结者
2楼-- · 2019-01-04 13:13

This is because if you are declaring package in your java file, then JAVA compiler believe you are having same folder architecture in your system. In your case Java compiler looking for One as a package and then test.class., or to be very specific just look inside your .class file you can see what path it looking for. Please have a look for below Image (I my case I use Hello and Tester)

.class

As you can see path in image is Hello/Tester(my case example), so architecture should be like Hello->Tester.

And if you are not having same architecture and want to create same while compiling, then use javacp command.

查看更多
ゆ 、 Hurt°
3楼-- · 2019-01-04 13:16

while creating a class with a package if you want to run it from cmd you must created a directory with same name of package put the .class in it and then you can easily run it for example you created a class with name "one" and this class in the package with name pack ,you must run these commands

1 javac one.java
after compilation created a directory with the name pack ,after that run this command
2 java pack.one
Note:
all this must be done in the current working directory and the name "one" i chose it here as file name and main class name we all know the first name used in the first command is file name and second one is main class name

查看更多
Rolldiameter
4楼-- · 2019-01-04 13:18

The following line of Haralan Dobrev code solves the problem.

java -cp ../ one.Test
查看更多
倾城 Initia
5楼-- · 2019-01-04 13:20

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.

查看更多
\"骚年 ilove
6楼-- · 2019-01-04 13:23

If the directory is:

basic\One

Run java from the base directory of the package:

basic>java One.test or basic>One.test <optional arguments>

(ideally the package would be lowercase and the class upper case):

basic>java one.Test

If you get 'does not exist' messages, then the java command cannot find classes you referenced in your class. You can point to them with the -cp option ('.' means 'here', and you can add as many places as you like divided by ';' on Windows and ':' on Linux).

basic>java -cp . one.Test
or
basic>java -cp .;..\..\someJar.jar;c:\someDirectory\classesDirectory  one.Test
查看更多
登录 后发表回答