Including external jar files to resource path in Rhino shell - jar

I am using javascript on a Rhino Shell. I need to add the com.sun.jna.jar to the classpath. Now when I type - js:> Packages.com.sun.jna.NativeLibrary
[JavaClass com.sun.jna.NativeLibrary]
This shows that the jna library is accessible by the Rhino Shell? But when I try to use the library: js:> var c=Packages.com.sun.jna.NativeLibrary.getInstance("c");
java.lang.UnsatisfiedLinkError: Unable to load library '2': JNA native support (win32-x86/2.dll) not found in resource path (C:\ti\ccsv5\eclipse\plugins/org.eclipse.equinox.launcher_1.3.0.v20120522-1813.jar)
The resource path(C:\ti\ccsv5......launcher_1.3.0.v20120522-1813.jar) is the path it shows when I type - js:> print(System.getProperty("java.class.path"));
I donot understand how to add the jar files to the resource path.I tried to do it by the Command prompt: java -cp "path of jar" .name of class containing main ---> How do I understand what is the name of the class containing main since Rhino does the conversion from javascripts to classes?
Thanks in Advance.
Sohini.

I don't believe that the issue is specifically related to Rhino. Rather, it is a difference between the Linux and Windows system libraries.
The getting started document for JNA, at
https://github.com/twall/jna/blob/master/www/GettingStarted.md provides the following example:
package com.sun.jna.examples;
import com.sun.jna.Library;
import com.sun.jna.Native;
import com.sun.jna.Platform;
/** Simple example of JNA interface mapping and usage. */
public class HelloWorld {
// This is the standard, stable way of mapping, which supports extensive
// customization and mapping of Java to native types.
public interface CLibrary extends Library {
CLibrary INSTANCE = (CLibrary)
Native.loadLibrary((Platform.isWindows() ? "msvcrt" : "c"),
CLibrary.class);
void printf(String format, Object... args);
}
public static void main(String[] args) {
CLibrary.INSTANCE.printf("Hello, World\n");
for (int i=0;i < args.length;i++) {
CLibrary.INSTANCE.printf("Argument %d: %s\n", i, args[i]);
}
}
}
The native library loaded depends on the platform you are running the program on: For Windows you will need to use the 'msvcrt' (Microsoft Visual C Run Time) library; for others, use the 'c' library.
I ran a test case to confirm:
Running on Windows fails if I try to load the "c" library.
PS C:\Users\jharig\workspace\JNATest> java -cp ".\jna-3.5.2.jar;.\platform-3.5.2.jar;.\js.jar;.\js-14.jar" org.mozilla.javascript.tools.shell.Main
Rhino 1.7 release 4 2012 06 18
js> var c = Packages.com.sun.jna.NativeLibrary.getInstance("c");
Exception in thread "main" java.lang.UnsatisfiedLinkError: Unable to load library 'c': JNA native support (win32-amd64/c
.dll) not found in resource path (.\jna-3.5.2.jar;.\platform-3.5.2.jar;.\js.jar;.\js-14.jar)
at com.sun.jna.NativeLibrary.loadLibrary(NativeLibrary.java:220)
at com.sun.jna.NativeLibrary.getInstance(NativeLibrary.java:322)
Running on Windows succeeds if I use the 'msvcrt' library.
PS C:\Users\jharig\workspace\JNATest> java -cp ".\jna-3.5.2.jar;.\platform-3.5.2.jar;.\js.jar;.\js-14.jar" org.mozilla.javascript.tools.shell.Main
Rhino 1.7 release 4 2012 06 18
js> var c = Packages.com.sun.jna.NativeLibrary.getInstance("msvcrt");
js> var f = c.getFunction("printf");
js> f.invoke(["Hello World\n"]);
Hello World
js>

Related

Unable to load assemby built with F# compiler services via reflection

I keep getting the following error message when I tried to load (via reflection) a .dll built using F# compiler services (even though the Equals method being complained about does exist in the build):
Unhandled Exception: System.Reflection.ReflectionTypeLoadException: Unable to load one or more of the requested types.
Method 'Equals' in type 'XXX' from assembly 'YYY', Version=0.0.0.0, Culture=neutral, PublicKeyToken=null' does not have an implementation.
at System.Reflection.RuntimeModule.GetTypes(RuntimeModule module)
at System.Reflection.RuntimeModule.GetTypes()
at System.Reflection.Assembly.GetTypes()
I'm using .NET Core on Ubuntu.
However, if I build using Visual Studio Code, I'm able to load the assembly. I opened both builds and I noticed the version that doesn't work has an override for one of the Equals method while the version that does, doesn't. I'm not sure if this is of consequence:
[CompilerGenerated]
public sealed bool Equals(object obj, IEqualityComparer comp)
vs
[CompilerGenerated]
public sealed override bool Equals(object obj, IEqualityComparer comp)
Additionally, when I check the references using dnSpy, the version that works has a reference to .netstandard while the version that does not does not have this reference. I've tried adding a reference to the .netstandard .dll as part of the compilation but this doesn't seem to have any effect.
The code I'm trying to build is a simple record, roughly equivalent to the following:
namespace Xxx
open System
type Yyy =
{
ServiceCategory : string
DateRange : DateTime
}
Here's the code I used to build using F# Compiler Services:
// Detect the file location for the library that defines the object type
let corelibRefLocation = typeof<Object>.GetTypeInfo().Assembly.Location
let corelibRefDirectory = Path.GetDirectoryName(corelibRefLocation)
let mscorlibRefLocation = sprintf #"%s/mscorlib.dll" corelibRefDirectory
let systemRuntimeRefLocation = sprintf #"%s/System.Runtime.dll" corelibRefDirectory
let netStandardRefLocation = sprintf #"%s/netstandard.dll" corelibRefDirectory
let staticArgs =
[|
"fsc.exe"
"--noframework"
"-o"; outputPath;
"-a"; sourcePath
|]
let references =
[|
"-r"; corelibRefLocation
"-r"; systemRuntimeRefLocation
"-r"; mscorlibRefLocation
"-r"; netStandardRefLocation
|]
let args = Array.concat([|staticArgs; references|])
let errors, exitCode =
checker.Compile(args)
|> Async.RunSynchronously
match (errors, exitCode) with
| [||], 0 -> Console.WriteLine("OK!")
| _ -> Console.WriteLine("Not OK!")
I'm not sure why the .dll isn't loading and what I need to do differently.
UPDATE:
I've upgraded from .net Core 2.1. to .NET Core 3.0 and even though the generated code now no longer includes the override, the issue persists. The only significant difference between the version that works (compiled with VSCode) and the version that doesn't (compiled with FCS) that I can observe now is that the FCS version has an explicit reference to mscorlib and System.Private.CoreLib. However, I'm unable to get the code to compile without these explicit references.

WebConsoleListener IllegalAccessError in JavaFX 12

I recently downloaded the latest JavaFX SDK 12 and I wish to intercept Console Messages in my JavaFX WebView.
So, I have this
WebConsoleListener.setDefaultListener((webView, message, lineNumber, sourceId) -> {
//////// I am listening for a specific console message here in my
///webview
});
but I keep getting
Caused by: java.lang.IllegalAccessError: class rumbler.launcher.ApplicationLoader (in unnamed module #0x5c4c6905) cannot access class com.sun.javafx.webkit.WebConsoleListener (in module javafx.web) because module javafx.web does not export com.sun.javafx.webkit to unnamed module #0x5c4c6905
Here is my build.gradle file
javafx {
version = "12.0.1"
modules = ['javafx.base', 'javafx.controls', 'javafx.web']
}
Here are my VM OPTIONS
--module-path "path_to_\javafx-sdk-11.0.2\lib" --add-modules javafx.controls,javafx.fxml,javafx.web,javafx.base
.Am I missing something?
You are using private API, which is not advised.
Anyway, the error message is quite clear:
module javafx.web does not export com.sun.javafx.webkit to unnamed module #0x5c4c6905
Whenever you want to access some non-exposed package from your project (either modular on non-modular), you need to use --add-exports:
The command line option --add-exports $module/$package=$readingmodule exports $package of $module to $readingmodule. Code in $readingmodule can hence access all public types in $package but other modules can not. [source].
So in this case, the solution is straight forward:
--add-exports javafx.web/com.sun.javafx.webkit=ALL-UNNAMED \
--module-path "path_to_\javafx-sdk-11.0.2\lib" \
--add-modules javafx.web,javafx.fxml

how to enable Visor Command Line In Apache Ignite?

I have started Apache Ignite server via Maven Dependency trough eclipse,can anyone tell me how to monitor cache through visor command? How to enable it when setup Apache Ignite via Maven?
I think the most easy way is to download binary distributive and lunch Visor command line from "\bin" folder. Note, you need to download release that match to that you are using in your Maven based application.
The second way is to use ignite-visor-console module from Maven
And start Visor command line via: org.apache.ignite.visor.commands.VisorConsole object (it extends App). Note, Visor command line is written on Scala.
Sample code:
import org.apache.ignite.visor.commands.VisorConsole;
public class Test {
static public void main(String args[]) {
VisorConsole.main(args);
}
}
Also see Visor command line documentation.
And also give a try for Web Console, as Dmitriy suggested.

Jasper Reports "package net.sf.jasperreports.engine does not exist" exception in JDeveloper 11.1 using WebLogic only in EAR Web Page

I'm using JDeveloper 11.1, Oracle 11 and TIBCO JasperReports 6.0.1.
I'm having problems trying to generate Jasper Reports from my web page (ViewController) while using an ApplicationModule (Model - EJB) for doing that. At the end the PDF file has to be sent via email, that's why I let it into the Model project.
If I execute the ApplicationModule, it works fine, no exceptions, the PDF is very well generated and sent.
However, if I execute the client method since a web page I got this exception :
net.sf.jasperreports.engine.JRException: Errors were encountered when compiling report expressions class file:
C:\Users\rodmar\AppData\Roaming\JDeveloper\system11.1.1.7.40.64.93\DefaultDomain\Simple_Blue_1429546047623_56582.java:4: package net.sf.jasperreports.engine does not exist
import net.sf.jasperreports.engine.*;
^
C:\Users\rodmar\AppData\Roaming\JDeveloper\system11.1.1.7.40.64.93\DefaultDomain\Simple_Blue_1429546047623_56582.java:5: package net.sf.jasperreports.engine.fill does not exist
import net.sf.jasperreports.engine.fill.*;
^
C:\Users\rodmar\AppData\Roaming\JDeveloper\system11.1.1.7.40.64.93\DefaultDomain\Simple_Blue_1429546047623_56582.java:18: cannot find symbol
symbol: class JREvaluator
public class Simple_Blue_1429546047623_56582 extends JREvaluator
^
C:\Users\rodmar\AppData\Roaming\JDeveloper\system11.1.1.7.40.64.93\DefaultDomain\Simple_Blue_1429546047623_56582.java:25: cannot find symbol
symbol : class JRFillParameter
location: class Simple_Blue_1429546047623_56582
private JRFillParameter parameter_REPORT_LOCALE = null;
^
C:\Users\rodmar\AppData\Roaming\JDeveloper\system11.1.1.7.40.64.93\DefaultDomain\Simple_Blue_1429546047623_56582.java:26: cannot find symbol
symbol : class JRFillParameter
location: class Simple_Blue_1429546047623_56582
private JRFillParameter parameter_Description = null;
^
C:\Users\rodmar\AppData\Roaming\JDeveloper\system11.1.1.7.40.64.93\DefaultDomain\Simple_Blue_1429546047623_56582.java:27: cannot find symbol
symbol : class JRFillParameter
location: class Simple_Blue_1429546047623_56582
private JRFillParameter parameter_JASPER_REPORT = null;
^
C:\Users\rodmar\AppData\Roaming\JDeveloper\system11.1.1.7.40.64.93\DefaultDomain\Simple_Blue_1429546047623_56582.java:28: cannot find symbol
symbol : class JRFillParameter
location: class Simple_Blue_1429546047623_56582
private JRFillParameter parameter_REPORT_VIRTUALIZER = null;
^
I'm just pasting a fragment.
I'm using other .jars as POI, for reading .xlsx files and inserting then into database so I don't know why I can access POI without any problem but at the same time I don't have Jasper Reports available.
I have already searched some solutions in the web but nothing solves my problems. I get some information about jdt-compiler but I don't find it into JasperReports suite. My project is really a mess with all these libraries, maybe I'm missing or adding too many ?
My EAR project at the moment is like this :
The EAR\lib:
This is a kind of a problem for jars settings, or something like that. I had already found this page but it is really strange, I don't think that my issue is something so complicated.
http://docs.oracle.com/cd/E25178_01/fusionapps.1111/e15524/adv_wls_e.htm
EDIT 1 :
I tried to look to my classpath using this code :
ClassLoader cl = ClassLoader.getSystemClassLoader();
URL[] urls = ((URLClassLoader)cl).getURLs();
for (URL url: urls) {
logger.info(String.format("Classpath: %s.", url.toString()));
}
And I get this output. POI is not here but why, I'm using it without any problems while I'm not been capable of using JasperReports ?
1 ) Is POI.jar by default used in WebLogic 10.3 basic installation ? NO
EDIT 2 :
I've discovered that in WebLogic is necessary to access weblogic.utils.classloaders.GenericClassLoader which sends me all the libraries that belong to this EAR application. Using it, Jasper Reports are available however I don't know what to do to access them successfully...
GenericClassLoader jre = (GenericClassLoader) JREvaluator.class.getClassLoader();
logger.info(String.format("jre: %s, s.", jre, jre.getClassPath()));
Thanks you very much,
Well, as somebody says it was necessary to use jdt-compiler-3.1.1.jar. I added it to /lib folder and now it is working.
It's really strange that this .jar is not in the installation product for TIBCO Jasper Reports 6.0.0 actually I descend until JR 3.7.6 which is the lowest version. The file is from 28/12/2008...
Nice...
I used eclipse ecj-4.3.1.jar and it works.
First checks the dependencies of jasperReports - for example version 6.1.0 - https://mvnrepository.com/artifact/net.sf.jasperreports/jasperreports/6.1.0
With this you can see if there is any dependency that is missing in your lib directory (webApp) or inside your .jar (if it is a main application of java).
That mistake also happened to me, and I solved it by adding dependence
<groupId>org.eclipse.jdt.core.compiler</groupId>
<artifactId>ecj</artifactId>
<version>4.3.1</version>
I hope it supports you

How to run standalone TestNG project from jar/bat/

I have a TestNG project. Don't have any main class, currently it is running like "Run As TestNG".
I want to export it as runnable jar or jar so that any one can just hit a command from command line and test cases start running.
Could any one help me out in this? or suggest any other way to deliver the code in runnable form...
I am not using ant or maven.
Thanks
I seem to have found the solution after a bit of googling. This works fine in Eclipse (Juno).
Say, you have a TestNG file named 'Tests.java'. As you rightly pointed out, there won't be a class with main method.
So, we have to create a new Java class file under the same package. Let us name it 'MainOne.java'. This will have a class with main method.
Here is the code you need:
import com.beust.testng.TestNG;
public class MainOne {
public static void main(String[] args) {
TestNG testng = new TestNG();
Class[] classes = new Class[]{Tests.class};
testng.setTestClasses(classes);
testng.run();
}
Run the 'MainOne.java' as a Java application. Then right click on the package -> Export -> Runnable Jar [Choose 'MainOne' as Launch Configuration] -> Finish.
My current understanding is that, in order to benefit from the parallel niftiness of TestNG, one should use the static main method in org.testng's jar file when running the Java class from the command line rather than from inside Eclipse IDE.
The issue then becomes classpath, which defines how java finds all the JAR files. I found http://javarevisited.blogspot.com/2012/10/5-ways-to-add-multiple-jar-to-classpath-java.html to be most useful because it has the * wildcard mentioned --- VERY helpful when you need to reference all the jar files required for Selenum + TestNG + custom test suites.
This is my current Windows BAT file, and it works. ADV.jar contains my custom class but no main method.
setlocal
set r=d:\Apps\Selenium\
cd /d %~dp0
java -classpath %r%Downloaded\*;%r%MyCompany\ADV.jar; org.testng.TestNG .\testng-customsuite-adv.xml
pause
All the JAR files that I downloaded from public places went into my d:\Apps\Selenium\Downloaded folder. I put my custom ADV.jar file in d:\Apps\Selenium\MyCompany to keep it separate.
I created my ADV.jar file from Eclipse using Export Jar file and ignored warnings about a missing main method.
Aside: while this https://stackoverflow.com/a/16879386/424855 was very intriguing, I could not figure out how to make that work.
Here is the better way to do it.
You can just create a main method which will have list of all test classes to be executed as follows:
public static void main(String[] args) {
TestListenerAdapter tla = new TestListenerAdapter();
TestNG testng = new TestNG();
testng.setTestClasses(new Class[] { test_start.class });
testng.addListener(tla);
testng.run();
}
Here is the reference URL from the official testng website.
Run the MainOne.java as a Java application. Then right click on the package -> Export -> Runnable Jar [Choose MainOne as Launch Configuration] -> Finish.

Resources