Not able to execute jar file. Getting NoClassDefFoundError - java

I have created a executable jar file using build.
<target name="deploy" depends="compile">
<jar jarfile="${deploy.home}/${app.name}.jar" basedir="${build.home}">
<manifest>
<attribute name="Main-Class" value="abc.xyz"/>
</manifest>
</jar>
Here the xyz call has the main method to execute the whole program..
public static void main(String[] args) {
File buildFile = new File("build.xml");
Project p = new Project();
p.setUserProperty("ant.file", buildFile.getAbsolutePath());
p.init();
ProjectHelper helper = ProjectHelper.getProjectHelper();
p.addReference("ant.projectHelper", helper);
helper.parse(p, buildFile);
p.executeTarget(p.getDefaultTarget());
}
The jar file is being created successfuly. When I execute the like java -jar appname.jar, it is throwing exception --
Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/tools/ant/Project
at abc.xyz.main(xyz.java:20)
Caused by: java.lang.ClassNotFoundException: org.apache.tools.ant.Project
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)
The project class not found. Its there in ant.jar.
But when i execute the abc.java class simply by on command prompt java abc its working fine.
Pleased help me to get the cause? and its solution.

Add required libraries to the class path of the jar
<target name="deploy" depends="compile">
<jar jarfile="${deploy.home}/${app.name}.jar" basedir="${build.home}">
<manifest>
<attribute name="Main-Class" value="abc.xyz"/>
<attribute name="Class-Path" value="ant.jar"/>
</manifest>
</jar>
If you need more than one jar you have to separate them by spaces.

Related

Getting java.lang.NoClassDefFoundError: org/openqa/selenium/WebDriver while building selenium test by Ant

When I am trying to run my test case as Java Application, it executes successfully. But when I am running it through Ant, I am getting error that WebDriver class definition is not found. However, I have included the respective jar.
Please let me know where I am going wrong.
Below java file of test case resides in C:\Automation_Practice\DEPAutomation\src\workflows
package workflows;
import java.io.IOException;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.interactions.Actions;
public class UploadToken {
public static void main(String[] args) throws InterruptedException, IOException {
// Variable Declaration
WebDriver driver = new FirefoxDriver();
Actions action = new Actions(driver);
String baseUrl = "https://staging.maas360.com/emc/";
//String expectedTitle = "MaaS360";
//String actualTitle = "";
String uname = "3043275_sagrawal";
String password = "admin#123";
//Open Home Page
driver.manage().window().maximize();
driver.get(baseUrl);
//Login
driver.findElement(By.id("EMail")).sendKeys(uname);
driver.findElement(By.id("Password")).sendKeys(password);
driver.findElement(By.id("submit")).click();
//Go to Enrollments Page
Thread.sleep(5000);
WebElement deviceMenu = driver.findElement(By.xpath(".//*[#id='203']"));
WebElement enrollments = driver.findElement( By.xpath(".//[#id='766']"));
action.moveToElement(deviceMenu).perform();
Thread.sleep(5000);
enrollments.click();
//Go to Streamlined Enrollment page
Thread.sleep(10000);
driver.switchTo().frame(driver.findElement(By.id("Content")));
WebElement streamlineButton = driver.findElement(By.id("streamlinedEnrollButton"));
streamlineButton.click();
//Upload Token
Thread.sleep(10000);
driver.findElement(By.id("tokenNameInput")).sendKeys("Token 1");
WebElement browseButton = driver.findElement(By.xpath("//input[#id='fileInputTokenFile']"));
browseButton.click();
Thread.sleep(10000);
Runtime.getRuntime().exec("C:/Users/akhandelwal/Desktop/akash.exe");
Thread.sleep(5000);
driver.findElement(By.id("confirmFormSubmit")).click();
//Authenticate token upload
Thread.sleep(5000);
driver.findElement(By.id("passwordtext")).sendKeys(password);
driver.findElement(By.id("confirmPasswordFormSubmit")).click();
Thread.sleep(10000);
//Close Browser
driver.quit();
}
}
All the jars of selenium resides in C:\Automation_Practice\DEPAutomation\lib
Here is my build.xml file
<?xml version="1.0" encoding="UTF-8"?>
<project name="DEP" default="exec" basedir=".">
<!-- set global properties for this build -->
<property name="src" location="./src"/>
<property name="build.dir" location="${basedir}/build"/>
<property name="external.jars" value=".\lib"/>
<property name="TestOperation.dir" value="${external.jars}/TestOperation" />
<property name="test.dir" value="${src}/workflows"/>
<target name="setClasspath">
<path id="classpath_jars">
<pathelement path="${basedir}/"/>
<fileset dir="${external.jars}">
<include name="**/*.jar"/>
</fileset>
</path>
<pathconvert pathsep=";" property="test.classpath" refid="classpath_jars" />
</target>
<target name="clean">
<echo message="----------Delete the binary folder and create it again----------" />
<delete dir="${build.dir}"/>
<mkdir dir="${build.dir}"/>
</target>
<target name="compile" depends="clean,setClasspath">
<echo message="----------Compile the source files to ${build.dir}----------" />
<echo message="classpath:${test.classpath}"/>
<javac srcdir="${test.dir}" destdir="${build.dir}" debug="on" includeantruntime="false">
<classpath refid="classpath_jars"/>
</javac>
</target>
<target name="exec" depends="compile">
<jar destfile="${TestOperation.dir}/TestOperation.jar" basedir="${build.dir}">
<manifest >
<attribute name="Main-Class" value="workflows.UploadToken" />
</manifest>
</jar>
<java fork="true" jar="${TestOperation.dir}/TestOperation.jar" />
</target>
</project>
On running the Ant Build I am getting following error message:
Buildfile: C:\Automation_Practice\DEPAutomation\build.xml
clean:
[echo] ----------Delete the binary folder and create it again----------
[delete] Deleting directory C:\Automation_Practice\DEPAutomation\build
[mkdir] Created dir: C:\Automation_Practice\DEPAutomation\build
setClasspath:
compile:
[echo] ----------Compile the source files to C:\Automation_Practice\DEPAutomation\build----------
[echo] classpath:C:\Automation_Practice\DEPAutomation;C:\Automation_Practice\DEPAutomation\lib\TestOperation\TestOperation.jar;C:\Automation_Practice\DEPAutomation\lib\apache-mime4j-0.6.jar;C:\Automation_Practice\DEPAutomation\lib\bsh-1.3.0.jar;C:\Automation_Practice\DEPAutomation\lib\cglib-nodep-2.1_3.jar;C:\Automation_Practice\DEPAutomation\lib\commons-codec-1.10.jar;C:\Automation_Practice\DEPAutomation\lib\commons-collections-3.2.1.jar;C:\Automation_Practice\DEPAutomation\lib\commons-exec-1.3.jar;C:\Automation_Practice\DEPAutomation\lib\commons-io-2.4.jar;C:\Automation_Practice\DEPAutomation\lib\commons-lang3-3.4.jar;C:\Automation_Practice\DEPAutomation\lib\commons-logging-1.1.3.jar;C:\Automation_Practice\DEPAutomation\lib\cssparser-0.9.16.jar;C:\Automation_Practice\DEPAutomation\lib\gson-2.3.1.jar;C:\Automation_Practice\DEPAutomation\lib\guava-18.0.jar;C:\Automation_Practice\DEPAutomation\lib\hamcrest-core-1.3.jar;C:\Automation_Practice\DEPAutomation\lib\hamcrest-library-1.3.jar;C:\Automation_Practice\DEPAutomation\lib\htmlunit-2.17.jar;C:\Automation_Practice\DEPAutomation\lib\htmlunit-core-js-2.17.jar;C:\Automation_Practice\DEPAutomation\lib\httpclient-4.4.1.jar;C:\Automation_Practice\DEPAutomation\lib\httpcore-4.4.1.jar;C:\Automation_Practice\DEPAutomation\lib\httpmime-4.4.1.jar;C:\Automation_Practice\DEPAutomation\lib\jcommander-1.29.jar;C:\Automation_Practice\DEPAutomation\lib\jetty-io-9.2.11.v20150529.jar;C:\Automation_Practice\DEPAutomation\lib\jetty-util-9.2.11.v20150529.jar;C:\Automation_Practice\DEPAutomation\lib\jna-4.1.0.jar;C:\Automation_Practice\DEPAutomation\lib\jna-platform-4.1.0.jar;C:\Automation_Practice\DEPAutomation\lib\junit-dep-4.11.jar;C:\Automation_Practice\DEPAutomation\lib\nekohtml-1.9.22.jar;C:\Automation_Practice\DEPAutomation\lib\netty-3.5.7.Final.jar;C:\Automation_Practice\DEPAutomation\lib\phantomjsdriver-1.2.1.jar;C:\Automation_Practice\DEPAutomation\lib\sac-1.3.jar;C:\Automation_Practice\DEPAutomation\lib\selenium-java-2.46.0-srcs.jar;C:\Automation_Practice\DEPAutomation\lib\selenium-java-2.46.0.jar;C:\Automation_Practice\DEPAutomation\lib\serializer-2.7.2.jar;C:\Automation_Practice\DEPAutomation\lib\testng-6.8.5.jar;C:\Automation_Practice\DEPAutomation\lib\websocket-api-9.2.11.v20150529.jar;C:\Automation_Practice\DEPAutomation\lib\websocket-client-9.2.11.v20150529.jar;C:\Automation_Practice\DEPAutomation\lib\websocket-common-9.2.11.v20150529.jar;C:\Automation_Practice\DEPAutomation\lib\xalan-2.7.2.jar;C:\Automation_Practice\DEPAutomation\lib\xercesImpl-2.11.0.jar;C:\Automation_Practice\DEPAutomation\lib\xml-apis-1.4.01.jar
[javac] Compiling 3 source files to C:\Automation_Practice\DEPAutomation\build
exec:
[jar] Building jar: C:\Automation_Practice\DEPAutomation\lib\TestOperation\TestOperation.jar
[java] java.lang.NoClassDefFoundError: org/openqa/selenium/WebDriver
[java] at java.lang.Class.getDeclaredMethods0(Native Method)
[java] at java.lang.Class.privateGetDeclaredMethods(Unknown Source)
[java] at java.lang.Class.privateGetMethodRecursive(Unknown Source)
[java] at java.lang.Class.getMethod0(Unknown Source)
[java] at java.lang.Class.getMethod(Unknown Source)
[java] at sun.launcher.LauncherHelper.validateMainClass(Unknown Source)
[java] at sun.launcher.LauncherHelper.checkAndLoadMain(Unknown Source)
[java] Caused by: java.lang.ClassNotFoundException: org.openqa.selenium.WebDriver
[java] at java.net.URLClassLoader.findClass(Unknown Source)
[java] at java.lang.ClassLoader.loadClass(Unknown Source)
[java] at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
[java] at java.lang.ClassLoader.loadClass(Unknown Source)
[java] ... 7 more
[java] Error: A JNI error has occurred, please check your installation and try again
[java] Exception in thread "main"
[java] Java Result: 1
BUILD SUCCESSFUL
Total time: 2 seconds

How to import properly a jar dependency into a .jar file generated by Ant? [duplicate]

This question already has an answer here:
How to include a .jar dependency into the ANT target that generate my final .jar file of my application?
(1 answer)
Closed 8 years ago.
I am pretty new in Ant and I have the following problem trying to create a build.xml file to compile a single class (that contains the main() method) command line application.
So this is the code of the Main class (at this time it is the only class in the application):
import java.sql.*;
import java.util.TimeZone;
public class Main {
public static void main(String[] args) {
System.out.println("Hello World !!!");
System.out.println(args.length);
if(args.length != 0) {
String partitaIVA = args[0];
String nomePDF = args[1];
}
Connection conn = null;
Statement stmt = null;
try {
Class.forName ("oracle.jdbc.OracleDriver");
TimeZone timeZone = TimeZone.getTimeZone("Etc/GMT+2");
TimeZone.setDefault(timeZone);
// Step 1: Allocate a database "Connection" object
conn = DriverManager.getConnection("jdbc:oracle:thin:#XXX.XXX.XXX.XXX:1521:eme1", "myUserName", "myPswd"); // Oracle DB driver
System.out.println("After obtained connection with DB");
} catch(SQLException ex) {
ex.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
}
}
}
As you can see the behavior of the main() method is very simple, at this time only obtain a connection with an Oracle database (represented by the Connection conn object). Running it into the IDE (I am using IntelliJ) it works fine (I can see it using the debugger, the **Connection conn is correctly set).
Now I am working on the following build.xml file for the Ant compilation:
<project name="edi-sta">
<description>
EDI-STA
</description>
<target name="clean">
<delete dir="build"/>
</target>
<target name="compile">
<mkdir dir="build/classes"/>
<javac srcdir="src" destdir="build/classes"/>
</target>
<target name="jar">
<mkdir dir="build/jar"/>
<jar destfile="build/jar/Main.jar" basedir="build/classes">
<manifest>
<attribute name="Main-Class" value="Main"/>
</manifest>
</jar>
</target>
<target name="run">
<java jar="build/jar/Main.jar" fork="true"/>
</target>
</project>
After that I have performed in order the clean, compile and jar targets I tried to open the console, access to the build/jar/ directory that contains the Main.jar file and I try to execute it performing the following statement:
C:\Projects\edi-sta\build\jar>java -jar Main.jar
Hello World !!!
0
java.lang.ClassNotFoundException: oracle.jdbc.OracleDriver
at java.net.URLClassLoader$1.run(Unknown Source)
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)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Unknown Source)
at Main.main(Unknown Source)
C:\Projects\edi-sta\build\jar>
But, as you can see, now happens a very strange thing, it seems that can't found the class that contains the Oracle driver (oracle.jdbc.OracleDriver) so the ClassNotFoundException is thrown.
I think that this happens because if I open (with WinZip) my generated Main.jar file it only contains the Main.class file and the META-INF folder (that contains only the MANIFEST.MF file) but I have not the ojdbc6.jar file that contains the Oracle driver that I use.
So my question is: what have I to do to include this ojdbc6.jar dependency properly in my generated Main.jar file and avoid the ClassNotFoundException?
Tnx
You can define jar dependencies in the Class-Path attribute of the manifest file of the jar. Read the documentation here - http://docs.oracle.com/javase/tutorial/deployment/jar/downman.html
To achieve that from your ant task, use the Class-Path attribute as below
<jar destfile="build/jar/Main.jar" basedir="build/classes">
<manifest>
<attribute name="Main-Class" value="Main"/>
<attribute name="Class-Path" value="your-jar-file"/>
....

Java program running on build but not in a fat jar

I have a java application that uses the Jasper Reports API to generate a PDF. It builds fine but I had to add in some fonts via Netbeans/iReport plugin in order for them to show up when exporting the PDF. On clean and build it's fine and everything works.
But when I build a fat jar (via a few package-for-store lines in the build.xml) the reports won't run. Running via the Terminal, I get this error (sorry for wall of text):
Exception in thread "AWT-EventQueue-0" java.lang.ExceptionInInitializerError
at net.sf.jasperreports.engine.fill.JRBaseFiller.<init>(JRBaseFiller.java:124)
at net.sf.jasperreports.engine.fill.JRVerticalFiller.<init>(JRVerticalFiller.java:89)
at net.sf.jasperreports.engine.fill.JRVerticalFiller.<init>(JRVerticalFiller.java:104)
at net.sf.jasperreports.engine.fill.JRVerticalFiller.<init>(JRVerticalFiller.java:62)
at net.sf.jasperreports.engine.fill.JRFiller.createFiller(JRFiller.java:179)
at net.sf.jasperreports.engine.fill.JRFiller.fill(JRFiller.java:108)
at net.sf.jasperreports.engine.JasperFillManager.fill(JasperFillManager.java:668)
at net.sf.jasperreports.engine.JasperFillManager.fill(JasperFillManager.java:649)
at net.sf.jasperreports.engine.JasperFillManager.fillReport(JasperFillManager.java:971)
at ie.orderofmalta.OMFinancialsForm.yTDReportBtnActionPerformed(OMFinancialsForm.java:283)
at ie.orderofmalta.OMFinancialsForm.access$700(OMFinancialsForm.java:30)
at ie.orderofmalta.OMFinancialsForm$8.actionPerformed(OMFinancialsForm.java:163)
at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2018)
at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2341)
at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:402)
at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:259)
at javax.swing.AbstractButton.doClick(AbstractButton.java:376)
at javax.swing.plaf.basic.BasicMenuItemUI.doClick(BasicMenuItemUI.java:833)
at javax.swing.plaf.basic.BasicMenuItemUI$Handler.mouseReleased(BasicMenuItemUI.java:877)
at java.awt.Component.processMouseEvent(Component.java:6505)
at javax.swing.JComponent.processMouseEvent(JComponent.java:3320)
at java.awt.Component.processEvent(Component.java:6270)
at java.awt.Container.processEvent(Container.java:2229)
at java.awt.Component.dispatchEventImpl(Component.java:4861)
at java.awt.Container.dispatchEventImpl(Container.java:2287)
at java.awt.Component.dispatchEvent(Component.java:4687)
at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4832)
at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4492)
at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4422)
at java.awt.Container.dispatchEventImpl(Container.java:2273)
at java.awt.Window.dispatchEventImpl(Window.java:2719)
at java.awt.Component.dispatchEvent(Component.java:4687)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:735)
at java.awt.EventQueue.access$200(EventQueue.java:103)
at java.awt.EventQueue$3.run(EventQueue.java:694)
at java.awt.EventQueue$3.run(EventQueue.java:692)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:87)
at java.awt.EventQueue$4.run(EventQueue.java:708)
at java.awt.EventQueue$4.run(EventQueue.java:706)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:705)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:242)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:161)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:150)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:146)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:138)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:91)
Caused by: net.sf.jasperreports.engine.JRRuntimeException: net.sf.jasperreports.engine.JRException: Input stream not found at : fonts/fontsfamily1406301744789.xml
at net.sf.jasperreports.engine.fonts.SimpleFontExtensionHelper.loadFontFamilies(SimpleFontExtensionHelper.java:188)
at net.sf.jasperreports.engine.fonts.FontExtensionsRegistry.getExtensions(FontExtensionsRegistry.java:56)
at net.sf.jasperreports.extensions.DefaultExtensionsRegistry.getExtensions(DefaultExtensionsRegistry.java:110)
at net.sf.jasperreports.engine.util.JRStyledTextParser.<clinit>(JRStyledTextParser.java:83)
... 50 more
Caused by: net.sf.jasperreports.engine.JRException: Input stream not found at : fonts/fontsfamily1406301744789.xml
at net.sf.jasperreports.repo.RepositoryUtil.getInputStreamFromLocation(RepositoryUtil.java:159)
at net.sf.jasperreports.engine.fonts.SimpleFontExtensionHelper.loadFontFamilies(SimpleFontExtensionHelper.java:183)
... 53 more
When I added in the fonts, I added them to a jar which I then added to the class path. By the "input stream not found" error I'm guessing this is something to do with those fonts. But everything works fine in the normal clean-and-build process but I can't understand why the fat jar won't work.
Any ideas?
EDIT: Very similar to this Font Extension for barcode in report
I was stuck with this same problem for two days and I could not find anything to help me. I hope this helps anyone facing the same problem.
Here is how I solved the problem.
First of all, the tools being used by me
Jaspersoft Studio to create my reports 6.0.3
Jasperreport Library 6.0.3
Arial Unicode MS, font being embedded
Netbeans 8.0.2
So, first I modified the file jasperreports_extension.properties inside of jasperreports-6.0.3.jar and included the following lines
net.sf.jasperreports.extension.registry.factory.simple.font.families=net.sf.jasperreports.engine.fonts.SimpleFontExtensionsRegistryFactory
net.sf.jasperreports.extension.simple.font.families.dejavu=net/sf/jasperreports/fonts/fonts.xml
For some reason when I was building my jar file there was a conflict between the file jasperreports_extension.properties from jasperreports-6.0.3.jar and the one inside jasperreports-fonts-6.0.3.jar and the last one would be overridden.
Now to include my font (Arial Unicode MS) I edited the file /net/sf/jasperreports/fonts/fonts.xml and inserted the following code:
<fontFamily name="Arial Unicode MS">
<normal>net/sf/jasperreports/fonts/ArialUnicodeMS/ARIALUNI.TTF</normal>
<pdfEncoding>Identity-H</pdfEncoding>
<pdfEmbedded>true</pdfEmbedded>
<exportFonts>
</exportFonts>
</fontFamily>
After this I included my font inside of jasperreports-fonts-6.0.3.jar.
Now this is very important, when you are creating your jar file make sure to extract jasperreports-fonts-6.0.3.jar before jasperreports-6.0.3.jar.
Here is my build.xml
<target name="-post-jar">
<jar jarfile="dist2/Reports.jar">
<zipfileset src="${dist.jar}" excludes="META-INF/*;*.jar;*.txt;*.html" />
<zipfileset src="lib/jetty-all-9.0.3.v20130506.jar" excludes="META-INF/*;*.txt;*.html" />
<zipfileset src="lib/lib/jasperreports-javaflow-6.0.3.jar" excludes="META-INF/*;*.txt;*.html" />
<zipfileset src="lib/jasperreports-fonts-6.0.3.jar" excludes="META-INF/*;*.txt;*.html" />
<zipfileset src="lib/jasperreports-6.0.3.jar" excludes="META-INF/*;*.txt;*.html" />
<zipfileset src="lib/mysql-connector-java-5.1.34-bin.jar" excludes="META-INF/*;*.txt;*.html" />
<zipfileset src="lib/commons-javaflow-20060411.jar" excludes="META-INF/*;*.txt;*.html" />
<zipfileset src="lib/servlet-api-2.4.jar" excludes="META-INF/*;*.txt;*.html" />
<zipfileset src="lib/javax.servlet-3.0.jar" excludes="META-INF/*;*.txt;*.html" />
<zipfileset src="lib/commons-logging-1.1.1.jar" excludes="META-INF/*;*.txt;*.html" />
<zipfileset src="lib/commons-collections-3.2.1.jar" excludes="META-INF/*;*.txt;*.html" />
<zipfileset src="lib/ant-1.7.1.jar" excludes="META-INF/*" />
<zipfileset src="lib/groovy-all-2.0.1.jar" excludes="META-INF/*" />
<zipfileset src="lib/commons-digester-2.1.jar" excludes="META-INF/*" />
<zipfileset src="lib/itextpdf-5.5.0.jar" excludes="META-INF/*" />
<zipfileset src="lib/itext-pdfa-5.5.0.jar" excludes="META-INF/*" />
<zipfileset src="lib/apache-commons-lang.jar" excludes="META-INF/*" />
<zipfileset src="lib/com.lowagie.text-2.1.7.jar" excludes="META-INF/*" />
<zipfileset src="lib/commons-beanutils-1.9.0.jar" excludes="META-INF/*" />
<manifest>
<attribute name="Main-Class" value="Jetty"/>
</manifest>
</jar>
</target>
What solved it for me was to append the contents of the jasperreports_extension.properties file contained in the jasperreports-6.x.x.jar to my custom jasperreports_extension.properties.
The Problem was that the properties file from the jar got overwritten when creating the fat jar. Obviously JasperReports merges both files at runtime. Since the original content is missing, it causes this slightly misleading error.
Caused by: net.sf.jasperreports.engine.JRException: Input stream not found at : fonts/fontsfamily1406301744789.xml at
net.sf.jasperreports.repo.RepositoryUtil.getInputStreamFromLocation(RepositoryUtil.java:159) at
net.sf.jasperreports.engine.fonts.SimpleFontExtensionHelper.loadFontFamilies(SimpleFontExtensionHelper.java:183)
... 53 more
You need to provide a proper input stream to whatever you want to do here. If the resource is inside a jar file you must write special code to handle this. FileInputStreams will not work.
#eoinDub:
I had a similar problem when loading the report itselfes from the class directory of the fat jar. So I used "...getResourceAsStream()".
I did something like this (see code below). [Actually I used a compiled *.jasper - I don't remember whether an uncompiled *.jrxml did work, too.]
JasperReport report = null;
InputStream is = MyClass.class.getClassLoader().getResourceAsStream(reportName);
if (FilenameUtils.isExtension(reportName, "jrxml")) {
JasperDesign jd = JRXmlLoader.load(is);
report = JasperCompileManager.compileReport(jd);
} else {
report = (JasperReport) JRLoader.loadObject(is);
}
JRCsvDataSource ds;
try {
ds = new JRCsvDataSource(csvName, csvEncoding);
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
return;
}
ds.setUseFirstRowAsHeader(true);
ds.setFieldDelimiter(csvDelimiter.charAt(0));
JasperPrint jprint = JasperFillManager.fillReport(report, reportParams, ds);
try {
JasperExportManager.exportReportToPdfFile(jprint, outPDF);
} catch (Exception e) {
e.printStackTrace();
}
I was stuck with the same problem. In my project I am using Maven, and I noticed that at the end of generating the fat .jar, it threw a message of
jasperreports_extension.properties already added, skipping
Among what I saw on the web about this problem, I got that this file is repeated in several libraries, such as: jasperreports and jasperfonts; this causes conflicts between them and the Maven ignores them. So what I did was put my custom jasperreports_extension.properties file among the first Maven dependency files, so that this file setting takes it first.

Using external libraries in Java

Here's the error I keep getting at runtime:
[java] Exception in thread "main" java.lang.NoClassDefFoundError: org/lwjgl/LWJGLException
Note, this is a runtime error, not a compile-time one. Both tasks in my build.xml have an identical classpath set, and the compile task runs fine every single time:
<path id="classpath">
<fileset dir="lib" includes="*.jar" />
</path>
<target name="compile">
<mkdir dir="build/classes"/>
<javac
srcdir="src"
classpathref="classpath"
includeantruntime="false"
destdir="build/classes"
/>
</target>
...
<target name="run" depends="clean,compile,jar">
<java
jar="build/jar/${project.name}.jar"
fork="true"
classpathref="classpath"
>
<sysproperty key="java.library.path" path="${path.lib}/windows"/>
</java>
</target>
Trying to run the jar via command-line manually yields the same result:
java -cp .:lib/*.jar -Djava.library.path=lib/windows -jar build/jar/JUtopia.jar
Exception in thread "main" java.lang.NoClassDefFoundError: org/lwjgl/LWJGLException
Note that the library jarfile is ok:
bash-3.1$ jar -tf lib/lwjgl.jar | grep LWJGLException
org/lwjgl/LWJGLException.class
And the native libraries are in place:
bash-3.1$ ls lib/windows/lwjgl.dll
lib/windows/lwjgl.dll
The question: where the blazes have I gone wrong? I've been beating at this problem for nearly 3 days. Any help would be much appreciated.
Full result stack:
clean:
[delete] Deleting directory C:\Users\mkumpan\Projects\JUtopia\build
compile:
[mkdir] Created dir: C:\Users\mkumpan\Projects\JUtopia\build\classes
[javac] Compiling 12 source files to C:\Users\mkumpan\Projects\JUtopia\build\classes
jar:
[mkdir] Created dir: C:\Users\mkumpan\Projects\JUtopia\build\jar
[jar] Building jar: C:\Users\mkumpan\Projects\JUtopia\build\jar\JUtopia.jar
run:
[java] Exception in thread "main" java.lang.NoClassDefFoundError: org/lwjgl/LWJGLException
[java] at JUtopia.<init>(Unknown Source)
[java] at JUtopia.main(Unknown Source)
[java] Caused by: java.lang.ClassNotFoundException: org.lwjgl.LWJGLException
[java] at java.net.URLClassLoader$1.run(URLClassLoader.java:366)
[java] at java.net.URLClassLoader$1.run(URLClassLoader.java:355)
[java] at java.security.AccessController.doPrivileged(Native Method)
[java] at java.net.URLClassLoader.findClass(URLClassLoader.java:354)
[java] at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
[java] at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
[java] at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
[java] ... 2 more
P.S.: Note, I'm using Console2 with bash in a windows environment for my commandline work, thus the windows natives yet linux shell syntax. Using vanilla cmd to run the jar yields the same result.
-jar...
When you use this option, the JAR file is the source of all user classes, and other user class path settings are ignored. - reference
try setting the Class-Path in the JAR
Alternatively try running without the -Jar option, by specifying the main class on the command line
One of the possible causes is that while loading the class LWJGLException it also references another class which can't be found on the classpath. Hence the reported error is sometimes not clear.
Important here is thet you have this NoClassDefFoundError and not ClassNotFoundException which is the error you assume you are having: it cannot find the class LWHLException, yes it can ! But it cannot load it....

How do I add the j2ee.jar to a Java2WSDL ant script programmatically?

I am using IBM's Rational Application Developer. I have an ant script that contains the Java2WSDL task. When I run it via IBM, it gives compiler errors unless I include the j2ee.jar file in the classpath via the run tool (it does not pick up the jar files in the classpath in the script). However, I need to be able to call this script programmatically, and it is giving me this error: "java.lang.NoClassDefFoundError: org.eclipse.core.runtime.CoreException" I'm not sure which jars need to be added or where? Since a simple echo script runs, I assume that it is the j2ee.jar or another ant jar that needs to be added. I've added it to the project's buildpath, but that doesn't help. (I also have ant.jar, wsanttasks.jar, all the ant jars from the plugin, tools.jar, remoteAnt.jar, and the swt - all which are included in the buildpath when you run the script by itself.)
Script:
<?xml version="1.0" encoding="UTF-8"?>
<project default="build" basedir=".">
<path id="lib.path">
<fileset dir="C:\Program Files\IBM\WebSphere\AppServer\lib" includes="*.jar"/>
<!--
Adding these does not help.
<fileset dir="C:\Program Files\IBM\SDP70Shared\plugins\org.apache.ant_1.6.5\lib" includes="*.jar"/>
<fileset dir="C:\Program Files\IBM\SDP70\jdk\lib" includes="*.jar"/>
<fileset dir="C:\Program Files\IBM\SDP70\configuration\org.eclipse.osgi\bundles\1139\1\.cp\lib" includes="*.jar"/>
<fileset dir="C:\Program Files\IBM\SDP70Shared\plugins" includes="*.jar"/>
-->
</path>
<taskdef name="java2wsdl"
classname="com.ibm.websphere.ant.tasks.Java2WSDL">
<classpath refid="lib.path"/>
</taskdef>
<target name="build">
<echo message="Beginning build"/>
<javac srcdir="C:\J2W_Test\Java2Wsdl_Example"
destdir="C:\J2W_Test\Java2Wsdl_Example">
<classpath refid="lib.path"/>
<include name="WSExample.java"/>
</javac>
<echo message="Set up javac"/>
<echo message="Running java2wsdl"/>
<java2wsdl output="C:\J2W_Test\Java2Wsdl_Example\example\META-INF\wsdl\WSExample.wsdl"
classpath="C:\J2W_Test\Java2Wsdl_Example"
className= "example.WSExample"
namespace="http://example"
namespaceImpl="http://example"
location="http://localhost:9080/example/services/WSExample"
style="document"
use="literal">
<mapping namespace="http://example" package="example"/>
</java2wsdl>
<echo message="Complete"/>
</target>
</project>
Code:
File buildFile = new File("build.xml");
Project p = new Project();
p.setUserProperty("ant.file", buildFile.getAbsolutePath());
DefaultLogger consoleLogger = new DefaultLogger();
consoleLogger.setErrorPrintStream(System.err);
consoleLogger.setOutputPrintStream(System.out);
consoleLogger.setMessageOutputLevel(Project.MSG_INFO);
p.addBuildListener(consoleLogger);
try {
p.fireBuildStarted();
p.init();
ProjectHelper helper = ProjectHelper.getProjectHelper();
p.addReference("ant.projectHelper", helper);
helper.parse(p, buildFile);
p.executeTarget(p.getDefaultTarget());
p.fireBuildFinished(null);
} catch (BuildException e) {
p.fireBuildFinished(e);
}
Error:
[java2wsdl] java.lang.NoClassDefFoundError: org.eclipse.core.runtime.CoreException
[java2wsdl] at java.lang.J9VMInternals.verifyImpl(Native Method)
[java2wsdl] at java.lang.J9VMInternals.verify(J9VMInternals.java:68)
[java2wsdl] at java.lang.J9VMInternals.initialize(J9VMInternals.java:129)
[java2wsdl] at com.ibm.ws.webservices.multiprotocol.discovery.ServiceProviderManager.getDiscoveredServiceProviders(ServiceProviderManager.java:378)
[java2wsdl] at com.ibm.ws.webservices.multiprotocol.discovery.ServiceProviderManager.getAllServiceProviders(ServiceProviderManager.java:214)
[java2wsdl] at com.ibm.ws.webservices.wsdl.fromJava.Emitter.initPluggableBindings(Emitter.java:2704)
[java2wsdl] at com.ibm.ws.webservices.wsdl.fromJava.Emitter.<init>(Emitter.java:389)
[java2wsdl] at com.ibm.ws.webservices.tools.ant.Java2WSDL.execute(Java2WSDL.java:122)
[java2wsdl] at org.apache.tools.ant.UnknownElement.execute(UnknownElement.java:275)
[java2wsdl] at org.apache.tools.ant.Task.perform(Task.java:364)
[java2wsdl] at org.apache.tools.ant.Target.execute(Target.java:341)
[java2wsdl] at org.apache.tools.ant.Target.performTasks(Target.java:369)
[java2wsdl] at org.apache.tools.ant.Project.executeSortedTargets(Project.java:1216)
[java2wsdl] at org.apache.tools.ant.Project.executeTarget(Project.java:1185)
[java2wsdl] at att.ant.RunAnt.main(RunAnt.java:32)
A jarfinder search suggests that because you are running outside of the Rational environment, you need access to the Eclipse core jars (perhaps) where that file might be contained.

Categories