Connecting to remote HBase service using Java - java

I have a small sample code in which I try to establish a connection to a remote HBase entity. The code runs on a windows machine without HBase installed and I try to connect to a remote Ubuntu Server that has it installed and running. The IP in the below snippet is of course just a placeholder.
The code is as follows:
public static void main(String[] args) {
Configuration conf = HBaseConfiguration.create();
HBaseAdmin admin = null;
String ip = "10.10.10.10";
String port = "2181";
conf.set("hbase.zookeeper.quorum", ip);
conf.set("hbase.zookeeper.property.clientPort", port);
try {
admin = new HBaseAdmin(conf);
boolean bool = admin.tableExists("sensor_data");
System.out.println("Table exists? " + bool);
} catch (IOException e) {
e.printStackTrace();
}
}
But for some reason I get this error:
org.apache.hadoop.hbase.DoNotRetryIOException: java.lang.IllegalAccessError: tried to access method com.google.common.base.Stopwatch.<init>()V from class org.apache.hadoop.hbase.zookeeper.MetaTableLocator
at org.apache.hadoop.hbase.client.RpcRetryingCaller.translateException(RpcRetryingCaller.java:229)
at org.apache.hadoop.hbase.client.RpcRetryingCaller.callWithoutRetries(RpcRetryingCaller.java:202)
at org.apache.hadoop.hbase.client.ClientScanner.call(ClientScanner.java:320)
at org.apache.hadoop.hbase.client.ClientScanner.nextScanner(ClientScanner.java:295)
at org.apache.hadoop.hbase.client.ClientScanner.initializeScannerInConstruction(ClientScanner.java:160)
at org.apache.hadoop.hbase.client.ClientScanner.<init>(ClientScanner.java:155)
at org.apache.hadoop.hbase.client.HTable.getScanner(HTable.java:811)
at org.apache.hadoop.hbase.MetaTableAccessor.fullScan(MetaTableAccessor.java:602)
at org.apache.hadoop.hbase.MetaTableAccessor.tableExists(MetaTableAccessor.java:366)
at org.apache.hadoop.hbase.client.HBaseAdmin.tableExists(HBaseAdmin.java:303)
at org.apache.hadoop.hbase.client.HBaseAdmin.tableExists(HBaseAdmin.java:313)
at com.twoBM.Tests.HBaseWriter.main(HBaseWriter.java:26)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:147)
Caused by: java.lang.IllegalAccessError: tried to access method com.google.common.base.Stopwatch.<init>()V from class org.apache.hadoop.hbase.zookeeper.MetaTableLocator
at org.apache.hadoop.hbase.zookeeper.MetaTableLocator.blockUntilAvailable(MetaTableLocator.java:596)
at org.apache.hadoop.hbase.zookeeper.MetaTableLocator.blockUntilAvailable(MetaTableLocator.java:580)
at org.apache.hadoop.hbase.zookeeper.MetaTableLocator.blockUntilAvailable(MetaTableLocator.java:559)
at org.apache.hadoop.hbase.client.ZooKeeperRegistry.getMetaRegionLocation(ZooKeeperRegistry.java:61)
at org.apache.hadoop.hbase.client.ConnectionManager$HConnectionImplementation.locateMeta(ConnectionManager.java:1185)
at org.apache.hadoop.hbase.client.ConnectionManager$HConnectionImplementation.locateRegion(ConnectionManager.java:1152)
at org.apache.hadoop.hbase.client.RpcRetryingCallerWithReadReplicas.getRegionLocations(RpcRetryingCallerWithReadReplicas.java:300)
at org.apache.hadoop.hbase.client.ScannerCallableWithReplicas.call(ScannerCallableWithReplicas.java:153)
at org.apache.hadoop.hbase.client.ScannerCallableWithReplicas.call(ScannerCallableWithReplicas.java:61)
at org.apache.hadoop.hbase.client.RpcRetryingCaller.callWithoutRetries(RpcRetryingCaller.java:200)
... 15 more
I am using Gradle to build my project and currently I am only using the two following dependencies:
compile 'org.apache.hive:hive-jdbc:2.1.0'
compile 'org.apache.hbase:hbase:1.1.6'
Does anyone know to fix this problem? I have tried googling this problem, but without any of the found links providing an actual solution.
Best regards

This is definitely Google Guava's dependency conflict. The default constructor of Stopwatch class became private since Guava v.17 and marked deprecated even earlier.
So to HBase Java client works properly you need Guava v.16 or earlier. Check the way you build your application (Maven/Gradle/Classpath) and find the dependency which uses Guava v.17+. After that, you can resolve the conflict.

I received the same error and had to spend for 5 days to know the issue.
I added following dependency and its gone.
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>15.0</version>
</dependency>

You can use maven shade plugin to solve this issue. That a look at this blog post. Here is an example (actually a snippet from my working pom.)
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>3.0.0</version>
<executions>
<execution>
<id>assemble-all</id>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
<configuration>
<!--<finalName>PROJECT_NAME-${project.version}-shaded</finalName>-->
<relocations>
<relocation>
<pattern>com.google.common</pattern>
<shadedPattern>shaded.com.google.common</shadedPattern>
</relocation>
<relocation>
<pattern>com.google.protobuf</pattern>
<shadedPattern>shaded.com.google.protobuf</shadedPattern>
</relocation>
</relocations>
<artifactSet>
<includes>
<include>*:*</include>
</includes>
</artifactSet>
</configuration>
</execution>
</executions>
</plugin>

Related

Creating executable JAR in IntelliJ (Java 18, JavaFX 18 Maven project), "WARNING: Unsupported JavaFX configuration..."

I have a Java 18, JavaFX 18 Maven project which has a lot of libraries, beside the javaFX libraries, that needs to be included in the artifact. I want to create an artifact, a jar, which contains all dependencies. I started following this video to create the jar: https://www.youtube.com/watch?v=UKd6zpUnAE4
Summarizing my steps, and referring to the steps in the video:
In IntelliJ in Project Structure/Project Settings/Libraries I removed all Maven added libraries, and added C:\Program Files\Java\javafx-sdk-18.0.2\lib
After, in Run/Edit Configurations... I added a VM options, and in that window I added
--module-path "C:\Program Files\Java\javafx-sdk-18.0.2\lib"
--add-modules javafx.controls,javafx.fxml
After, in the video, "Ken" the host of the video creates a class, with a main() method, that runs the application original main() class. I did not need this step, because I already has a class that does the same.
After, File/Project Structure/Project Settings/Artifact/ I added a JAR/From modules with dependencies/ and I choose the class I recently created, and shortened the path until the source folder (src)
Following this step, after I clicked add (+), and added the content of "...javafx-sdk-18.0.2/bin" all dll's and everything (all files).
Here, at this point, separate from the video, I also created a folder named "jars" and put all Maven dependencies jars in that folder.
According to the video, after these steps, with a double click on the artifact the jar runs without a problem.
However, I needed I more step. My dependency jars are signed jars, so I needed to open the artifact with WinRAR and remove the *.SF, *.DSA and *.RSA files. Earlier this caused me problems so I followed the idea here: Invalid signature file digest for Manifest main attributes exception while trying to run jar file, and here: "Invalid signature file" when attempting to run a .jar
After this, everything should be fine, however not :( The jar doesn't run on double click. When I run it from command line, I receive the following error:
$ java -jar jHasher.jar
jan. 15, 2023 3:19:07 DU. com.sun.javafx.application.PlatformImpl startup
WARNING: Unsupported JavaFX configuration: classes were loaded from 'unnamed module #3a178016'
javafx.fxml.LoadException:
unknown path:53
at javafx.fxml.FXMLLoader.constructLoadException(FXMLLoader.java:2707)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2685)
at javafx.fxml.FXMLLoader.load(FXMLLoader.java:2532)
at view.GUI.start(GUI.java:29)
at com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$9(LauncherImpl.java:847)
at com.sun.javafx.application.PlatformImpl.lambda$runAndWait$12(PlatformImpl.java:484)
at com.sun.javafx.application.PlatformImpl.lambda$runLater$10(PlatformImpl.java:457)
at java.base/java.security.AccessController.doPrivileged(AccessController.java:399)
at com.sun.javafx.application.PlatformImpl.lambda$runLater$11(PlatformImpl.java:456)
at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:96)
at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
at com.sun.glass.ui.win.WinApplication.lambda$runLoop$3(WinApplication.java:184)
at java.base/java.lang.Thread.run(Thread.java:833)
Caused by: java.lang.RuntimeException: java.lang.reflect.InvocationTargetException
at com.sun.javafx.fxml.BeanAdapter.put(BeanAdapter.java:263)
at com.sun.javafx.fxml.BeanAdapter.put(BeanAdapter.java:54)
at javafx.fxml.FXMLLoader$Element.applyProperty(FXMLLoader.java:523)
at javafx.fxml.FXMLLoader$Element.processValue(FXMLLoader.java:373)
at javafx.fxml.FXMLLoader$Element.processPropertyAttribute(FXMLLoader.java:335)
at javafx.fxml.FXMLLoader$Element.processInstancePropertyAttributes(FXMLLoader.java:245)
at javafx.fxml.FXMLLoader$ValueElement.processEndElement(FXMLLoader.java:778)
at javafx.fxml.FXMLLoader.processEndElement(FXMLLoader.java:2924)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2639)
... 11 more
Caused by: java.lang.reflect.InvocationTargetException
at java.base/jdk.internal.reflect.DirectMethodHandleAccessor.invoke(DirectMethodHandleAccessor.java:119)
at java.base/java.lang.reflect.Method.invoke(Method.java:577)
at com.sun.javafx.fxml.ModuleHelper.invoke(ModuleHelper.java:102)
at com.sun.javafx.fxml.BeanAdapter.put(BeanAdapter.java:259)
... 19 more
Caused by: java.lang.UnsupportedOperationException: Cannot resolve 'win10-document'
at org.kordamp.ikonli.AbstractIkonResolver.resolve(AbstractIkonResolver.java:61)
at org.kordamp.ikonli.javafx.IkonResolver.resolve(IkonResolver.java:73)
at org.kordamp.ikonli.javafx.FontIcon.setIconLiteral(FontIcon.java:251)
at java.base/jdk.internal.reflect.DirectMethodHandleAccessor.invoke(DirectMethodHandleAccessor.java:104)
... 22 more
I have searched the following error message. I also found some posts on StackOverflow, however they are not clear to me, and I was not able to fix this issue. Please, guide me how to proceed. All suggestions are highly appreciated.
After several hard day, I was able to create the executable jar. I'd like to share the know-how with you.
After 5th step, skipping the WinRAR for removing the *.SF, *.DSA and *.RSA files. I added maven-shade-plugin to my pom.xml. The shade plugin can automatically remove these unwanted files, but unfortunately by itself cannot create a runnable JAR, because throws again exceptions and doesn't run on double click (JavaFX 18 Maven IntelliJ: Graphics Device initialization failed for: d3d, sw Error initializing QuantumRenderer: no suitable pipeline found).
To avoid this exception and include the unlocated/missing JavaFX files we have to repack the already packed JAR. To do that, I used the spring-boot-maven-plugin. After setting up the plugins (code below), you have to run the plugins with maven in a correct order! My maven command was the following: mvn clean package spring-boot:repackage
That it, finally the created JAR (JAR of the JAR) can run on double click.
My pom.xml's corresponding parts:
Shade plugin setting:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>3.4.1</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
<configuration>
<transformers>
<transformer implementation="org.apache.maven.plugins.shade.resource.ServicesResourceTransformer"/>
<transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
<mainClass>controller.Start</mainClass>
</transformer>
</transformers>
<minimizeJar>true</minimizeJar>
<filters>
<filter>
<artifact>*:*</artifact>
<excludes>
<exclude>META-INF/*.SF</exclude>
<exclude>META-INF/*.DSA</exclude>
<exclude>META-INF/*.RSA</exclude>
</excludes>
</filter>
</filters>
</configuration>
</execution>
</executions>
</plugin>
The Spring-boot-maven-plugin setting (this should be placed outside the plugins section, at the very end of the pom.xml):
<pluginManagement>
<plugins>
<plugin>
<!-- mvn clean package spring-boot:repackage -->
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<executions>
<execution>
<goals>
<goal>repackage</goal>
</goals>
<configuration>
<classifier>spring-boot</classifier>
<mainClass>
controller.Start
</mainClass>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</pluginManagement>
Make sure to run the plugins in the correct order, as mentioned above! I found this resource very useful: https://www.baeldung.com/spring-boot-repackage-vs-mvn-package

Setting CventSessionHeader CVENT API WSDL

I am generating Java classes from the CVENT WSDL file using a maven plugin (see the sample below from my POM file). The code generates successfully.
I then call the code (see below) (the start and end dates passed into the getUpdated call are parameters to my method)
When I run / debug, it connects succesfully, but the getUpdated call fails:
Fault from server: INVALID_CVENT_HEADER_VALUE
In examples online, I can see that I need to set the header on the session - but I don't see any method in V200611Soap that allows me to set it.
Anyone with experience of this, or any sample code?
Thanks in advance.
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>jaxws-maven-plugin</artifactId>
<version>1.12</version>
<configuration>
<wsdlUrls>
<wsdlUrl>https://api.cvent.com/soap/V200611.ASMX?WSDL</wsdlUrl>
</wsdlUrls>
<keep>true</keep>
<sourceDestDir>${basedir}/target/generated/src/main/java</sourceDestDir>
</configuration>
<executions>
<execution>
<goals>
<goal>wsimport</goal>
</goals>
</execution>
</executions>
</plugin>
V200611 aV200611 = new V200611();
V200611Soap soap = aV200611.getV200611Soap();
String accountNumber = "xxxxxx";
String userName = "xxxxxx";
String password = "xxxxxx";
LoginResult logingResult = soap.login(accountNumber, userName, password);
CventSessionHeader header = new CventSessionHeader();
header.setCventSessionValue(logingResult.getCventSessionHeader());
GetUpdatedResult getUpdatedResult = soap.getUpdated(CvObjectType.TRAVEL, startDateXMLGregorianCalendar, endDateXmlGregorianCalendar);
I fixed by changing to use the cxf plugin
Then added the wsdlOption
<extendedSoapHeaders>true</extendedSoapHeaders>
Which puts the arguments that are implicit (in the wsdl:binding but not wsdl:port), into the generated API classes.

jersey with jackson works when I run with netbeans but now when execute the jar file [duplicate]

I am using the Java Jersey framework(with Maven), and use IntelliJ as my IDE. I have encountered this runtime exception that ONLY happens when I try to run the code from the command line (using maven to compile and then java -jar ) but NOT when running within IntelliJ, which is strange.
I have some Java code that will try to make an HTTP GET on some remote URL and try to read the returned JSON into some Lombok POJO :
String targetUrl = "some valid URL";
WebTarget webTarget = client.target(targetUrl);
Response response = webTarget.request(MediaType.APPLICATION_JSON_TYPE).get();
ParseResponse parseResponse = response.readEntity(ParseResponse.class);
I am not sure why, but when it hits that last line that does the "readEntity()" method, I will get the error below:
org.glassfish.jersey.message.internal.MessageBodyProviderNotFoundException: MessageBodyReader not found for media type=text/json; charset=utf-8
This is strange, because I definitely have the jersey-media-json-jackson dependency specified in my pom.xml :
<dependency>
<groupId>org.glassfish.jersey.media</groupId>
<artifactId>jersey-media-json-jackson</artifactId>
<version>2.23</version>
</dependency>
and this is my POJO class that I was trying to readEntity() into :
#Data
#JsonIgnoreProperties(ignoreUnknown = true)
public class ParseResponse {
#JsonProperty("id")
private Integer id;
...Other params...
}
And like I mentioned before, it is strange that this only happens when I try to run on the command line like this but there is no error when running in IntelliJ:
mvn clean package
java -jar target/NameOfJar.jar
Did I miss something obvious here? I have looked at other people with similar issues like this online but haven't found a solution.
Thanks
IS
If you look inside the jersey-media-json-jackson jar you should see a file
META-INF/services/org.glassfish.jersey.internal.spi.AutoDiscoverable
The contents of this file should be a single fully qualified name of a class that implements the name of the file, namely
org.glassfish.jersey.jackson.internal.JacksonAutoDiscoverable
This file is used by Jersey auto-discoverable mechanism to automatically register features without us having to explicitly register them. Briefly, how it works, is that all Jersey modules/jars that have components that should be automatically registered, should have the above named file located in the jar, with the contents being the name(s) of the auto-discoverable component. Jersey will then use the Service Loader pattern to load the classes named in the file, and register them.
The problem this causes when creating uber jars is that you can only have one copy of a file, you can't have duplicates. So what if we have multiple jars with the above file? Well only one of those files will be included in the uber jar. Which one? Who knows, but there is only one lucky winner. So for the rest of the jars, their auto-discover mechanism never kicks in. This is the case with your Jackson feature, where the auto-discoverable registers the JacksonFeature. You can try to explicitly register with your application, and you should see that it now works.
But what about other jars/modules that may have this file? It's for this reason that when creating uber jars, you should use the maven-shade-plugin. What this plugin allows you to do, is combine the contents of the files so that all the discoverables get included into that one single file. Below is an example usage
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>2.3</version>
<configuration>
<createDependencyReducedPom>true</createDependencyReducedPom>
<filters>
<filter>
<artifact>*:*</artifact>
<excludes>
<exclude>META-INF/*.SF</exclude>
<exclude>META-INF/*.DSA</exclude>
<exclude>META-INF/*.RSA</exclude>
</excludes>
</filter>
</filters>
</configuration>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
<configuration>
<transformers>
<transformer implementation="org.apache.maven.plugins.shade.resource.ServicesResourceTransformer"/>
<transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
<mainClass>com.example.YourApp</mainClass>
</transformer>
</transformers>
</configuration>
</execution>
</executions>
</plugin>
This example was actually taken from Dropwizard's Getting Started. You can check it out for further explanation. The main part of concern the ServicesResorceTransformer, which is what concatenates the services files.

Getting UnsatisfiedLinkError: no jnilept in java.library.path when I create TessBaseAPI

I am new to java cpp and tesseract-ocr. I am stuck with one issue from couple of hours.
I am getting UnsatisfiedLinkError: no jnilept in java.library.path when I create TessBaseAPI. Below is the piece of my code.
public static void tesseractForPdf(String filePath) throws Exception {
BytePointer outText;
TessBaseAPI api = new TessBaseAPI();//getting the UnsatisfiedLinkError exception here.
// Initialize tesseract-ocr with English, without specifying tessdata path
if (api.Init(".", "ENG") != 0) {
System.err.println("Could not initialize tesseract.");
System.exit(1);
}
// Open input image with leptonica library
PIX image = pixRead(filePath);
api.SetImage(image);
// Get OCR result
outText = api.GetUTF8Text();
String string = outText.getString();
System.out.println("OCR output:\n" + string);
// Destroy used object and release memory
api.End();
outText.deallocate();
pixDestroy(image);
}
Exception I am getting on TessBaseAPI api = new TessBaseAPI(); line
Exception in thread "main" java.lang.UnsatisfiedLinkError: no jnilept in java.library.path
at java.lang.ClassLoader.loadLibrary(Unknown Source)
at java.lang.Runtime.loadLibrary0(Unknown Source)
at java.lang.System.loadLibrary(Unknown Source)
at org.bytedeco.javacpp.Loader.loadLibrary(Loader.java:702)
at org.bytedeco.javacpp.Loader.load(Loader.java:500)
at org.bytedeco.javacpp.Loader.load(Loader.java:417)
at org.bytedeco.javacpp.lept.<clinit>(lept.java:10)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Unknown Source)
at org.bytedeco.javacpp.Loader.load(Loader.java:472)
at org.bytedeco.javacpp.Loader.load(Loader.java:417)
at org.bytedeco.javacpp.tesseract$TessBaseAPI.<clinit>(tesseract.java:3648)
at om.practiceproblems.BasicTesseractExampleTest.givenTessBaseApi_whenImageOcrd_thenTextDisplayed(BasicTesseractExampleTest.java:35)
at com.practiceproblems.BasicTesseractExampleTest.main(BasicTesseractExampleTest.java:22)
Caused by: java.lang.UnsatisfiedLinkError: no liblept in java.library.path
at java.lang.ClassLoader.loadLibrary(Unknown Source)
at java.lang.Runtime.loadLibrary0(Unknown Source)
at java.lang.System.loadLibrary(Unknown Source)
at org.bytedeco.javacpp.Loader.loadLibrary(Loader.java:702)
at org.bytedeco.javacpp.Loader.load(Loader.java:491)
... 9 more
I am using java-presets libraries tesseract-3.04.01-1.2 and leptonica-1.73-1.2.jar with javacpp-1.2.1 in my example.I have windows OS.
I did see this https://github.com/bytedeco/javacpp-presets/issues/46 and couple of discussions on SO and github which pointed that this issue is fixed in jacacpp-1.1 itself.But I am using javacpp1.2.
I would really appreciate any help in resolving the issue or finding the root cause.
you could clone or download the project:
https://github.com/bytedeco/javacpp-presets#the-cppbuildsh-scripts
then build the modules: JavaCPP Presets for Tesseract and JavaCPP Presets for Leptonica;
(to build the leptonica project you maybe need to install nasm
https://www.nasm.us/)
(to build the entire javacpp-presets project you also have to install cmake)
that would create the native libraries:
libjnilept.so and libjnitesseract.so
then you have to specify the jni.library.path
You can do it with:
System.setProperty(JAVA_LIBRARY_PATH, tmpDirName);
/* Optionally add these two lines */
System.setProperty("jna.library.path", tmpDirName);
System.setProperty("jni.library.path", tmpDirName);
final Field fieldSysPath;
fieldSysPath = ClassLoader.class.getDeclaredField(SYS_PATHS);
fieldSysPath.setAccessible(true);
fieldSysPath.set(null, null);
(you could instead specify the -Djava.library.path= on the virtual machine options)
you only have to put the generated files:
libjnilept.so and libjnitesseract.so in some folder and set this path for: jni.library.path
<dependency>
<groupId>org.bytedeco.javacpp-presets</groupId>
<artifactId>tesseract</artifactId>
<version>4.0.0-1.4.4</version>
</dependency>
<dependency>
<groupId>org.bytedeco.javacpp-presets</groupId>
<artifactId>leptonica</artifactId>
<version>1.77.0-1.4.4</version>
</dependency>
you can also try to add
<dependency>
<groupId>org.bytedeco.javacpp-presets</groupId>
<artifactId>leptonica-platform</artifactId>
<version>1.77.0-1.4.4</version>
</dependency>
<dependency>
<groupId>org.bytedeco.javacpp-presets</groupId>
<artifactId>tesseract-platform</artifactId>
<version>4.0.0-1.4.4</version>
</dependency>
and add into the build a maven-assembly-plugin
<build>
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<configuration>
<archive>
<manifest>
mainClass>fully.qualified.MainClass</mainClass>
</manifest>
</archive>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
</configuration>
<!-- new -->
<executions>
<execution>
<id>make-assembly</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>
</build>
Also, you could also get an error like this:
sscanf(line, "%" QUOTED_TOKENSIZE "s %" QUOTED_TOKENSIZE "s %f %f",
linear_token, essential_token, &ParamDesc[i].Min, &ParamDesc[i].Max) == 4
:Error:Assert failed:in file clusttool.cpp, line 73
#
# A fatal error has been detected by the Java Runtime Environment:
Due to Tesseract's locale requirements, export LC_ALL=C is required
before running any client programs.
so:
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>1.6.0</version>
<executions>
<execution>
<goals>
<goal>exec</goal>
</goals>
</execution>
</executions>
<configuration>
<environmentVariables>
<LC_ALL>C</LC_ALL>
</environmentVariables>
<executable>java</executable>
<arguments>
<argument>-classpath</argument>
<classpath />
<argument>${classpath}</argument>
</arguments>
</configuration>
</plugin>
source:
- https://github.com/nguyenq/tess4j/issues/106
- https://github.com/sirfz/tesserocr/issues/165

Scala NoClassDefFoundError

I am new to Scala. I have this code:
import org.apache.hadoop.conf.Configuration
import org.apache.hadoop.fs.{Path, FileSystem}
/**
* Created by serban on 19/01/16.
*/
object TestHadoop {
def main(args: Array[String]):Unit = {
val namenodeEndpoint = "hdfs://123.45.123.45:8020"
val conf = new Configuration
conf.set("fs.defaultFS", namenodeEndpoint)
val fs = FileSystem.newInstance(conf)
val path = new Path("/user/ubuntu")
val fileStatus = fs.listFiles(path,false)
println("Hello world "+fileStatus.getClass)
while(fileStatus.hasNext())
{
println("FS: "+fileStatus.next())
}
}
}
When I run it from Maven, it runs OK. But I moved the compiled class to another machine and ran it in the command line via scala TestHadoop. This is what I get:
java.lang.NoClassDefFoundError: org/apache/hadoop/conf/Configuration
at TestHadoop$.main(TestHadoop.scala:13)
at TestHadoop.main(TestHadoop.scala)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:606)
at scala.tools.nsc.util.ScalaClassLoader$$anonfun$run$1.apply(ScalaClassLoader.scala:78)
at scala.tools.nsc.util.ScalaClassLoader$class.asContext(ScalaClassLoader.scala:24)
at scala.tools.nsc.util.ScalaClassLoader$URLClassLoader.asContext(ScalaClassLoader.scala:88)
at scala.tools.nsc.util.ScalaClassLoader$class.run(ScalaClassLoader.scala:78)
at scala.tools.nsc.util.ScalaClassLoader$URLClassLoader.run(ScalaClassLoader.scala:101)
at scala.tools.nsc.ObjectRunner$.run(ObjectRunner.scala:33)
at scala.tools.nsc.ObjectRunner$.runAndCatch(ObjectRunner.scala:40)
at scala.tools.nsc.MainGenericRunner.runTarget$1(MainGenericRunner.scala:56)
at scala.tools.nsc.MainGenericRunner.process(MainGenericRunner.scala:80)
at scala.tools.nsc.MainGenericRunner$.main(MainGenericRunner.scala:89)
at scala.tools.nsc.MainGenericRunner.main(MainGenericRunner.scala)
Caused by: java.lang.ClassNotFoundException: org.apache.hadoop.conf.Configuration
at java.net.URLClassLoader$1.run(URLClassLoader.java:366)
at java.net.URLClassLoader$1.run(URLClassLoader.java:355)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:354)
at scala.tools.nsc.util.ScalaClassLoader$URLClassLoader.scala$tools$nsc$util$ScalaClassLoader$$super$findClass(ScalaClassLoader.scala:88)
at scala.tools.nsc.util.ScalaClassLoader$class.findClass(ScalaClassLoader.scala:44)
at scala.tools.nsc.util.ScalaClassLoader$URLClassLoader.findClass(ScalaClassLoader.scala:88)
at java.lang.ClassLoader.loadClass(ClassLoader.java:425)
at scala.tools.nsc.util.ScalaClassLoader$URLClassLoader.scala$tools$nsc$util$ScalaClassLoader$$super$loadClass(ScalaClassLoader.scala:88)
at scala.tools.nsc.util.ScalaClassLoader$class.loadClass(ScalaClassLoader.scala:50)
at scala.tools.nsc.util.ScalaClassLoader$URLClassLoader.loadClass(ScalaClassLoader.scala:88)
at java.lang.ClassLoader.loadClass(ClassLoader.java:358)
It has a problem with my first import. When I run it in Maven, Maven knows how to solve this dependency.
Question: What can I do to run this example on my machine from the command line?
Thanks.
Regards,
Serban
You have a NoClassDefFoundError error because you are simply invoking your class without passing to Scala the required external libraries. Maven has already any defined dependencies as part of its classpath, hence external libraries are detected at runtime.
You need to use the -cp option of Scala and pass to it the required jar files (the external libraries), although this approach may be error prone and not maintainable for large set of dependencies.
Alternatively, you could add to your pom the following:
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>2.2</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
<configuration>
<finalName>${artifactId}-${version}-with-dependencies</finalName>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
Note: if you already have plugins configured, just add the plugin element of the Maven Shade Plugin.
This configuration will create a far jar using your artifactId and version and adding as a suffix the -with-dependencies token. Just run:
mvn package
And you will find the new jar in the target folder.
You can then use this jar (and not just the compiled file) in another machine and run it as following:
scala -cp yourproject-yourversion-with-dependencies.jar TestHadoop

Categories