Spark Java error in standalone application - java

I was able to create an executable using launch4j and it works fine on my machine. When I send it to someone to run on their windows machine they get the following error:
Error: A JNI error has occurred, please check your installation and try again
Exception in thread "main" java.lang.NoClassDefFoundError: spark/TemplateEngine
at java.lang.Class.getDeclaredMethods0(Native Method)
at java.lang.Class.privateGetDeclaredMethods(Unknown Source)
at java.lang.Class.privateGetMethodRecursive(Unknown Source)
at java.lang.Class.getMethod0(Unknown Source)
at java.lang.Class.getMethod(Unknown Source)
at sun.launcher.LauncherHelper.validateMainClass(Unknown Source)
at sun.launcher.LauncherHelper.checkAndLoadMain(Unknown Source)
Caused by: java.lang.ClassNotFoundException: spark.TemplateEngine
at java.net.URLClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
... 7 more
Any thoughts?

This exception may also occur if your maven dependencies do not have <scope> set to compile time (default) and dependency jars are not available at compile time. For instance if maven dependencies have <scope>provided</scope> in your pom.xml compiler will assume that the JRE/environment will provide these dependency jars but when the sources are compiled and these dependencies are not found, this exception will be thrown.
For example - below may lead to this exception if spark-mllib_2.11 dependency is not found during compile time although they are added and there is no error during editing;
<dependency>
<groupId>org.apache.spark</groupId>
<artifactId>spark-mllib_2.11</artifactId>
<version>2.2.0</version>
<scope>provided</scope>
</dependency>

I had this problem because my version of java that was as default was 9, and somehow Spark didn't recognize it. So I changed to version 8 and it worked. To change in linux :
sudo update-java-alternatives -s java-1.8.0-openjdk-amd64
In your case you may want another version, so choose yours (to list the versions you have in your computer use the -l option).

Had to set relative paths in the class path so the executable could locate the jar files

Related

Exception in thread "main" java.lang.NoClassDefFoundError maven jar

i am new to maven, after running Maven with mvn clean package command.
it compiles the project into an executable jar.
i run the jar in the terminal and got this error:
$ java -jar create_pass_criteria-1.0-SNAPSHOT.jar
Exception in thread "main" java.lang.NoClassDefFoundError: com/mprv/automation/core/exceptions/AutomationException
at Create_pass_criteria.Main.main(Main.java:23)
Caused by: java.lang.ClassNotFoundException: com.mprv.automation.core.exceptions.AutomationException
at java.net.URLClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
... 1 more
i have a lot of jars that i use in my code by importing them.
how can i make the jar also include those jars?
is this what this error means?
appreciate any help,
thank you
Try it
Just add all the required libraries to the classpath when using java -jar target/PriseDeNotes-0.0.1-SNAPSHOT.jar with the --classpath argument.

org/apache/poi/openxml4j/exceptions/InvalidFormatException using JXLS

I am using JXLS Java library to generate excel reports. I am trying to run the sample getting started tutorial (Object collection output demo). As suggested in the tutorial I am using maven to specify the required libraries in my project build configuration file. Below is the java code used
List<Employee> employees = generateSampleEmployeeData();
try(InputStream is = ObjectCollectionDemo.class.getResourceAsStream("object_collection_template1.xls")) {
try(OutputStream os = new FileOutputStream("target/" + fileName)) {
Context context = new Context();
context.putVar("employees", employees);
JxlsHelper.getInstance().processTemplate(is, os, context);
}
}
When i run the program in my eclipse i get the below exception:
SLF4J: Failed to load class "org.slf4j.impl.StaticLoggerBinder".
SLF4J: Defaulting to no-operation (NOP) logger implementation
SLF4J: See http://www.slf4j.org/codes.html#StaticLoggerBinder for further details.
Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/poi/openxml4j/exceptions/InvalidFormatException
at java.lang.Class.getDeclaredMethods0(Native Method)
at java.lang.Class.privateGetDeclaredMethods(Unknown Source)
at java.lang.Class.privateGetMethodRecursive(Unknown Source)
at java.lang.Class.getMethod0(Unknown Source)
at java.lang.Class.getMethod(Unknown Source)
at org.jxls.util.TransformerFactory.createTransformer(TransformerFactory.java:34)
at org.jxls.util.JxlsHelper.createTransformer(JxlsHelper.java:217)
at org.jxls.util.JxlsHelper.processTemplate(JxlsHelper.java:104)
at com.ucas.ObjectCollectionDemo.main(ObjectCollectionDemo.java:42)
Caused by: java.lang.ClassNotFoundException: org.apache.poi.openxml4j.exceptions.InvalidFormatException
at java.net.URLClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
... 9 more
Below is a snapshot of the maven dependencies
Java Version: 1.8
Environment: Windows 7
IDE: Eclipse Neon
It looks like you have an issue with some XML dependencies required for Apache POI processing.
I can see them in your dependencies screenshot however for some reason they are not in effect when you are running your program. May be there is some conflict between the dependencies.
Try to create a minimal jxls project by just adding only the following two dependencies and see if it works
<dependency>
<groupId>org.jxls</groupId>
<artifactId>jxls</artifactId>
<version>2.4.0</version>
</dependency>
<dependency>
<groupId>org.jxls</groupId>
<artifactId>jxls-poi</artifactId>
<version>1.0.12</version>
</dependency>
Then you can add your own dependencies one by one and see at which point it breaks.
Not seeing very much in the way of code in your question, so I'll be shooting blanks here. It almost looks like you have a version mismatch with the file object_collection_template1.xls being created with a different version of Excel than JXLS is compatible with, at least as its written.

Serritor Problems

I´m trying to crawl a dynamic Webpage with Serritor but there is a Problem:
This is my Code:
public class MyCrawler extends BaseCrawler {
public MyCrawler() {
String pathToDriver = ".//ChromeDriver//chromedriver.exe";
System.setProperty("webdriver.chrome.driver", pathToDriver);
config.setWebDriver(new ChromeDriver());
config.setFilterOffsiteRequests(true);
config.addSeedAsString("http://yourspecificsite.com");
config.setCrawlingStrategy(CrawlingStrategy.DEPTH_FIRST);
config.setDelayBetweenRequests(Duration.ofSeconds(1));
}
}
And this the return:
Exception in thread "main" java.lang.NoClassDefFoundError: org/openqa/selenium/htmlunit/HtmlUnitDriver
at com.github.peterbencze.serritor.internal.CrawlerConfiguration.<init>(CrawlerConfiguration.java:47)
at com.github.peterbencze.serritor.api.BaseCrawler.<init>(BaseCrawler.java:68)
at serritor_versuch2.MyCrawler.<init>(MyCrawler.java:21)
at serritor_versuch2.run.main(run.java:5)
Caused by: java.lang.ClassNotFoundException: org.openqa.selenium.htmlunit.HtmlUnitDriver
at java.net.URLClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
... 4 more
Exception in thread "main" java.lang.NoClassDefFoundError: org/openqa/selenium/htmlunit/HtmlUnitDriver
Above exception clearly states that HtmlUnitDriver class is not found. If you are using maven, you should probably add maven dependency in your pom.xml file.
Not sure, which version are you using but here is the latest version.
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-htmlunit-driver</artifactId>
<version>2.52.0</version>
</dependency>
If you are using gradle or any other dependency management tool, look accordingly from: Maven Repository
If you are not using any dependency management tool, you should download the jar file and put it in your classpath.

Tess4j Issue in Windows: java.lang.UnsatisfiedLinkError: The specified module could not be found in instance.doOCR(imageFile)

After 2 days of googling I am still unable to find the solution of my issue with Tess4j version 3.0: java.lang.UnsatisfiedLinkError: The specified module could not be found.
I write server side Spring boot app on my Windows 10 x64. I used this tutorial http://tess4j.sourceforge.net/tutorial/
I make ant test in tess4j project's source and this command works ok in my PC. I also have Visual C++ Redistributable for VS2012 and Visual C++ Redistributable for VS2013 installed.
But I have missed dlls in my PC, libtesseract304.dll depends on:
Can it be the reason of problem? But how it is possible, that Tess4J-3.0-src project works ok in my PC?
My full stack trace:
java.lang.UnsatisfiedLinkError: The specified module could not be found.
at com.sun.jna.Native.open(Native Method) ~[jna.jar:4.2.1 (b0)]
at com.sun.jna.NativeLibrary.loadLibrary(NativeLibrary.java:263) ~[jna.jar:4.2.1 (b0)]
at com.sun.jna.NativeLibrary.getInstance(NativeLibrary.java:403) ~[jna.jar:4.2.1 (b0)]
at com.sun.jna.Library$Handler.<init>(Library.java:147) ~[jna.jar:4.2.1 (b0)]
at com.sun.jna.Native.loadLibrary(Native.java:502) ~[jna.jar:4.2.1 (b0)]
at com.sun.jna.Native.loadLibrary(Native.java:481) ~[jna.jar:4.2.1 (b0)]
at net.sourceforge.tess4j.util.LoadLibs.getTessAPIInstance(Unknown Source) ~[tess4j-3.0.jar:na]
at net.sourceforge.tess4j.TessAPI.<clinit>(Unknown Source) ~[tess4j-3.0.jar:na]
at net.sourceforge.tess4j.Tesseract.init(Unknown Source) ~[tess4j-3.0.jar:na]
at net.sourceforge.tess4j.Tesseract.doOCR(Unknown Source) ~[tess4j-3.0.jar:na]
at net.sourceforge.tess4j.Tesseract.doOCR(Unknown Source) ~[tess4j-3.0.jar:na]
at net.sourceforge.tess4j.Tesseract.doOCR(Unknown Source) ~[tess4j-3.0.jar:na]
at ocr.OCRController.handleFileUpload(OCRController.java:109) ~[classes/:na]
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_51]
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:1.8.0_51]
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.8.0_51]
at java.lang.reflect.Method.invoke(Method.java:497) ~[na:1.8.0_51]
My code:
ITesseract instance = new Tesseract(); // JNA Interface Mapping
instance.setDatapath(new File(datapath).getPath());
instance.setLanguage("eng");
try {
String result = instance.doOCR(imageFile); //error here
} catch (TesseractException e) {
System.err.println(e.getMessage());
}
maven:
<dependency>
<groupId>jai_imageio</groupId>
<artifactId>com.jai_imageio</artifactId>
<version>3.0</version>
<scope>system</scope>
<systemPath>${project.basedir}/lib/jai_imageio.jar</systemPath>
</dependency>
<dependency>
<groupId>commons-io-2.4</groupId>
<artifactId>com.commons-io-2.4</artifactId>
<version>3.0</version>
<scope>system</scope>
<systemPath>${project.basedir}/lib/commons-io-2.4.jar</systemPath>
</dependency>
<dependency>
<groupId>jna</groupId>
<artifactId>com.jna</artifactId>
<version>3.0</version>
<scope>system</scope>
<systemPath>${project.basedir}/lib/jna.jar</systemPath>
</dependency>
<dependency>
<groupId>tess4j-3.0</groupId>
<artifactId>com.tess4j-3.0</artifactId>
<version>3.0</version>
<scope>system</scope>
<systemPath>${project.basedir}/lib/tess4j-3.0.jar</systemPath>
</dependency>
I also tried to load libs in force way:
Runtime.getRuntime().loadLibrary("lib/win32-x86-64/gsdll64");
Runtime.getRuntime().loadLibrary("lib/win32-x86-64/libtesseract304");
But without success:
There was an unexpected error (type=Internal Server Error, status=500).
C:\Users\Iuliia\IdeaProjects\ENumbersBackend\lib\win32-x86-64\libtesseract304.dll: Can't find dependent libraries
Thank you for any help!
I think you our misled by the output of depends.exe.
The DLL only imports these other dlls:
dumpbin libtesseract304.dll /imports|find ".dll"
Dump of file libtesseract304.dll
WS2_32.dll
liblept171.dll
MSVCP120.dll
MSVCR120.dll
KERNEL32.dll
To doublecheck you can get the linker version used to compile that dll:
dumpbin libtesseract304.dll /headers | find "linker version"
12.00 linker version
So all you need is the Visual Studio 2013 Runtime (again: don't be misled: 12.0 is 2013, which can be rather confusing)
Presumably the liblept171.dll is the thing that is missing, so you should check where it is stored and why the one project is able to find it and not the other. A good idea is to copy all dependencies into a common path and setting java.library.path to that directory (just for testing purposes)
liblept171.dll is part of lept4j, there is a accordingly named .jar in your lib directory which contains that dll:
7z l lib\lept4j-1.0.1.jar | find ".dll"
2015-11-14 11:46:04 ..... 2406400 2406400 win32-x86-64\liblept171.dll
2015-11-14 11:46:04 ..... 1834496 1834496 win32-x86\liblept171.dll
In addition you should take care that the bitness of your JRE, the Visual Studio Runtime and Tesseract do match. If in doubt: install x86 and x64.
As a further troubleshooting aid you might want to find out where the dll is being searched for. Use procmon.exe with a filter for that dll.
The problem is not connected with Windows 10.
I've already fix the error with adding
<dependency>
<groupId>net.sourceforge.tess4j</groupId>
<artifactId>tess4j</artifactId>
<version>3.0.0</version>
</dependency>
instead all previous maven dependencies.

Why is my jar not running?

I can run my program outside of a jar file, but I want to deploy it and run it as a jar.
This command works for running the .class file:
java -classpath .;./libs/mail.jar;./libs/jcommon-1.0.16.jar;./libs/jfreechart-1.0.13.jar;./libs/jxl.jar;./libs/ojdbc5.jar gdsreports/ReportsDriver
This is how I package my jar file:
jar -cfvm GDSReports.jar GDSReports.mf gdsreports/* util/* libs/*
My manifest looks like this:
Manifest-Version: 1.0
Main-Class: gdsreports.ReportsDriver
Created-By: Me
This is how I run my jar:
java -classpath .;./libs/mail.jar;./libs/jcommon-1.0.16.jar;./libs/jfreechart-1.0.13.jar;./libs/jxl.jar;./libs/ojdbc5.jar;./util;./gdsreports -jar GDSReports.jar
However, run I run the jar file, it fails to load all the libraries. This is what it spits out:
Error loading configuration file: config/reportConfig
Error loading configuration file: config/gdsIds
Exception in thread "main" java.lang.NoClassDefFoundError: jxl/write/WriteException
at util.ReportConfig.setupReports(ReportConfig.java:197)
at util.ReportConfig.setup(ReportConfig.java:65)
at gdsreports.ReportsDriver.main(ReportsDriver.java:36)
Caused by: java.lang.ClassNotFoundException: jxl.write.WriteException
at java.net.URLClassLoader$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
... 3 more
This is the expected output:
Error loading configuration file: config/reportConfig
Error loading configuration file: config/gdsIds
-------------------------------------
Running TimingReport with parameters:
Filename: Reports/06-26-2012/TimingRptZL_06-26-2012.xls
Brand: ZL
Using production database: false
-------------------------------------
-------------------------------------
Exception in thread "main" java.lang.NullPointerException
at gdsreports.TimingReport.fillChartData(TimingReport.java:417)
at gdsreports.TimingReport.fillReport(TimingReport.java:238)
at gdsreports.TimingReport.run(TimingReport.java:131)
at gdsreports.ReportsDriver.main(ReportsDriver.java:40)
What am I doing wrong?
The -classpath argument (and the CLASSPATH environment variable) are ignored when you use the -jar argument.
If a jar file depends on other libraries, you have two choices:
Use the Class-Path header in your MANIFEST.MF file to call out the other jars, or
Unpack the other jars and include them in your jar.

Categories

Resources