Access a class no package Java [duplicate] - java

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.

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.

How to import a class from default package using reflection api [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.

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?

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.

How to use classes in Referenced Libraries in Eclipse

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

Categories