How to use classes in Referenced Libraries in Eclipse - java

I have imported the algs4.jar file as a referenced library as seen above. All seems to be fine but I can't seem to use or access any of the classes in the library.
Is there an import statement I'm missing?
Image reference:
Note how I'm trying to use BinarySearch from the referenced library but it is not offering me an option to use or import it.

I am taking an Algorithms class via Coursera that utilizes the algs4.jar reference library.
The Problem:
The issue you are having is that the reference library is located in the default package and the source in which you are accessing the reference library is not.
The Solution
All classes in your project need to be in the default package, otherwise the reference libraries (which are located in the default package) will not be recognized. Your project classes need to be in the src directory and you should not declare a package at the beginning of your .java files. Additionally you also need to make sure you've added the reference libraries to your build path.

I am not sure, I did not try this. Some interpretations though.
Is this a standard library or someone created it by himself and gave you?
The problem is class BinarySearch is in default package. You will not be able to import it. If this library is created by you, or by any of your friend, you need to ask your friend to move all classes from default package to a good namespace and then re-create the jar.
If class is accessible, ctr + shift + o should import the class.

Note that the classes in the jar files are all contained in the default package.
If your classes are then contained in a package then they will not be directly accessible.
I submit this answer because I am taking the algorithms course to which these jar files belong and this was the specific cause of my program not working.

If you want to import the classes, you need to download the packaged version of the library. See the Q&A section at the bottom of this webpage.
http://introcs.cs.princeton.edu/java/stdlib/
Q. If I use a named package to structure my code, the compiler can no longer access the libraries in stdlib.jar. Why not?
A. The libraries in stdlib.jar are in the "default" package. In Java, you can't access classes in the default package from a named package. If you need to use our libraries with a named package, you can use the packaged version stdlib-package.jar.

Firstly, BinarySearch can not be created (private Constructor). Use the static Method BinarySearch.rank(int key, int[] array)
Secondly you can access BinarySearch only from default package
Thirdly what about Arrays.binarySearch(array, key) ?

Just delete module_info.java file and the referenced libraries are accessible instantly

Related

Import Statement doen't showing the class file in JAR [duplicate]

I'm working now together with others in a grails project. I have to write some Java-classes. But I need access to an searchable object created with groovy. It seems, that this object has to be placed in the default-package.
My question is: Is there a way to access this object in the default-package from a Java-class in a named package?
You can’t use classes in the default package from a named package.
(Technically you can, as shown in Sharique Abdullah's answer through reflection API, but classes from the unnamed namespace are not in scope in an import declaration)
Prior to J2SE 1.4 you could import classes from the default package using a syntax like this:
import Unfinished;
That's no longer allowed. So to access a default package class from within a packaged class requires moving the default package class into a package of its own.
If you have access to the source generated by groovy, some post-processing is needed to move the file into a dedicated package and add this "package" directive at its beginning.
Update 2014: bug 6975015, for JDK7 and JDK8, describe an even stricter prohibition against import from unnamed package.
The TypeName must be the canonical name of a class type, interface type, enum type, or annotation type.
The type must be either a member of a named package, or a member of a type whose outermost lexically enclosing type is a member of a named package, or a compile-time error occurs.
Andreas points out in the comments:
"why is [the default package] there in the first place? design error?"
No, it's deliberate.
JLS 7.4.2. Unnamed Packages says: "Unnamed packages are provided by the Java SE platform principally for convenience when developing small or temporary applications or when just beginning development".
In fact, you can.
Using reflections API you can access any class so far. At least I was able to :)
Class fooClass = Class.forName("FooBar");
Method fooMethod = fooClass.getMethod("fooMethod", String.class);
String fooReturned = (String)fooMethod.invoke(fooClass.newInstance(), "I did it");
Use jarjar to repackage the jar file with the following rule:
rule * <target package name>.#1
All classes in the default package of the source jar file will move to the target package, thus are able to access.
You can use packages in the Groovy code, and things will work just fine.
It may mean a minor reorganization of code under grails-app and a little bit of a pain at first, but on a large grails project, it just make sense to organize things in packages. We use the Java standard package naming convention com.foo.<app>.<package>.
Having everything in the default package becomes a hindrance to integration, as you're finding.
Controllers seem to be the one Grails artifact (or artefact) that resists being put in a Java package. Probably I just haven't figured out the Convention for that yet. ;-)
just to complete the idea:
From inside default-package you can access objects resided in named packages.

Do I always have to type package name in Java?

Today I started learning Java.
I saw that package automatic gets included in .Java file.
I was wondering if it always need to be included?
Consider specify a common package for all the types within a same project.
In Java is common to start a project with a specific package setting. A package creates a namespace to disambiguate the types that it includes, to play nicelly with other projects that may or may not be in the same classpath. Normally, the package is bound to a URL of the project.
Think of Java packages like C++ namespaces.
A huge project/product written in Java can depend on lots and lots of projects, each described in a different package.
Organizations like Apache have lots of projects, organized under a common package pattern: org.apache.<<name_of_the_project>>.
Consider starting your project with a package named: com.user3552670; or something like your personal site, so persons that will consume your project can relate to the creator.
Yes and no.
It's used to specify the package of the class, read more here.
You could create a class without a package, but your code will look bad..
They exists to avoid conflicts, example between your code and default java package.
If packages doesn't exists, you can't create a class named ArrayList because already exists in Java.
Some IDEs force the fact that, if your .java file is in com/a/b/c folder his package should be com/a/b/c (If i don't remember wrong, IntellIJ IDEA do that)
Yes and no.
It must be there, but the IDE takes care of it (I don't use Netbeans, but I'd bet that it can do it, too). When moving files between packages, it has to be updated, but again, the IDE does it all.

confused with package and import statements including default package

I am confused with the terminology and its use in the eclipse IDE.
It is my understanding that you can use the import keyword to reference specific classes/java files within a group of class/java files called packages. You can also reference an entire group of classes/java files by using a wildcard modifier end the end of the import statement. So why do this when you can just make the package statement at the top of the java src file instead of having to do an import?
for instance:
folder structure: myapp>graphics>.class1, .class2
instead of doing this: import myapp.graphics.*
we can do this right?: package myapp
so whey even have the import keyword? can't you just use package myapp and reference the class with say class1 example = class1();
Second, I tried to remove the package com.example.myapp and just import the classes with import myapp.graphics.* but it it gave me an error stating that "" does not match the expected package com.example.myapp. one of the fixes for it was to move MainActivity.java to a default package. I chose that option and it moved it to a default package and then I was able to use the import myapp.graphcis.*; statement while leaving out the package myapp.graphics statement without any errors.
I am confused. Also, what is a deauflt package? I read somewhere that it is bad practice to use a default package. why?
thanks.
Packages - A location where class is located. It is used to build the fully qualified class name.
Import - Used for incorporating pre-existing classes, by using this you can avail the functionality from them.
Default package - Location directly under src folder, for example: src\NoPackage.java
For example, if NoPackage.java contains import com.assertcheck.AssertTesting; then AssertTesting is references under NoPackage class.
However in AssetTesting class you can't import/use NoPackage.
According to oracle:
A package is a namespace that organizes a set of related classes and
interfaces. Conceptually you can think of packages as being similar to
different folders on your computer. You might keep HTML pages in one
folder, images in another, and scripts or applications in yet another.
Because software written in the Java programming language can be
composed of hundreds or thousands of individual classes, it makes
sense to keep things organized by placing related classes and
interfaces into packages.
So, you can think of packages as organizers, which have nothing to do with imports.
The default package is no package at all. Eclipse warns you about this when you leave the field blank. Using it is bad practice because
Using the default package may create namespace collisions. Imagine you're creating a library which contains a MyClass class. Someone uses
your library in his project and also has a MyClass class in his
default package. What should the compiler do? Package in Java is
actually a namespace which fully identifies your project. So it's
important to not use the default package in the real world projects.
Why shouldn't we use the (default)src package?

Jars with default package

I found this question -> Import custom libraries in Java
And #Andy Thomas-Cramer said that the classes in "stdlib.jar" from "An introduction to programming in Java" have no packages, so they are in the default package.
Isn't this a bad practice? If you have something with no package the IDEs' auto-completion is quite slower. And also this means that we could not use any of the classes, in that jar, from classes with packages different then the default?
Can someone please tell me how we could deal with this?
EDIT:
I have 2 jars and I put them in Referenced libraries, they both have a bunch of classes in default package. When I create class in different package then the default - lets say org.myquestion I can't access the classes from the jars anymore.
This is something that really bugs me... First I can't create my own package and use anything from the jars. Second my IDE's (I use eclipse) auto-complete goes terrible - I guess it searches to meany classes at once... What I want to do is to put somehow the jars in some namespace... and to be able to access them like org.someones.libs.SomeClass
It certainly is bad practice to use the default package. A package groups classes and provides them with access protection (protected, package private) and functions as a unique namespace.
You can always use classes from every package, them being default or not, you can always mix.
Download the jar source code, And built it to jar by yourself and added the package name whatever your like.That's will solve your problem.
Importing classes inside JAR files that are in the default package
I ran into the exactly same problem as you did. The problem is the jar file "stdln.jar" has no named package, say, only with default package.
You cannot import a class from a default package, basically, since the import operation needs the package name:
import packagename.*;
So there are only two way to fix this problem:
the easier one: Do not create a package in your src folder and use default package two! Every class in stdln.jar would be imported to your src automatically.
Like this:
enter image description here
try to create your own jar file with a named package and copy all the class file into your newly-created jar file.
Since the stdln.jar is only used for education, so which you are gonna choose does not really matter. In real development, we never use default named package since it's not really a good practice, always leading to some confusing stuff.
Hope this would help you!

accessing referenced libraries from packages in eclipse

I can only access referenced library classes if I save my classes in the default package. If I try to access them from any other package I get "className cannot be resolved". Any idea why this could happen?
That package is from the standard library of Princeton's IntroCS Course after a quick Google.
If you follow down to the FAQ on the page
http://introcs.cs.princeton.edu/java/stdlib/
Q. If I use a named package to structure my code, the compiler can no
longer access the libraries in stdlib.jar. Why not?
A. The libraries
in stdlib.jar are in the "default" package. In Java, you can't access
classes in the default package from a named package. If you need to
use our libraries with a named package, you can use the packaged
version stdlib-package.jar.
Download package jar file:
http://introcs.cs.princeton.edu/java/stdlib/stdlib-package.jar
Right click project folder and add external JAR.
import edu.princeton.cs.introcs.*;
Add above line to the classes where you need to reference the classes.
The first line references the correct package name and the * wildcard imports all the classes within it.
:) Hope that helps.
//Edit - If you right click the project folder and use "Organize Imports" it will be faster so you don't have to manually add to each class.
Check if you've proper import declarations and the type you are refering to has public access modifier.
Import declaration for Types in the default package are per definition impossible. JLS specifies that it is a compile time error to import a type from an unnamed package.
You must access your class via reflections or much better do not ever use the default package. Eclipse should show you a warning when you want to create a type inside default package, because it's generally discouraged.
If you're using an IDE like Eclipse then try hitting CTRL+SPACE behind the type name in the class where you want to use it. Eclipse should give you all matching opportunities and will add the import automatically for you if you select your class.
You need to import the package into other package.
Classes in one package cannot directly reference to other package unless its get referenced.

Categories