2

this is my file tree:

$ tree
.
├── Hi.java
├── com
│   └── libai688
│       ├── User.class
│       └── User.java

this is my Hi.java:

import com.libai688;

public class Hi {
    public static void main(String[] args) {
        User p1 = new User();
    }
}

while I am trying to compile Hi.java

$javac Hi.java
Hi.java:1: error: package com does not exist
import com.libai688;
          ^
Hi.java:5: error: cannot find symbol
        User p1 = new User();
        ^
  symbol:   class User
  location: class Hi
Hi.java:5: error: cannot find symbol
        User p1 = new User();
                      ^
  symbol:   class User
  location: class Hi
3 errors

it throws a long error, I carefully check it with some other project, but I still can not find what is wrong with it.

As I know, in some other language, if you want to import a customized module it should write a relative path. But in Java, I am confused with how to import a third-party module or a customized muddle.

This is my User.java

package com.libai688;

public class User{
    public String name;
    public int age;
    public User(String name, int age){
        this.name = name;
        this.age = age;
    }
}
Shuai Li
  • 2,426
  • 4
  • 24
  • 43
  • Possible duplicate of [Including jars in classpath on commandline (javac or apt)](https://stackoverflow.com/questions/2096283/including-jars-in-classpath-on-commandline-javac-or-apt) – f1sh Jul 04 '18 at 08:49
  • 1
    This is not a duplicate. Just invalid usage of the `import` statement. – am9417 Jul 04 '18 at 08:55

1 Answers1

2

The way import com.libai688; is not good.


  • You need to import the class you need :

    import com.libai688.User;
    
  • Or the whole package stuff :

    import com.libai688.*;
    
azro
  • 53,056
  • 7
  • 34
  • 70
  • here's an interesting post about whether or not to include a package with a wildcard: https://stackoverflow.com/questions/147454/why-is-using-a-wild-card-with-a-java-import-statement-bad – Vincent Gerris Jan 30 '19 at 12:40