Running java in package from command line

103,849

Solution 1

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.

Solution 2

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

Solution 3

The following line of Haralan Dobrev code solves the problem.

java -cp ../ one.Test

Solution 4

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

Solution 5

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.

Share:
103,849
user2756339
Author by

user2756339

Updated on July 23, 2020

Comments

  • user2756339
    user2756339 almost 4 years

    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.

  • user2756339
    user2756339 over 10 years
    The directory from which it must be done was really important.I got that wrong earlier.TY....Noted. I was under the false impression that only javas inbuilt packages must be in small.
  • Haralan Dobrev
    Haralan Dobrev almost 10 years
    Is there a way to run it from the same directory? For example specifying a working directory via a command line option? I am searching a lot for this and everyone says "just go one dir up", but it's really annoying in some scenarios.
  • Haralan Dobrev
    Haralan Dobrev almost 10 years
    Oh, here it is: java -cp ../ one.Test. I was sure I've tried it before.
  • jdurston
    jdurston over 8 years
    Yes - In the man file for the Java application launcher java, it states: "By default, the first argument without an option is the name of the class to be called. A fully qualified class name should be used." I think they should change this to 'A fully qualified class name must be used'!
  • binarysubstrate
    binarysubstrate over 8 years
    The example with -cp flag is helpful. +1
  • Abhishek Tiwari
    Abhishek Tiwari over 7 years
    Thanks, Its helpful.
  • Nicolas Henneaux
    Nicolas Henneaux over 7 years
    You should use formatting to ease the comprehension of your answer.