Java - unable to import com.package. * (Wildcard)

This might be a simple problem, but Google didn’t return anything.

I read Help with packages in java - import does not work

I still don't understand why direct imports will work, but there won't be a wildcard. [EDIT] By class package, I mean class package. I'm still new to Java, so I don’t know the semantics of [EDIT]

I have a class package: com.company.functions when I try to import com.company.* get the following error.

 java: package com.company does not exist 

If I import com.company.function explicitly, I have no problem.

So, I suppose I have a solution, but for the sake of training, can someone explain why I see this problem?

IDE: IntelliJ IDEA 12

 import com.sociapathy.*; <--Throws compile error java: package com.sociapathy does not exist import com.sociapathy.databaseAccess.MySQL; <--Works just fine 
+4
source share
2 answers

It looks like you are trying to import a package that does not contain classes, but contains only subpackages.

i.e. You have classes in com.company.functions - for example. com.company.functions.Foo
But there are no classes directly in com.company - for example. com.company.Bar

Although java packages look hierarchical, they are not for import purposes.

So, you cannot import com.company.* Because it does not contain its own classes.
You can import com.company.functions.* Because it contains classes
And you can import com.company.functions.Foo because it is a class.

You may now be tempted to create the Bar class at com.company . This will allow you to import com.company.*
But , because import is not processed hierarchically, which will not lead to importing classes in com.company.functions
You still need to explicitly import com.company.functions.Foo or the wildcard com.company.functions.*

+6
source

In java you can import the whole package:

 import package.name.*; 

Or you can import a specific member of the package

 import package.name.class_name; 

Do not confuse periods in package names with the member access operator β€” they are just literal periods. You cannot try to import multiple packages by breaking package names.

 import package.*; //doesn't work import packa*; //doesn't work for the same reason 
+1
source

Source: https://habr.com/ru/post/1484960/


All Articles