Reputation: 742
I'm trying to create an Ant build script in order to build an executable JAR for a small program I wrote. In particular, this small program uses an external JAR selenium-server-standalone-2.35.0.jar
, which is stored in the lib
directory in my project's root directory, while the source java files ExampleScenario.java
and ExampleClass.java
are defined in src/com/selenium/example
.
So far, I have the following build.xml defined:
<?xml version="1.0" encoding="UTF-8"?>
<project name="SampleDemo" default="jar">
<property name="src.dir" value="src"/>
<property name="bin.dir" value="bin"/>
<property name="lib.dir" value="lib"/>
<property name="dist.dir" value="dist"/>
<property name="com.dir" value="com"/>
<property name="selenium.dir" value="${com.dir}/selenium"/>
<property name="example.dir" value="${selenium.dir}/example"/>
<target name="clean" description="Delete all generated files">
<delete dir="${bin.dir}" failonerror="false"/>
<delete dir="${dist.dir}" failonerror="false"/>
</target>
<target name="compile" description="">
<mkdir dir="${bin.dir}/${example.dir}"/>
<javac srcdir="${src.dir}/${example.dir}" destdir="${bin.dir}/${example.dir}" classpath="${lib.dir}/selenium-server-standalone-2.35.0.jar"/>
</target>
<target name="jar" description="JARS the Task" depends="compile">
<mkdir dir="${dist.dir}"/>
<jar destfile="${dist.dir}/${ant.project.name}.jar" basedir="${bin.dir}/${example.dir}">
<manifest>
<attribute name="Main-Class" value="ExampleScenario"/>
<attribute name="Class-Path" value="${lib.dir}/selenium-server-standalone-2.35.0.jar; "/>
</manifest>
</jar>
</target>
The main method is located within the ExampleScenario class. When I try to run the output JAR file, however, I get the following error:
Error: Could not find or load main class ExampleScenario
As a beginner to ANT scripting, I've read over the ANT documentation and have followed some tutorials online. I've also tried looking online for answers regarding this particular issue, but I haven't been able to figure out what is causing this issue. Any help would be greatly appreciated.
EDIT: Issue has been solved, but my external JAR appears not to be recognized:
ERROR LOG:
Exception in thread "main" java.lang.NoClassDefFoundError: org/openqa/selenium/WebDriver
at java.lang.Class.getDeclaredMethods0(Native Method)
at java.lang.Class.privateGetDeclaredMethods(Unknown Source)
at java.lang.Class.getMethod0(Unknown Source)
at java.lang.Class.getMethod(Unknown Source)
at sun.launcher.LauncherHelper.getMainMethod(Unknown Source)
at sun.launcher.LauncherHelper.checkAndLoadMain(Unknown Source)
Caused by: java.lang.ClassNotFoundException: org.openqa.selenium.WebDriver
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)
... 6 more
EDIT: The previous issue has been resolved by moving the created JAR file to the root directory of my project. It seems that directories defined in the manifest file are relative to your current directory. Since my lib directory exists in the root directory of my project and not in the dist directory, it makes sense why the aforementioned error was being thrown. More information is provided in the following link:
Java NoClassDefFoundError when running jar containing library jar
Upvotes: 0
Views: 5643
Reputation: 3400
Your manifest Main-Class needs the package information as well, just like any any main method to run using java. Try with below.
manifest>
<attribute name="Main-Class" value="com.selenium.example.ExampleScenario"/>
<attribute name="Class-Path" value="${lib.dir}/selenium-server-standalone-2.35.0.jar; "/>
</manifest>
Update - selenium-java jars are missing. Please add them. Download it from here
Upvotes: 1