Build a Model From Stream (Exception: Reader not found on classpath) - java

I’m just trying to add my ontology to the DB repository ……
My code is:
oConnection = H2Db.getM_oConnection();
m_oSDBConnection = new SDBConnection(oConnection);
StoreDesc oStoreDesc = new StoreDesc(LayoutType.LayoutTripleNodesHash, DatabaseType.H2);
m_oStore = SDBFactory.connectStore(m_oSDBConnection, oStoreDesc);
m_oModel = SDBFactory.connectDefaultModel(m_oStore);
InputStream oInputStream = this.getClass().getResourceAsStream("/META-INF/betaas_context.owl");
m_oModel.read(oInputStream, null);
At the beginning with the following dependencies:
ID State Blueprint Level Name
[ 994] [Active ] [ ] [ 80] H2 Database Engine (1.3.170)
[1114] [Active ] [ ] [ 80] wrap_mvn_org.apache.jena_jena-iri_1.0.0 (0)
[1223] [Active ] [ ] [ 80] wrap_mvn_org.apache.jena_jena-arq_2.11.0 (0) -> contains package org.apache.jena.riot.adapters
[1279] [Active ] [ ] [ 80] wrap_mvn_org.apache.jena_jena-core_2.11.0 (0)
[1311] [Active ] [ ] [ 80] wrap_mvn_xerces_xercesImpl_2.7.1 (0)
[1314] [Active ] [ ] [ 80] wrap_mvn_com.ibm.icu_icu4j_3.4.4 (0)
And I get this exception:
java.lang.Exception: com.hp.hpl.jena.shared.ConfigException: Reader not found on classpath
Caused by: com.hp.hpl.jena.shared.ConfigException: Reader not found on classpath
Caused by: java.lang.ClassNotFoundException:
org.apache.jena.riot.adapters.JenaReadersWriters$RDFReaderRIOT_RDFXML
And the problem was with the following line:oModel.read(oInputStream, null);
EDIT
Then, following the suggestion mentioned by AndyS(see bellow), I updated my Jena libraries and used the 2.11.1-SNAPSHOT:
[1511] [Active ] [ ] [ 80] wrap_mvn_org.apache.jena_jena-core_2.11.1-SNAPSHOT (0)
[1512] [Active ] [ ] [ 80] wrap_mvn_org.apache.jena_jena-arq_2.11.1-SNAPSHOT (0)
[1515] [Active ] [ ] [ 80] wrap_mvn_org.apache.jena_jena-sdb_1.4.1-SNAPSHOT (0)
[1516] [Resolved ] [ ] [ 80] wrap_mvn_xerces_xercesImpl_2.11.0 (0)
[1521] [Active ] [ ] [ 80] wrap_mvn_org.apache.jena_jena-iri_1.0.1-SNAPSHOT (0)
But the exception is the same:
ClassNotFoundException: org.apache.jena.riot.adapters.JenaReadersWriters$RDFReaderRIOT_RDFXML

There is a fixed bug to do with handling .owl files. This maybe the issue for some of the exceptions you are seeing. The current development snapshots have this fixed.
Unrelated:
You have the wrong version of xerces for jena. You do not need icu4j anymore.

The root error is a ClassNotFoundException for the following class: org.apache.jena.riot.adapters.JenaReadersWriters$RDFReaderRIOT_RDFXML. It looks like this library is using dynamic reflection to load the class, e.g. using Class.forName(), which is a really bad thing to do.
Anyway you should be able to fix it by adding the package org.apache.jena.riot.adapters to your Import-Package list.

At last, it was certainly a problem of Jena libraries versions.
I solved it with the following ones:
xerces/xercesImpl/2.9.1
com.ibm.icu/icu4j/3.4.4
org.slf4j/slf4j-api/1.6.1
com.hp.hpl.jena/arq/2.8.7
com.hp.hpl.jena/jena/2.6.4
com.hp.hpl.jena/iri/0.8
com.hp.hpl.jena/sdb/1.3.4
As Andy S. suggested me: "the problem is in the bundling.jena-core makes a reflection call to set up RIOT, and RIOT installs JenaReadersWriters$RDFReaderRIOT_RDFXML. This is instantiated with call of Class.newInstance(). It looks like the latter is failing.
This may be because of classloaders as setup by the OSGi bundling. You probably want one bundle with all of the Jena jars in it. As I understand your reported setup, you have a separate bundle, hence a different classloader, hence not found."
I have not checked it but probably the solution is to make a unique bundle jar with at least 2.11.0 version:
org.apache.jena/jena-core/2.11.0
org.apache.jena/jena-arq/2.11.0
org.apache.jena/jena-sdb/1.4.0
org.apache.jena/jena-iri/1.0.0
xerces/xercesImpl/2.11.0`

Thanks for the post!!
I had the same issue and fixed it by just adding org.apache.jena.riot.adapters package at the imports of jena-core.jar.
E.g. BND configuration file of jena-core-2.11.1.jar:
version=2.11.1
Bundle-Version: ${version}
Bundle-Name: Jena CORE
Export-Package: !etc, !jena, !jena-log4j.properties, !jena.cmdline, !ont-policy.rdf, *; version=${version}
Import-Package: org.apache.jena.riot.adapters, *

Related

how to import java packages in a java core Jupyter Notebook

I'm trying to execute java code from a github repository in google colab,I know that google colab is for python by default so I installed the ijava library on to the Jupyter notebook using the commands:
!wget https://github.com/SpencerPark/IJava/releases/download/v1.3.0/ijava-1.3.0.zip
!unzip ijava-1.3.0.zip
!python install.py --sys-prefix
after I changed the Jupyter notebook configurations as follows:
{
"nbformat": 4,
"nbformat_minor": 0,
"metadata": {
"colab": {
"name": "example.ipynb",
"provenance": []
},
"kernelspec": {
"name": "python3"--->"java",
"display_name": "python3"--->"java"
},
"language_info": {
"name": "python"
}
},
"cells": [
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "NzKT3-VExE4i"
},
"outputs": [],
"source": [
""
]
}
]
}
After these changes I was able to run java in that Jupyter notebook, so I put this ipynb file inside my java netbeans project in the same package as my main file but when I try to import a package from that project I get an error that the package isn't defined, so how can I import packages from my java github repo inside the ipynb file?

openapi-generator gradle plugin output directory

I am using the openapi-generator Gradle plugin to generate model files from the open API schema.
With these settings in build.gradle script everything seems ok:
openApiGenerate {
globalProperties = [
apis: "false",
modelDocs: "false",
models: "Pet"
]
generatorName = "java"
generateModelTests = false
inputSpec = "$rootDir/src/main/resources/schema/my_schema.json".toString()
outputDir = "$rootDir".toString()
modelPackage = "org.openapi.example.model"
configOptions = [
dateLibrary: "java8",
serializationLibrary: "jackson",
library: "jersey1"
]
}
And the result classes are generated in the proper package:
The problem is here - I don't need them in my sources, I need them only at compile stage.
I want them to be generated in the build directory, to separate them from other logic.
But when I am changing the output-dir to "$buildDir/generated".toString() this happens:
Is there a way to get rid of the wrong packages "src.main.java"?
You can set the "sourceFolder" option to an empty string.
configOptions = [
sourceFolder: ""
]
This is an option of the generator not of the gradle plugin.
https://openapi-generator.tech/docs/generators/java

How to use Room Database in Android with Bazel?

I have lots of SQLite tables that has now become hard to manage at the app side because of multiple DAO classes. I am using Bazel as my build system but I can't figure out how to use Room DB with Bazel build system.
If you use a Maven artifact resolver like rules_jvm_external, it'll look something like this.
In your WORKSPACE file, add the dependency on the Room compiler:
load("#rules_jvm_external//:specs.bzl", "maven")
maven_install(
name = "maven",
artifacts = [
"androidx.room:room-runtime:2.1.0-alpha04",
"androidx.room:room-compiler:2.1.0-alpha04",
"com.google.guava:guava:28.1-android",
maven.artifact("com.google.auto", "auto-common", "0.10", neverlink = True),
# .. other artifacts
],
repositories = [
"https://maven.google.com",
"https://jcenter.bintray.com",
],
)
In a BUILD file (e.g. <project root>/BUILD), create the java_plugin target to expose the annotation processor for Room:
java_plugin(
name = "androidx_room_room_compiler_plugin",
processor_class = "androidx.room.RoomProcessor",
deps = ["#maven//:androidx_room_room_compiler"],
neverlink = True,
)
java_library(
name = "androidx_room_room_compiler_library",
exports = [
"#maven//:androidx_room_room_compiler",
],
exported_plugins = [
":androidx_room_room_compiler_plugin"
],
)
Finally, in your app's BUILD file, depend on the Room compiler and runtime:
android_library(
name = "lib_prod",
# ...
deps = [
"#maven//:androidx_room_room_runtime",
"//:androidx_room_room_compiler_library",
],
)
I have ported an Android sample app that uses the Room and Lifecycle libraries to build with Bazel here: https://github.com/jin/BasicRxJavaSample-Bazel

Spring Config Server - resolvePlaceholders + enviroment variables

I've got question about resolving environment variables in shared files of config server.
My current setup is pretty minimal :
src/main/resources/shared/application.yml :
application:
version: 0.0.1-early
test: ${JAVA_HOME}
src/main/resources/application.properties :
spring.profiles.active=native
spring.cloud.config.server.native.searchLocations=classpath:/shared
Using gradle with :
spring-boot-gradle-plugin:2.0.0.RELEASE
spring-cloud-dependencies:Camden.SR7
And then of course compile 'org.springframework.cloud:spring-cloud-config-server' in deps
Problem :
GET http://localhost:8888/apptest/application gives me :
{
"name": "apptest",
"profiles": [
"application"
],
"label": null,
"version": null,
"state": null,
"propertySources": [
{
"name": "classpath:/shared/application.yml",
"source": {
"application.version": "0.0.1-early",
"application.test": "${JAVA_HOME}"
}
}
]
}
So env variable is not resolved. Same thing is with :
http://localhost:8888/apptest/application?resolvePlaceholders=true
http://localhost:8888/lab/apptest-application.properties?resolvePlaceholders=true
http://localhost:8888/lab/apptest-application.properties?resolvePlaceholders=false
http://localhost:8888/apptest-application.properties?resolvePlaceholders=true
I've looked at Spring cloud config server. Environment variables in properties but solution didn't help me + there where few new versions since then. So I'm opening new question.
Actually it's not a bug and everything is fine. I did not understood how Config server works.
http://localhost:8888/apptest/application - returns yet not resolved value of ${JAVA_HOME}
When we get ei. into container "C" that pings Config Service for configuration and do curl http://config:8888/apptest/application we get the same - unresolved ${JAVA_HOME}
But when we look into Spring application ei. in container "C" and try to inject #Value("${application.test}") somewhere, we get proper value or info that env variable was not set.
It means that environment variables are resolved on client side.
Thanks to that I've understood how NOT production ready env_variables approach is.
Well the changes happened here https://github.com/spring-cloud/spring-cloud-config/commit/f8fc4e19375d3b4c0c2562a71bc49ba288197100 that removes the support of replacing the environment variables.
You can always add a new controller and override the behaviour of the EnvironmentPropertySource#prepareEnvironment

jaxb too slow when parsing large file on servicemix

I'm trying to parse an xml file containing 100000 lines
schéma like :
<RplyColl ...>
<Rply>
....
</Rply>
</RplyColl>
<EnvColl>
<Env>
...
</Env>
</EnvColl>
<FpdColl rowID="73">
<Fpd>
...
</Fpd>
</FpdColl>
I parse the file like this:
final Unmarshaller unMarshaller = JAXBContext.newInstance("my.context", ObjectFactory.class.getClassLoader()).createUnmarshaller();
object= unMarshaller.unmarshal(new StreamSource(new StringReader(new String(message.getBytes(), "UTF-8"))));
I'm using servicemix with jaxb-impl
90] [Active ] [ ] [ ] [ 50] JAXB2 Basics - Runtime (0.6.4)
[ 91] [Active ] [ ] [ ] [ 50] Apache ServiceMix :: Bundles :: jaxb-impl (2.2.11.1)
So when i'm going in debug mode i see my jaxbcontext is :
bundle://91.0:1/com/sun/xml/bind/v2/runtime/JAXBContextImpl.class Build-Id: 2.2.11
Classes known to this context:
...
...
After this, the unmarshall method call take 3minutes30sec :(
I try this code in Unit test and it take 10seconds,
Here is the jaxbcontext class to compare:
jar:file:/D:/maven/repository/com/sun/xml/bind/jaxb-impl/2.2.11/jaxb-impl-2.2.11.jar!/com/sun/xml/bind/v2/runtime/JAXBContextImpl.class Build-Id: 2.2.11
Classes known to this context:
[B
...
...
So why my servicemix take 3minutes30seconds and my unit test take only 10 seconds during unmarshall operation?
Does i miss something?
thank you very much
VERSION
jaxb-impl 2.2.11
servicemix :5.5.2
finally i found my answer:
in servicemix someone change the value of :
org.apache.servicemix.specs.timeout=100
i change it to :
org.apache.servicemix.specs.timeout=0
and now it rocks!

Categories