Running java in package from command line
Asked Answered
S

5

43

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.

Succinctorium answered 17/10, 2013 at 17:28 Comment(1)
just to add some good stuff regarding the question: webucator.com/how-to/how-compile-packages-java.cfmSeraphim
A
64

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.

Asteria answered 17/10, 2013 at 17:29 Comment(4)
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.Succinctorium
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.Warsle
Oh, here it is: java -cp ../ one.Test. I was sure I've tried it before.Warsle
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'!Ballentine
T
24

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
Tidewaiter answered 8/12, 2014 at 23:41 Comment(0)
R
5

The following line of Haralan Dobrev code solves the problem.

java -cp ../ one.Test
Regurgitation answered 22/7, 2018 at 18:46 Comment(0)
T
2

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

Toandfro answered 8/11, 2016 at 21:25 Comment(1)
You should use formatting to ease the comprehension of your answer.Juryrigged
K
2

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.

Knp answered 7/9, 2017 at 14:58 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.