Alpha Code
Alpha Code

Reputation: 21

How to compile and run java with package

HI I'm having trouble with my java compille I made folder named 'Test'. In this folder i make two folders, one is src, another one is bin. Then I made Test. java in that src folder

package Test;
import java.io.*;
public class Test {
    public static void main(String args[]) {
            System.out.println("hi");
    }
}

i saved it and back at Test folder and then i compile like this

javac -d bin src/Test.java

Thus, i have Test folder in bin folder.

finally in Test folder i write this command

java -cp bin/Test Test

unfortunately, it says can't find Test class

How can i run this code???

Upvotes: 0

Views: 206

Answers (2)

guninvalid
guninvalid

Reputation: 477

"-cp" expects a directory, not a file. Give it the ./bin/ directory, not the file you're trying to execute.

java -cp bin Test.Test

Upvotes: 2

Elliott Frisch
Elliott Frisch

Reputation: 201517

When your class is in a package, the name of the class includes the package. Thus Test.Test is the Test class in the Test package. -cp bin tells java that the classpath starts in bin.

java -cp bin Test.Test
#  classpath main-class

Upvotes: 3

Related Questions