102

Possible Duplicate: How to access java-classes in the default-package?


I am using Eclipse 3.5 and I have created a project with some package structure along with the default package. I have one class in default package - Calculations.java and I want to make the use of that class in any of the package (for instance in com.company.calc). When I try to make the use of the class which is in the default package, it's giving me a compiler error. It's not able to recognise the class in default package. Where is the problem?

Calculations.java - source code

public class Calculations {
    native public int Calculate(int contextId);
    native public double GetProgress(int contextId);
    static  {
        System.loadLibrary("Calc");
    }
}

I can't put my class in any other package. This class has some native methods which are implemented in Delphi. If I put that class in any of the folders, I will have to make changes to that DLL which I want to avoid (really - I can not). That's why I put my class in the default package.

Community
  • 1
  • 1
Michał Ziober
  • 31,576
  • 17
  • 81
  • 124
  • 1
    I found this link: http://stackoverflow.com/questions/283816/how-to-access-java-classes-in-the-default-package after creating this questions. – Michał Ziober Feb 03 '10 at 16:33
  • possible duplicate of [What's the syntax to import a class in a default package in Java?](http://stackoverflow.com/questions/2030148/whats-the-syntax-to-import-a-class-in-a-default-package-in-java) – Pops Nov 22 '11 at 19:47
  • Of course, the important bit of this question is that the class **and** its code **has to be in the default package**. As of now, any answer other than to use reflection API *(really, an overkill for something this simple)* is **not** a solution. It's mind-boggling how Java tries to have its cake (discourage default package) and eat it too (make JNI so convoluted, most of us end up using DLLs that require default package). – ADTC Jul 10 '15 at 10:36
  • The reason seems historic, but it does sometimes bite. e.g. ``` T.java: import static a.Foo.*; class T { Bar bar = new Bar(); } a/Foo.java: package a; public class Foo { public static final class Bar { } } ``` The above compiles fine, however, if Foo is placed in default package, it does not work. Thus, I can't statically import Foo.* so as to use shorthand Bar instead of Foo.Bar. – Kedar Mhaswade Feb 05 '16 at 16:01
  • Possible duplicate of [What's the syntax to import a class in a default package in Java?](https://stackoverflow.com/questions/2030148/whats-the-syntax-to-import-a-class-in-a-default-package-in-java) – Cœur Jul 10 '18 at 13:13

9 Answers9

89

From the Java language spec:

It is a compile time error to import a type from the unnamed package.

You'll have to access the class via reflection or some other indirect method.

McDowell
  • 102,869
  • 29
  • 193
  • 261
  • 1
    Wow I really don't know java at all. That was a great help. Guess I'll have to move my class into a package... – anhoppe Jan 20 '16 at 20:14
51

Classes in the default package cannot be imported by classes in packages. This is why you should not use the default package.

matt b
  • 132,562
  • 64
  • 267
  • 334
8

There is a workaround for your problem. You can use reflection to achieve it.

First, create an interface for your target class Calculatons :

package mypackage;

public interface CalculationsInterface {  
    int Calculate(int contextId);  
    double GetProgress(int contextId);  

}

Next, make your target class implements that interface:

public class Calculations implements mypackage.CalculationsInterface {
    @Override
    native public int Calculate(int contextId);
    @Override
    native public double GetProgress(int contextId);
    static  {
        System.loadLibrary("Calc");
    }
}

Finally, use reflection to create an instance of Calculations class and assign it to a variable of type CalculationsInterface :

Class<?> calcClass = Class.forName("Calculations");
CalculationsInterface api = (CalculationsInterface)calcClass.newInstance();
// Use it 
double res = api.GetProgress(10);
Mohammad Banisaeid
  • 1,748
  • 21
  • 32
5

From some where I found below :-

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", new Class[] { String.class });

String fooReturned =
    (String) fooMethod.invoke(fooClass.newInstance(), "I did it");
Radiodef
  • 35,285
  • 14
  • 78
  • 114
Adnan Ghaffar
  • 1,235
  • 7
  • 26
  • 44
  • 2
    Seems like reflection is the only way to go. I have a JNI library that simply refuses to work when in any package other than the default one. I didn't build the DLL so I can't rebuild it. Anything I do has to be in Java. Thank you for saving the day :) – ADTC Jul 10 '15 at 10:58
5

I can give you this suggestion, As far as know from my C and C++ Programming experience, Once, when I had the same kinda problem, I solved it by changing the dll written structure in ".C" File by changing the name of the function which implemented the JNI native functionality. for example, If you would like to add your program in the package "com.mypackage", You change the prototype of the JNI implementing ".C" File's function/method to this one:

JNIEXPORT jint JNICALL
Java_com_mypackage_Calculations_Calculate(JNIEnv *env, jobject obj, jint contextId)
{
   //code goes here
}

JNIEXPORT jdouble JNICALL
Java_com_mypackage_Calculations_GetProgress(JNIEnv *env, jobject obj, jint contextId)
{
  //code goes here
}

Since I am new to delphi, I can not guarantee you but will say this finally, (I learned few things after googling about Delphi and JNI): Ask those people (If you are not the one) who provided the Delphi implementation of the native code to change the function names to something like this:

function Java_com_mypackage_Calculations_Calculate(PEnv: PJNIEnv; Obj: JObject; contextId: JInt):JInt; {$IFDEF WIN32} stdcall; {$ENDIF} {$IFDEF LINUX} cdecl; {$ENDIF}
var
//Any variables you might be interested in
begin
  //Some code goes here
end;



function Java_com_mypackage_Calculations_GetProgress(PEnv: PJNIEnv; Obj: JObject; contextId: JInt):JDouble; {$IFDEF WIN32} stdcall; {$ENDIF} {$IFDEF LINUX} cdecl; {$ENDIF}
var
//Any variables you might be interested in
begin
//Some code goes here
end;

But, A final advice: Although you (If you are the delphi programmer) or them will change the prototypes of these functions and recompile the dll file, once the dll file is compiled, you will not be able to change the package name of your "Java" file again & again. Because, this will again require you or them to change the prototypes of the functions in delphi with changed prefixes (e.g. JAVA_yourpackage_with_underscores_for_inner_packages_JavaFileName_MethodName)

I think this solves the problem. Thanks and regards, Harshal Malshe

3

Unfortunately, you can't import a class without it being in a package. This is one of the reasons it's highly discouraged. What I would try is a sort of proxy -- put your code into a package which anything can use, but if you really need something in the default package, make that a very simple class which forwards calls to the class with the real code. Or, even simpler, just have it extend.

To give an example:

import my.packaged.DefaultClass;

public class MyDefaultClass extends DefaultClass {}
package my.packaged.DefaultClass;

public class DefaultClass {

   // Code here

}
Radiodef
  • 35,285
  • 14
  • 78
  • 114
Jon
  • 1,239
  • 1
  • 10
  • 21
1

Create a new package And then move the classes of default package in new package and use those classes

  • Welcome to StackOverflow! Perhaps you can add more details, to explain better the most important parts of your answer. Have a read of [How do I write a good answer?](https://stackoverflow.com/help/how-to-answer) for more information. Also, once you get more reputation, you will be able to post this as a comment rather than an answer, which would be more appropriate. – Francesco B. Mar 31 '18 at 12:25
-1
  1. Create a new package.
  2. Move your files from the default package to the new one.
Troy Alford
  • 24,997
  • 8
  • 60
  • 77
Evgueni
  • 11
-3
  1. Create "root" package (folder) in your project, for example.

    package source; (.../path_to_project/source/)

  2. Move YourClass.class into a source folder. (.../path_to_project/source/YourClass.class)

  3. Import like this

    import source.YourClass;

Andrew
  • 31,284
  • 10
  • 129
  • 99