Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Some problems about adding external library in IntellijIdea

I have added an external library.

enter image description here

But I can only use it in src.I cannot use stdlib.jar(the external library) in com.jc.Searching. How to fix it?

detailed information: src\example this is ok src\com\jc\Searching\example this one is not ok

like image 649
fei_hsueh Avatar asked Mar 11 '26 03:03

fei_hsueh


2 Answers

According to your answer on my comment above. Usually, you need to provide an import statement in your ST class, which belong to com.ja.Searching package. Here you can read about it.

In your case, you can't do it, because a StdOut class is probably declared in default package within your external library and it's not possible in Java to import classes from unnamed package. Only way to get this class instance, is to try to use reflection, but obviously, that's not the way you need and even have to do.

It seems to me, you are using some lib, which doesn't provide any packages and all it's classes belong to the default one, that is why you don't need to make an import in your class within default package. It's not a good practice at all, but sometimes used in some tutorials to make a code snippets be more readable.

like image 65
Stanislav Avatar answered Mar 12 '26 17:03

Stanislav


Classes in the default package can't be imported. So they can effectively be only used by classes in the default package as well.

It's a really, really bad practice to put classes in the default package, especially for a reusable library. Ask the author of the library to fix it, and use a proper package name.

like image 31
JB Nizet Avatar answered Mar 12 '26 15:03

JB Nizet