Getting error while executing java programs of packages from command line

31
August 24, 2019, at 9:40 PM

I'm executing the programs from command line and using packages in them. my program file names are TestA.java and TestB.java.

I've executed below initially javac TestA.java

No issues for the above and it generated the class file as well for the following i'm observing the issue javac TestB.java

output :

TestB.java:2: error: '.' expected
import TestA;
            ^
TestB.java:2: error: ';' expected
import TestA;
             ^
2 errors

and the TestA.java file is

package a.b;
class TestA {
    public static void methodPublic(){
        methodPrivate();
    }
    protected static void methodProtected(){
        methodPrivate();
    }
    static void methodDefault(){
        methodPrivate();
    }
    private static void methodPrivate(){}
}

TestB.java content is :

package a.b;
import TestA;
public class TestB {
    public static void main(String args[]) {
        TestA.methodPublic();
        TestA.methodProtected();
        TestA.methodDefault();
    }
    public static void methodPublic() {
    }
    protected static void methodProtected() {
    }
    static void methodDefault() {
    }
    private static void methodPrivate() {
    }
}

I'm executing the javac by navigating to b folder where these two files exist.

Answer 1

I'm executing the javac by navigating to b folder where these two files exist.

You don't want to do that; the fully qualified class name of every class includes the package. They form a tree. Much like your filesystem. From the b folder move up two directories (to the folder containing a - e.g. cd ../.. or cd ..\.. on Windows). Then

javac -cp . a/b/TestA.java a/b/TestB.java

Also, you would normally want that to be written to a "binary" output folder. So

javac -cp . -d bin a/b/TestA.java a/b/TestB.java

Finally, you don't need to import TestA because it is in the same package as TestB. But, if you want to you need

import a.b.TestA;
READ ALSO
Connection Pooling is not working for DB2 database through UCP

Connection Pooling is not working for DB2 database through UCP

We are writing a simple standalone java batchWe are using DB2 database

55
Which are the advantages of Singleton EJB and Asynchrnous methods against single execution timer service?

Which are the advantages of Singleton EJB and Asynchrnous methods against single execution timer service?

I am writing this post because I am doing an huge refactoring of some old codeIn our Java EE product, running under Wildfly 10 with EJB 3

47
How to display the notification without playing any sound

How to display the notification without playing any sound

I need to show a notification without playing a notification sound using notification builder

50
Classifier does not have a companion object , and thus must be initialized here

Classifier does not have a companion object , and thus must be initialized here

Mates, I am very new to kotlin and springI am trying to throw exepiion if record did not exist

46