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 default package, its giving me compile error. Its not able to recognise the class in default package. Where is a 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 put my class in any other package. This class has some native method which is implemented in Delphi . If I put that class in any of the folder,I will have to make the change that DLL which I want to avoid(really - I can not). Thats why I put my class in the default package.
Source: Tips4all
From the Java language spec:
ReplyDeleteIt 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.
Classes in the default package cannot be imported by classes in packages. This is why you should not use the default package.
ReplyDeleteI can give you this suggestion,
ReplyDeleteAs 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
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.
ReplyDeleteTo give an example:
--
import my.packaged.DefaultClass;
public class MyDefaultClass extends DefaultClass {}
--
package my.packaged.DefaultClass;
public class DefaultClass {
// Code here
}