Ant / External libraries - java

When I'm using external libraries (lucene) and running my java application through eclipse (run application) all works fine with the libs in the classpath.
But when I'm using Ant, i got this error here:
java.lang.ClassNotFoundException: org.apache.lucene.store.Directory
I guess this error shows up, cause of an incorrect classpath. Stange that no compilation error occurs, when ant compiles the code. This is my ant file:
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<project basedir="." default="build">
<path id="classpath">
<pathelement location="bin"/>
<pathelement location="GUI_P/lucene-6.2.0/lucene-6.2.0/analysis/common/lucene-analyzers-common-6.2.0.jar"/>
<pathelement location="GUI_P/lucene-6.2.0/lucene-6.2.0/core/lucene-core-6.2.0.jar"/>
<pathelement location="GUI_P/lucene-6.2.0/lucene-6.2.0/grouping/lucene-grouping-6.2.0.jar"/>
<pathelement location="GUI_P/lucene-6.2.0/lucene-6.2.0/queryparser/lucene-queryparser-6.2.0.jar"/>
<pathelement location="GUI_P/lucene-6.2.0/lucene-6.2.0/queries/lucene-queries-6.2.0.jar"/>
</path>
<target name="init">
<mkdir dir="bin"/>
<copy includeemptydirs="false" todir="bin">
<fileset dir="src">
<exclude name="**/*.launch"/>
<exclude name="**/*.java"/>
</fileset>
</copy>
</target>
<target depends="init" name="build">
<javac debug="true" destdir="bin" includeantruntime="true">
<src path="src"/>
<classpath refid="classpath"/>
</javac>
</target>
<target name="jar" depends="build">
<mkdir dir="GUI_P"/>
<jar destfile="GUI_P/GUI_P.jar" basedir="bin">
<manifest>
<attribute name="Main-Class" value="gui.Gui"/>
</manifest>
</jar>
</target>
<target name="copySamples" depends="jar">
<copy todir="GUI_P/samples">
<fileset dir="src/gui/samples"/>
</copy>
</target>
</project>
Can you please help me here out?

It's not that strange, because ClassNotFoundException arises always when trying a dynamic instantiation (through Class.forName()). So it is possible that the same classpath produces a right compilation, but it is not complete for an execution: It lacks the dynamic dependencies.
In your case, you must add to the execution classpath the lucene-core library (at least).

Related

Jar file cannot find SWT library, but Eclipse can

I have an Ant build (see build.xml below) that compiles and runs as I expect within Eclipse.
When I run the build via the command-line (java -jar swtgui.jar), however, I receive the following error:
Error: A JNI error has occurred, please check your installation and try again
Exception in thread "main" java.lang.NoClassDefFoundError: org/eclipse/swt/widgets/Layout
at java.lang.Class.getDeclaredMethods0(Native Method)
...
etc
build.xml
<?xml version="1.0" encoding="UTF-8"?>
<project default="run" name="My Project ">
<target name="run" depends="createjar">
<java classname="com.company.program.project.MyMainClass">
<classpath path="staging">
<fileset dir="C:\COMPANY\Eclipse\3.6-64\plugins">
<include name="org.eclipse.swt.*.jar" />
</fileset>
</classpath>
</java>
</target>
<target name="createjar" depends="compile">
<jar destfile="./builds/jars/swtgui.jar" basedir="staging" filesetmanifest="mergewithoutmain">
<manifest>
<attribute name="Main-Class" value="com.company.program.project.MyMainClass" />
</manifest>
<fileset dir="C:\COMPANY\Eclipse\3.6-64\plugins\" includes="org.eclipse.swt.win32.win32.x86_64_3.6.0.v3650b.jar" />
</jar>
</target>
<target name="compile">
<javac includeantruntime="false" srcdir="./src" destdir="staging">
<classpath>
<fileset dir="C:\COMPANY\Eclipse\3.6-64\plugins">
<include name="org.eclipse.swt.*.jar" />
</fileset>
</classpath>
</javac>
</target>
<record name="./MyMainClass.log" loglevel="verbose" action="start"/>
The error doesn't make sense to me since I specify the same SWT library path for the compile target as I do for the createjar target.
If the SWT library is found for the compile target, why is it not found for the createjar target?

run target in ant that depends on jar

I have a jar target in ant that makes a jar out of two classes that I have. Then, I want to run this jar in another target that depends on the jar target. Would there be an easy way to do this in ant? I have my compile and jar targets pasted below.
<project>
<target name="compile">
<mkdir dir="build/classes"/>
<javac includeantruntime="false" srcdir="." destdir="build/classes"/>
</target>
<target name="jar">
<mkdir dir="build/jar"/>
<jar destfile="build/jar/KnightsTour.jar" basedir="build/classes">
<manifest>
<attribute name="Main-Class" value="PlayTour"/>
</manifest>
</jar>
</target>
</project
To run a Java application, use the java task (see https://ant.apache.org/manual/Tasks/java.html for its documentation). An example from the docs:
<java jar="dist/test.jar" fork="true" failonerror="true" maxmemory="128m">
<arg value="arg1"/>
<arg value="arg2"/>
<classpath>
<pathelement location="dist/test.jar"/>
<pathelement path="${java.class.path}"/>
</classpath>
</java>

Could not find .Main, error with Ant when I try to Run it

This is my first time using ant and one of my first times I program in java so I'm a little lost, I'm trying to compile and run a program with ant, it seems that it compiles right, but I have problems make it run, it says that it Couldn't find Tarea2.Main.
So this is the structure of my project file:
Tarea 2/
src/tarea/pkg2
build.xml
3rd party .jar
Inside src/tarea/pkg2 I have all my .java, and the .java that has the main method is called Tarea2.java.
And this is my build.xml:
<project name="Tarea2" default="run" basedir=".">
<property name="src" location="src"/>
<property name="build" location="build"/>
<target name="init">
<tstamp/>
<mkdir dir="${build}"/>
</target>
<path id="classpath">
<fileset dir=".">
<include name="**/*.jar"/>
</fileset>
</path>
<target name="compile" depends="init">
<javac srcdir="${src}" destdir="${build}" includeantruntime="false">
<classpath refid="classpath"/>
</javac>
</target>
<target name="run" depends="compile">
<java classname="Tarea2.Main" classpath="${build}">
<classpath refid="classpath"/>
</java>
</target>
</project>
So I write "ant" in cmd from windows and everything is ok, except in the "run" target, in which I got stuff like:
[java] Could not find Tarea2.Main. Make sure you have it in your classpath
[java] at org.apache.tools.ant.taskdefs.ExecuteJava.execute
...
Somebody can help me pls?.

import com.sun.xml.internal.fastinfoset.util.StringArray cannot be resolved at javac

I am trying to write ant script for my project but at javac i am facing this issue:
80: package com.sun.xml.internal.fastinfoset.util does not exist
[javac] import com.sun.xml.internal.fastinfoset.util.StringArray;
[javac] ^
JAVA_HOME is set to jdk1.6
ANT_HOME is set to apache-ant-1.8.4
build file is
<?xml version="1.0" ?>
<project name="project" default="war">
<property name="location" location="D:\Project"></property>
<property name="project-location" location="${location}\project"></property>
<path id="Web App Libraries.libraryclasspath">
<fileset dir="${project-location}/web/WEB-INF/lib"/>
</path>
<path id="EAR Libraries.libraryclasspath"/>
<path id="compile.classpath">
<pathelement location="${project-location}/web/WEB-INF/classes"/>
<path refid="Web App Libraries.libraryclasspath"/>
<path refid="EAR Libraries.libraryclasspath"/>
</path>
<path id="Server Library [JBoss v4.2] (unbound).libraryclasspath"/>
<target name="init" >
<mkdir dir="D:/JBOSSHOME/build/classes"/>
<mkdir dir="D:/JBOSSHOME/dist" />
</target>
<target name="compile" depends="init" >
<javac destdir="D:/JBOSSHOME/build/classes" debug="true" srcdir="${project-location}/src">
<classpath refid="compile.classpath"/>
</javac>
</target>
<target name="war" depends="compile">
<war destfile="D:/JBOSSHOME/project.war" webxml="${project-location}/web/WEB-INF/web.xml">
<fileset dir="${project-location}/web"/>
<lib dir="${project-location}/web/WEB-INF/lib"/>
<classes dir="D:/JBOSSHOME/build/classes"/>
</war>
</target>
any help or pointer would be much appreciated ty!
com.sun.internal packages are NOT recommended to use , because
they are not guaranteed to be available across environments
support to those classes are also not guaranteed

Ant script to generate Jar - Reference not found error

I have following ant script to generate the jar file
<project name="myProject" basedir="." default="jar">
<property name="src" value="Java Source"/>
<property name="output" value="bin"/>
<target name="compile" depends="create">
<javac destdir="bin">
<src path="${src}"/>
<classpath refid="myProject.classpath"/>
</javac>
</target>
<target name="jar" depends="compile">
<jar destfile="myProject.jar">
<fileset dir="bin"/>
</jar>
</target>
<target name="clean">
<delete dir="${output}"/>
</target>
<target name="create" depends="clean">
<mkdir dir="${output}"/>
</target>
When I run ant script i get following error
Reference myProject.classpath not found.
I am not sure how to solve this error. It requires path of .classpath file ?
I also tried with
refid="classpath"
and it didnt work.
Can anyone help please!
Thanks
You need to define first something like because right now MyProject.classpath is not defined:
<classpath>
<pathelement path="${classpath}"/>
</classpath>
assuming that your classpath has what you need.
If it does not, create another entry under classpath element that has references to jars or whatever you need, or you need to custom specify path:
<path id="MyProject.classpath">
<pathelement location="lib/"/>
<pathelement path="${classpath}/"/>
<pathelement path="${additional.path}"/>
</path>
http://ant.apache.org/manual/using.html#path

Categories