How to override a java library in plugin development environment - java

I develop an application in a "special" plugin-development environment.
This environment needs to include some java libraries by default so that the created plugin-application can be exported and used successfully.
The problem is now that the plugin-environment comes with an old "javax.mail" library. It is not supported to override this library in the environment.
Of course it is possible to include the newer javax.mail library into my plugin-application but the library is not recognized and the old library is used.
Question:
Is it possible to force a Java Application to use a special included library which is using the same package and class names like a "native" library from a "plugin-environment"?
What I have tried:
I tried to rename the package files within the custom library "javax.mail" to "javax_external.mail" and to use something like this in my application:
javax_external.mail.Session session = javax_external.mail.Session.getDefaultInstance(props);
But I get the error: Type mismatch: cannot convert from javax.mail.Session to javax.mail.Session

Related

JavaNativeFoundation.framework comes without major version in Azul and Liberica JDK – how can I tell Java to use it anyway?

I am trying to use a native library on an ARM based Mac, i.e. an M1 processor. There are several JVMs available for that architecture, for example Azul or Liberica. Both come with a JavaNativeFoundation.framework dynamic library, which is necessary to use any JNI code. However, the framework does not have a Versions folder that contains the major version of the framework (like A). Instead, the library is just contained in the top level folder:
/Library/Java/JavaVirtualMachines/zulu-16.jdk/Contents/Home/lib/JavaNativeFoundation.framework
/Library/Java/JavaVirtualMachines/zulu-16.jdk/Contents/Home/lib/JavaNativeFoundation.framework/_CodeSignature
/Library/Java/JavaVirtualMachines/zulu-16.jdk/Contents/Home/lib/JavaNativeFoundation.framework/_CodeSignature/CodeResources
/Library/Java/JavaVirtualMachines/zulu-16.jdk/Contents/Home/lib/JavaNativeFoundation.framework/JavaNativeFoundation
/Library/Java/JavaVirtualMachines/zulu-16.jdk/Contents/Home/lib/JavaNativeFoundation.framework/Resources
/Library/Java/JavaVirtualMachines/zulu-16.jdk/Contents/Home/lib/JavaNativeFoundation.framework/Resources/Info.plist
Trying to run my program I get
[java] AquaNativeSupport: Unable to load library libvaqua.dylib: /private/var/folders/tg/06858t0j3w10js_5hb3k5frw0000gn/T/libvaqua4883847212092667097.dylib: dlopen(/private/var/folders/tg/06858t0j3w10js_5hb3k5frw0000gn/T/libvaqua4883847212092667097.dylib, 0x0001): Library not loaded: #rpath/JavaNativeFoundation.framework/Versions/A/JavaNativeFoundation
[java] Referenced from: /private/var/folders/tg/06858t0j3w10js_5hb3k5frw0000gn/T/libvaqua4883847212092667097.dylib
[java] Reason: tried: '/Library/Java/JavaVirtualMachines/zulu-16.jdk/Contents/Home/lib/server/./JavaNativeFoundation.framework/Versions/A/JavaNativeFoundation' (no such file),
'/Library/Java/JavaVirtualMachines/zulu-16.jdk/Contents/Home/lib/server/../JavaNativeFoundation.framework/Versions/A/JavaNativeFoundation' (no such file),
'/Library/Java/JavaVirtualMachines/zulu-16.jdk/Contents/Home/bin/./JavaNativeFoundation.framework/Versions/A/JavaNativeFoundation' (no such file),
'/Library/Java/JavaVirtualMachines/zulu-16.jdk/Contents/Home/bin/../lib/JavaNativeFoundation.framework/Versions/A/Java
It looks like the output is incomplete, but anyway: You can see that java is trying to load the framework from a Versions/A/ subfolder of the framework, which does not exist anywhere.
If I create that subfolder and create a softlink to the existing binary JavaNativeFoundation in it, then the application loads the JNI code just fine.
Given that situation I have two questions:
Why are the Frameworks in both Azul/zulu and Liberica missing the Versions structure?
Is there a way to work around that problem by loading the Framework in a different way? Some "dontusemajorversion" flag?
Thank you for your help.

PsiClass to java.lang.Class

I'm developing plugin for IntelliJ IDEA. How can plugin get the name and version of libraries that are imported to the project that is being checked by plugin? I have PsiClass of the project, but cannot convert it to java.lang.Class. Maybe there's the way to get ClassLoader from PsiElement?
super.visitImportStatement(psiImport);
Class importedClass = Class.forName(psiImport.getQualifiedName(), true, psiImport.getClass().getClassLoader());
PsiImport.getClass().GetClassLoader() - returns ClassLoader of class PsiImportStatementImpl instead of ClassLoader of class that I've imported.
IntelliJ does mostly static analysis on your code. In fact, the IDE and the projects you run/debug have completely different classpaths. When you open a project, your dependencies are not added to the IDE classpath. Instead, the IDE will index the JARs, meaning it will automatically discover all the declarations (classes, methods, interfaces etc) and save them for later in a cache.
When you write code in your editor, the static analysis tool will leverage the contents of this index to validate your code and show errors when you're trying to use unknown definitions for example.
On the other hand, when you run a Main class from your project, it will spawn a new java process that has its own classpath. This classpath will likely contain every dependency declared in your module.
Knowing this, you should now understand why you can't "transform" a PsiClass to a corresponding Class.
Back to your original question:
How can plugin get the name and version of libraries that are imported to the project that is being checked by plugin?
You don't need to access Class objects for this. Instead, you can use IntelliJ SDK libraries. Here's an example:
Module mod = ModuleUtil.findModuleForFile(virtualFile,myProject);
ModuleRootManager.getInstance(mod).orderEntries().forEachLibrary(library -> {
// do your thing here with `library`
return true;
});

Native Java to Android Java (JsonPath)

I'm converting native Java code to android one.
There is a Android Java code from a native Java as following:
import com.jayway.jsonpath.JsonPath;
...
body = new String(bodyPart);
JsonPath.with(body).getString("psIp");
...
However, there is no with method in Android. How can I convert it?
JsonPath class comes from some library (most likely 'com.jayway.jsonpath:json-path:0.8.1'), which has com.jayway.jsonpathpackage. This class is not present neither in native Java, nor in native Android.
You have a dependency to that library, that has JsonPath class, in your Java project (anyway you added it - either through gradle, maven, or simply by adding a reference to jar file through IDE). If you would add that dependency to your Android project (gradle is preferred for adding dependencies in Android, though you can add it any other way), you will be able to use JsonPath class in your Android code also.

How to implement a java library in Robot Framework

How can I create a library in Eclipse and then import it in Robot FrameWork?
I am searching a lot now and none of the guides out to help me out.
You need to do the following:
Create your java library
Add it to the classpath when running robot framework jython edition
Creating your java library:
Define a new java class. At this point try not to use a constructor yet (although it is possible to support constructors with fields)
Define the ROBOT_LIBRARY_SCOPE static String variable in the class.
public static final String ROBOT_LIBRARY_SCOPE = "GLOBAL";
Define public methods (not static) that will be used as the keywords
Adding your library to the class path
Compile your classes - ideally to a jar
Add the jar to the class path when running jython. The easiest way to do this is with the MVN Robot Framework plugin. Another option is to wrap the jybot run in a batch file and add CLASSPATH definition to it. There are other options as well (gradle or ant for example).
Using your library in your code
You need to import your library using the full package path
import library org.robot.sample.keywords.MyLibrary
https://blog.codecentric.de/en/2012/06/robot-framework-tutorial-writing-keyword-libraries-in-java/
You can see the full example of how to add a jar when using ride in this article
https://blog.codecentric.de/en/2012/04/robot-framework-tutorial-a-complete-example/

No class found exception while using json-lib in android

I am taking the input from the web, which is an Xml file and converting into a Json data using the library json-lib . I have created a user library and added the following jars into it:-
json-lib-2.3-jdk15.jar
commons-collections.jar
commons-lang.jar
commons-logging.jar
commons-beanutils.jar
ezmorph-1.0.6.jar
xom-1.1.jar
But still gives the following error:-
08-04 13:58:31.642: ERROR/dalvikvm(484): Could not find class 'net.sf.json.xml.XMLSerializer$CustomElement', referenced from method net.sf.json.xml.XMLSerializer.addNameSpaceToElement
Can anyone help me out in resolving this issue.
Either you have a sdk level / jdk level conflict. I mean dalvik can't get the byte code of the CustomElement class of your librairy as it is compiled with to recent features for your SDK like annotations for instance.
Or there is a conflicting librairy json-lib in some other of your jars or lib folders.
(the 3 first comments are not relevant, it's just the way inner classes are compiled, using a $)
Regards,
Stéphane
Since android already support json org.json a different json library may conflict. (You can download the jar here)
Try to use this library instead of an external library on android.
BTW: You can also use this library if you need on any java code (not only android)

Categories

Resources