How to import a class from the default package

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




I am using Eclipse 3.5 and I created a project with some package structure along with the default package. I have one class in the default package - Calculations.java , and I want to use this class in any package (for example, com.company.calc ). When I try to use a class that is in the default package, it gives me a compiler error. It cannot recognize the class in the package by default. 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 cannot put my class in any other package. This class has some proprietary methods that are implemented in Delphi. If I put this class in any of the folders, I will have to make changes to the DLL that I want to avoid (in fact - I can not). So I put my class in the default package.

+87
java jni java-native-interface
Feb 03 '10 at 15:54
source share
9 answers

From the Java language specification :

This is a compile-time error for importing a type from an unnamed package.

You will need to access the class through reflection or some other indirect method.

+79
Feb 03 '10 at 16:02
source share

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

+41
03 Feb '10 at 15:59
source share

There is a solution for your problem. You can use reflection to achieve this.

First create an interface for your target Calculatons class:

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

Then, make your target class implement this 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 the 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); 
+7
Sep 29 '16 at 9:38 on
source share

I can give you this suggestion, as far as I know from my experience in programming in C and C ++, Once, when I had the same problem, I solved it by changing the structure of the dll data in the ".C" file, changing the function name , which implemented the built-in JNI function. For example, if you want to add your program to the package "com.mypackage", you change the prototype of the JNI that implements the ".C" function / file method:

 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 cannot guarantee you, but I will say it at last (I learned a few things after I entered Delphi and JNI): Ask these people (if you are not the one) who provided Delphi with their own code implementation, to change 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, the final tip: although you (if you are a delphi programmer) or they 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 and again. Since this again will require you or their changing function prototypes in delphi with the changed prefixes (e.g. JAVA_yourpackage_with_underscores_for_inner_packages_JavaFileName_MethodName)

I think this solves the problem. Thanks and greetings, Harshal

+4
Mar 14 '10 at 10:02
source share

From some where I found below: -

Actually you can.

Using the reflection API, you can access any class. At least I could :)

 Class fooClass = Class.forName("FooBar"); Method fooMethod = fooClass.getMethod("fooMethod", new Class[] { String.class }); String fooReturned = (String) fooMethod.invoke(fooClass.newInstance(), "I did it"); 
+4
Apr 09 '14 at 12:01
source share

Unfortunately, you cannot import a class without entering the package. This is one of the reasons why he is very discouraged. What I would like to try is a kind of proxy server - put your code in a package that anyone can use, but if you really need something in the default package, make it a very simple class that redirects calls to the class using real code. Or, even easier, just expand it.

To give an example:

 import my.packaged.DefaultClass; public class MyDefaultClass extends DefaultClass {} 
 package my.packaged.DefaultClass; public class DefaultClass { // Code here } 
+1
03 Feb 2018-10-03T00
source share

Create a new package. Then move the default package classes to the new package and use these classes.

-one
Mar 31 '18 at 12:04
source share
  • Create a new package.
  • Move files from the default package to a new one.
-2
May 09 '15 at 18:54
source share
  • Create, for example, a β€œroot” package (folder) in your project.

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

  • Move YourClass.class to the source folder. (... / path_to_project / source / YourClass.class)

  • Import like this

    import source.YourClass;

-3
May 12 '14 at 10:58
source share



All Articles