0

I am unable to access Java classes in the default package from jshell. I cannot import since the default package has no name and when I try to reference the name then I have an error:

jshell> Test.test(); | Error: | cannot find symbol | symbol:
variable Test | Test.test(); | ^--^

The only solution I found so far is using:

/open Test.java

Is it possible or is it a bug?

Here the code of Test.java:

public class Test {
    public static void test() {
        System.out.println("test");
    }
    public static void main(String[] args) {
        Test.test();
    }
}
Pierre Thibault
  • 1,895
  • 2
  • 19
  • 22
  • for using `Test.test()`, did you create a `Test` class within `jshell`? – Naman Oct 05 '19 at 16:20
  • No. But the class is in the class path. If I modify the source for Test, add a package name and recompile. I can import the class in jshell and use it without problem. – Pierre Thibault Oct 05 '19 at 23:57
  • That is quite possible, given that every class in Java-9 should reside within a package else the compilation fails. But not sure, why `jshell` doesn't warn you of that while the classes are added to the classpath. – Naman Oct 06 '19 at 02:14
  • I am compiling with Java 12 and I have no warning for using the default package. – Pierre Thibault Oct 06 '19 at 17:05
  • Can you share the implementation of the `Test` class as well? – Naman Oct 06 '19 at 18:54

1 Answers1

1

This is not possible. From the docs,

Code in the default package, which is also known as the unnamed package, can’t be accessed from JShell.