I have been trying a normal Java API using the same example as mentioned in this page. I had to download various JARs:
java-client-api-3.0.7.jar
slf4j-api-1.7.25.jar
com.fasterxml.jackson.databind.jar
jackson-core-2.2.0-rc1.jar
apache-httpcomponents-httpclient.jar
javax.ws.rs.jar
It still gives me NoClassDefFoundError on
Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/http/params/HttpParams at com.marklogic.client.DatabaseClientFactory.newClientImpl(DatabaseClientFactory.java:322)
My Code:
import com.marklogic.client.DatabaseClient;
import com.marklogic.client.DatabaseClientFactory;
import com.marklogic.client.document.*;
import com.marklogic.client.io.*;
public class JavaML {
public static void main(String args[]) {
System.out.println("Here");
DatabaseClient client =
DatabaseClientFactory.newClient(
"localhost", 8000,"admin", "######",
DatabaseClientFactory.Authentication.DIGEST);
JSONDocumentManager docMgr = client.newJSONDocumentManager();
docMgr.write("/afternoon-drink",
new StringHandle("{name: \"Iced Mocha\", size: \"Grandé\", tasty: true}"));
String doc = docMgr.read("/afternoon-drink", new StringHandle()).get();
//System.out.println(doc);
}
}
Run:
javac -cp .;%CLASSES% JavaML.java
java -cp .;%CLASSES% JavaML
%CLASSES% - path to all the jars mentioned above.
Please help - am I missing something? Or is it a mandate that I have to download all these JARs?
It appears from the error message that you are missing the httpcore-4.1.jar.
However, there are a number of other dependencies that you are likely to be missing as well.
If you are maintaining your own set of dependencies and building your classpath manually, then you should download the Java API zip, which includes all of the necessary dependencies in the /lib directory.
However, it would be much easier if you were to use a build/dependency management tool, such as Gradle or Maven. If you have the Java API as a dependency, it will download all of it's dependencies and will compile your application with the necessary libraries on your classpath. It makes upgrades much easier, facilitates automation, and is a best practice in favor of manually collecting and organizing dependencies.
Related
I’m writing a Java-programm for school that also uses Thrift.
The problem is not so much the general programm/programm-logic itself, but just importing Thrift (to use it in a specific part).
My basic -possibly wrong- understanding is that you write the programm-code (here empfaenger.java), then you import Thrift into this file by adding the needed import-statements, e.g.:
import org.apache.thrift.TException;
import org.apache.thrift.protocol.TBinaryProtocol;
import org.apache.thrift.protocol.TProtocol;
import org.apache.thrift.transport.TSocket ;
import org.apache.thrift.transport.TTransport;
and adding a file in the same directory from which they can actually can be imported, in this case libthrift-0.13.0.jar.(1) Then you later also import a compiled .thrift-file with the language-specific realization oft he IDL-code, that iself again imports some Thrift-classes. This file is here named syncautohersteller.
EDIT: The approach with the .jar-file was recommended by the prof.
Current project-structure (as seen in InteliJ):
The problem is now just that all the Thrift import-statements all throw errors, e.g.
empfaenger.java
java: package org.apache.thrift does not exist
syncautohersteller
package javax.annotation does not exist
so clearly i’m doing something wrong.
Does anybody know how to fix this?
(1) I got the file from the Thrift folder (Home/Downloads/thrift-0.13.0/lib/java/build/libs and then the first of the three .jar-files in the folder) after installing Thrift using ./configure, sudo make and sudo make install and trying to verify by running “~/Downloads/thrift-0.13.0$ thrift –version” with result
Thrift version 0.13.0
In IntellJ Idea to add external Jars you can find some useful information in this question: Correct way to add external jars (lib/*.jar) to an IntelliJ IDEA project.
I suggest you to manage the project's dependencies through Maven, which help you to add JAR dependencies to the classpath in a simpler way.
First, you have to convert your project into a Maven project as explained in IntelliJ Idea documentation.
Then you can follow these steps:
Go to Maven repository website
Search for Thrift
Select first result
Select the version you need
Copy the maven dependency
org.apache.thrift
libthrift
0.13.0
Add maven dependency to your pom.xml file
Execute a mvn clean install, after clicking the following button in IntelliJ
This process will help you and people which work with you to manage in a simpler way the dependencies of the project.
You can do it the simplest way with the Gradle, something like this:
build.gradle.kts:
repositories {
mavenCentral()
}
dependencies {
implementation("org.apache.thrift:libthrift:0.13.0")
}
I'm trying to use RichTextFX but when I do try to use it I get a long error (see below). I'm using IntelliJ IDEA 2018.3.4, JDK 8 and Gradle version 4.8.1. My current way of trying to install it is I downloaded the jar (richtextfx-0.9.2.jar) and move it into a folder in the project. I then add the jar as a library and save. Then I go to my JavaFX file, and make a new Code AreaCodeArea c = new CodeArea(); This will result in a large error. I think there is an error with gradle installing. I haven't used it before so I'm very new at it.
I am able to use the actual files (when not compiled into a JAR) to run the demos provided. This is in a different project, and the gradle libraries are added as external libraries when the project was created. The project I'm working on however does not have these extra external libraries.
Any support on what to do would be greatly appreciated. Thanks!
There Exceptions are:
java.lang.reflect.InvocationTargetException
java.lang.RuntimeException: Exception in Application start method
java.lang.NoClassDefFoundError: org/fxmisc/flowless/Virtualized
java.lang.ClassNotFoundException: org.fxmisc.flowless.Virtualized
Test java code
package demo.javafx;
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.layout.Pane;
import javafx.stage.Stage;
import org.fxmisc.richtext.CodeArea;
public class TestRichTextFX extends Application {
#Override
public void start(Stage primaryStage) {
Pane root = new Pane();
Scene scene = new Scene(root);
CodeArea c = new CodeArea();
primaryStage.setAlwaysOnTop(true);
primaryStage.setTitle("");
primaryStage.setScene(scene);
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}
You can't just download the JAR for RichTextFX and add it to your project. RichTextFX itself has dependencies that only downloading the JAR won't provide. In your case, you're seeing an error saying the Flowless dependency is missing. However, you don't have to download the JAR files yourself—that's what Gradle is for. You just need to add the dependency to your build script; the README of RichTextFX provides an example1.
repositories {
mavenCentral()
}
dependencies {
implementation 'org.fxmisc.richtext:richtextfx:0.9.2'
}
Gradle will pull in all the needed dependencies for you. For more information, see Introduction to Dependency Management.
Note that doing it this way you shouldn't have to declare a library in IntelliJ. If your project is setup as a Gradle project then IntelliJ will see the dependencies declared in your build script(s) and add them to the project. You may have to "import changes" after modifying the build script. These help pages provide more information.
1. The example uses compile but Gradle recommends implementation—at least for the more recent versions (compile is deprecated). Note that Android projects may still use compile, though I'm not sure.
It's possible this has already been asked, but if so I've been unable to find it.
The Ask
Is there a way in Java 1.7/1.8, short of implementing a custom classloader, to make the application classpath higher-priority (loaded earlier) than the extension classpath?
The Issue
We have a platform with multiple apps using Apache's log4j library. We also have a custom JCA security provider installed in [jre]/lib/ext which also uses log4j. In order to do so, log4j has to be installed along with the provider in the ext directory.
One of the apps on the platform (Apache's activemq) relies on an older version of log4j/slf4j than the provider's. And since the provider's log4j jars are in [jre]/lib/ext, they override activemq's, causing a NoSuchMethodError to occur:
java.lang.NoSuchMethodError: org.slf4j.spi.LocationAwareLogger.log(Lorg/slf4j/Marker;Ljava/lang/String;ILjava/lang/String;Ljava/lang/Throwable;)V
So is there a way to make the jars in the application classpath supersede the extension directory?
Reproduction
You can reproduce this issue by installing these jars into [jre]/lib/ext:
log4j-1.2.17.jar
slf4j-api-1.7.2.jar
slf4j-log4j12-1.7.2.jar
And by installing these jars in the application classpath:
log4j-1.2.14.jar
slf4j-api-1.5.11.jar
slf4j-log4j12-1.5.11.jar
And by running this code:
import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;
import org.slf4j.LoggerFactory;
import org.slf4j.spi.LocationAwareLogger;
public class LogApp {
public final static LocationAwareLogger logger =
(LocationAwareLogger) LoggerFactory.getLogger(LogApp.class);
public static void main(String[] args) throws NoSuchAlgorithmException {
// Make sure custom security provider has been initialized.
SecureRandom rand = SecureRandom.getInstanceStrong();
rand.doubles();
//ClassLoader cl = ClassLoader.getSystemClassLoader();
LogApp.logger.error("error: {}: {}", "string", new Exception());
LogApp.logger.log(null, LogApp.class.getCanonicalName(),
logger.ERROR_INT, "some message", new Exception());
}
}
Which should generate this error:
Exception in thread "main" java.lang.NoSuchMethodError: org.slf4j.spi.LocationAwareLogger.log(Lorg/slf4j/Marker;Ljava/lang/String;ILjava/lang/String;Ljava/lang/Throwable;)V
at LogApp.main(LogApp.java:23)
Answer
Run Java with this parameter:
-Xbootclasspath/p:[path_to_app_libs]/log4j-1.2.14.jar:[path_to_app_libs]/slf4j-api-1.5.11.jar:[path_to_app_libs]/slf4j-log4j12-1.5.11.jar
If you have control how your java is run, you can use -Xbootclasspath/a:path/to/your.jar to force loading specific jar ahead of extensions.
Upgrading log4j in lib/ext might be easier solution. Hopefully, whatever requires log4j in lib/ext will survive newer version, otherwise, you will have issues in any case, as bootclasspath will take precedence and force your lib/ext code to use new version.
In any case, having libraries with specific versions of log4j is bad pratice. Anything but final application, should probably use slf4j without specific binding, or possibly JUL.
I have spring/postgis service with some spatial capabilities, but I haven't found Java library that can interpolate point to a linestring.
I'd like to to use this geoscript library to do that, but I'm not sure how to do that.
I've read about how to dynamically load groovy class in Java code, but I'm not sure how to do that with external library code. Should I just add this geoscript to build.gradle?
Or maybe there is some more efficient ways to do that?
I just gave it a try and it worked flawlessly. In the end a compiled Groovy library is just Java byte code.
Citing from Groovy in Action, Chapter 1.1.2 Playing nicely with Java: seamless integration:
Groovy is only
a new way of creating ordinary Java classes—
from a runtime perspective, Groovy is Java
with an additional JAR file as a dependency.
So add these dependencies
compile 'org.codehaus.groovy:groovy-all:2.4.5'
compile 'org.geoscript:geoscript-groovy:1.6.0'
and this repository
maven {
url "http://repo.boundlessgeo.com/main"
}
to your gradle build file and you are good to go.
This is the Java code I tried:
import geoscript.geom.LineString;
import geoscript.geom.Point;
public class Main {
public static void main(String[] args) {
LineString line = new LineString(new Point(1,2), new Point(3,4), new Point(4,5));
Point point = line.interpolatePoint(0.5);
System.out.println(point); // printed POINT (2.5 3.5)
}
}
Got this error while trying opennlp chunking:
NoClassDefFoundError: opennlp/tools/chunker/ChunkerModel
Here is the basic code:
import java.io.*;
import opennlp.tools.chunker.*;
public class test{
public static void main(String[] args) throws IOException{
ChunkerModel model = null;
InputStream modelIn = new FileInputStream("en-parser-chunking.bin");
model = new ChunkerModel(modelIn);
}
}
I don't see any NLP-specific reasons here, so just check tutorials about NoClassDefFoundError, for example:
Verify that all required Java classes are included in the
application’s classpath. The most common mistake is not to include all
the necessary classes, before starting to execute a Java application
that has dependencies on some external libraries.
The classpath of the
application is correct, but the Classpath environment variable is
overridden before the application’s execution."
or related question.
In particular, check that you have appropriate (and only one) version of opennlp jar in your classpath.
*it is not a good style to import all content of the package (by using wildcard) - instead, use IDE's support: e.g. ctrl+shift+o in Eclipse (ctrl+alt+o in IDEA) automatically resolves all needed imports.