How to load dynamically a jar at classpath with java 11 - jar

To load dynamically a jar to classpath I used to use the following code with Java 8.
File file = new File(jarPath);
URL u = f.toURI().toURL();
URLClassLoader urlClassLoader = (URLClassLoader) ClassLoader.getSystemClassLoader();
Class urlClass = URLClassLoader.class;
Method method = urlClass.getDeclaredMethod("addURL", new Class[]{URL.class});
method.setAccessible(true);
method.invoke(urlClassLoader, new Object[]{u});
However using Java 11 does not work anymore. I have tried some solution provided in the internet but none of them works for me.
Please could you help me to resolve this problem.
Thanks

Related

Why is runGitHub throwing an error on my public github repo containing a working shinyapp?

I've got a working shiny-app published (today) on a public Github repo:
I tried the line below to run the app from a script but get the following error.
shiny::runGitHub(repo = "<repo>",
username = "<username>")
# I have also tried:
# shiny::runGitHub(repo = "<username>/<repo>")
Downloading https://github.com/<username>/<repo>/archive/master.tar.gz
Error in utils::download.file(url, method = method, ...) :
cannot open URL 'https://github.com/<username>/<repo>/archive/master.tar.gz'
Any ideas why this could be happening?
The solution that worked came from here:
https://community.rstudio.com/t/run-shiny-app-from-github-repository/89839
The key is the argument ref="main"
shiny::runGitHub(repo = "<repo>",username = "<username >",ref="main")

How to convert Tensorflow Object Detection API model to TFLite?

I am trying to convert a Tensorflow Object Detection model(ssd-mobilenet-v2-fpnlite, from TensorFlow 2 Detection Model Zoo) to TFLite. First of all, I train the model using the model_main_tf2.py and then I use the export_tflite_graph_tf2.py to export a saved model(.pb). However, when it comes to convert the .pb file to .tflite it throws this error:
OSError: SavedModel file does not exist at: /content/gdrive/My Drive/models/research/object_detection/fine_tuned_model/saved_model/saved_model.pb/{saved_model.pbtxt|saved_model.pb}
To convert the .pb file I used:
import tensorflow as tf
SAVED_MODEL_PATH = os.path.join(os.getcwd(),'object_detection', 'fine_tuned_model', 'saved_model', 'saved_model.pb')
# SAVED_MODEL_PATH: '/content/gdrive/My Drive/models/research/object_detection/exported_model/saved_model/saved_model.pb'
converter = tf.lite.TFLiteConverter.from_saved_model(SAVED_MODEL_PATH)
converter.optimizations = [tf.lite.Optimize.DEFAULT]
converter.experimental_new_converter = True
converter.target_spec.supported_ops = [tf.lite.OpsSet.TFLITE_BUILTINS, tf.lite.OpsSet.SELECT_TF_OPS]
tflite_model = converter.convert()
open("detect.tflite", "wb").write(tflite_model)
or "tflite_convert" from command line, but with the same error. I also tried to run it with the latest tf-nightly version as it suggests here, but the outcome is the same. I tried to pass the path with various ways, it seems like the .pd is not well written (not the right file). Is there a way to manage to convert the model to tflite so as to implement it to android? Thank you!
Your saved_model path should be "/content/gdrive/My Drive/models/research/object_detection/fine_tuned_model/saved_model/". It is the folder instead of files in that folder
For quick test, try to type in terminal
tflite_convert \
--saved_model_dir="path to saved_folder" \
--output_file="path to tflite file u want to save"
I don't have enough reputation to just comment but the problem here seems to be your SAVED_MODEL_PATH.
You could try to hardcode the path and remove the .pb file. I don't remember exactly what's the trick here but it's definitively due to the path

Runtime error on loading intermediate representation open vino

This question follows from my earlier question . I had converted this keras model to .pb. The I also successfully converted this .pb to intermediate representation of Open VINO. The model is available in the attachment of last answer of this post at intel developer forum.
If you got this model. Now you can load the model like this .
model_path = 'jester_freezed/IR/model/tf_model.xml'
from openvino.inference_engine import IENetwork, IECore
import os
def load_to_IE(model_xml, cpu_extension=None):
#Load the Inference Engine API
plugin = IECore()
# Load IR files into their related class
model_bin = os.path.splitext(model_xml)[0] + ".bin"
net = IENetwork(model=model_xml, weights=model_bin)
return net
net = load_to_IE(model_path)
Here is the full error.
RuntimeError: Check 'PartialShape::broadcast_merge_into(pshape, get_input_partial_shape(i), autob)' failed at /teamcity/work/scoring_engine_build/releases_2020_1/ngraph/src/ngraph/node.cpp:890:
While validating node 'Add[Add_43](Convolution_41: float{1,40,12,12}, Constant_42: float{40}) -> (??)':
Argument shapes are inconsistent.
My open vino version is 2020.1.
There are no resources available to debug this error. Please help. Thanks.

Reference GPSBabel in PATH variable to use in R

I have read this question and answer here Read multiple .gpx files on reading GPX files in R. At the end of the question, the question setter notes "To run readGPS you will need the open source GPSBabel program installed and referenced in your PATH variable."
I have installed GPSBabel, but am unsure how to link it to R so that the supplied code works.
library(maptools)
gpx.raw <- readGPS(i = "gpx", f = "Data/Waypoints_11-MAY-18.gpx", type="w")
Running the code at the head of the question (above) with my own filename returns the error
Error in readGPS(i = "gpx", f = "Data/Waypoints_11-MAY-18.gpx", type = "w") :
gpsbabel not found
I had this problem, and I downloaded and installed GPSBabel, copied of the path for where it installed, and then:
setwd("C:/Program Files (x86)/GPSBabel")
That took care of it for me, not sure if there is a simpler way to do this.

How to work with an expected return type of "java.lang.Class"?

I thought I'd try to write an R interface to Scribe (mature OAuth library for Java by Pablo Fernandez) as a way of refreshing myself on Java (not used it in 8 years), learning rJava and to make better use of the Twitter API. But mostly because it's Friday afternoon and I thought it'd be fun. :)
Unfortunately I haven't got very far...
I downloaded the .jar file for scribe and also commons-condec (its only dependency, which I subsequently unzipped). I've ran the code in Java using netbeans and it works fine using his twitter example.
I was OK for the first few lines of code by just following the rJava documenation:
# load R packages
library(rJava)
# Initialise
.jinit()
# Add class paths
d1 <- "C:/Users/Tony/Documents/R/java/scribe-1.1.0.jar"
d2 <- "C:/Users/Tony/Documents/R/java/commons-codec-1.4/"
.jaddClassPath(path=c(d1, d2))
But then scribe quick start guide says the following is needed:
// Java Code
OAuthService service = new ServiceBuilder()
.provider(TwitterApi.class)
.apiKey("6icbcAXyZx67r8uTAUM5Qw")
.apiSecret("SCCAdUUc6LXxiazxH3N0QfpNUvlUy84mZ2XZKiv39s")
.build();
I can't figure out how to rewrite that into rJava parlance. A little web searching suggests I should do it in parts, so first I did:
# Create object (back to R code again)
( service <- .jnew("org.scribe.builder.ServiceBuilder") )
[1] "Java-Object{org.scribe.builder.ServiceBuilder#58fe64b9}"
# Set up apiKey and apiSecret using "$" shortcut
service$apiKey("6icbcAXyZx67r8uTAUM5Qw")
service$apiSecret("SCCAdUUc6LXxiazxH3N0QfpNUvlUy84mZ2XZKiv39s")
Good so far. Then I need to figure out what return type is expected from the provider function:
# Inspect return type
.jmethods(service, "provider")
[1] "public org.scribe.builder.ServiceBuilder org.scribe.builder.ServiceBuilder.provider(java.lang.Class)"
It needs "java.lang.Class". This is where I get confused. What does that mean? I guess, looking at the source, it needs a return type of type "ServiceBuilder", but how to do that? This was my best guess after looking at ?.jcall (note: 'use.true.class = TRUE' didn't do anything):
> .jcall(obj = service, returnSig = "Lorg.scribe.builder.ServiceBuilder;", method = "org.scribe.builder.ServiceBuilder.provider", "org.scribe.builder.api.TwitterApi")
Error in .jcall(obj = service, returnSig = "Lorg.scribe.builder.ServiceBuilder;", :
method org.scribe.builder.ServiceBuilder.provider with signature (Ljava/lang/String;)Lorg.scribe.builder.ServiceBuilder; not found
Any ideas?
It looks to me like the provider method returns ServiceBuilder and takes a Class as a parameter.
In Java if you put the classname followed by .class that makes a class literal object in the code. If you instead load the class using reflection you can refer to a class by its string name. I'm not sure how this works in R but in Java the syntax is:
Class c = Class.forName("org.scribe.builder.api.TwitterApi");
This puts the class instance into the variable c. Then you could call the provider method:
service$provider(c);

Resources