I am trying to execute a jar file StartupUtil.jar but it's giving an error of Couldnot find and load main class. I looked at other similar question and tried but couldnot figure out what is wrong.
My structure for created StartupUtil.jar is
->com.ihc.startup.util.StartupService
->META-INF/MANIFEST.MF
The content of MANIFEST is:
Manifest-Version: 1.0
Ant-Version: Apache Ant 1.9.2
Created-By: 1.7.0_79-b15 (Oracle Corporation)
Main-Class: com.ihc.startup.util.StartupService
Class-Path: C:\Users\tgupta12\workspace_new\IHC_Startup\lib\bson-3.0.1
.jar C:\Users\tgupta12\workspace_new\IHC_Startup\lib\mongodb-driver-3
.0.1.jar C:\Users\tgupta12\workspace_new\IHC_Startup\lib\mongodb-driv
er-core-3.0.1.jar C:\Users\tgupta12\workspace_new\IHC_Startup\classes
Here is my build.xml
<?xml version="1.0" encoding="UTF-8"?>
<project name="Startup" default="build" basedir=".">
<property file="./build.properties" />
<path id="lib-classpath">
<fileset dir="${libApp.dir}">
<include name="*.jar"/>
</fileset>
<pathelement path="${bin.dir}"/>
</path>
<target name="build" description="Compile main source tree java files">
<echo message=" Build Startup Utility" />
<mkdir dir="${bin.dir}"/>
<echo message=" Compiling source files" />
<javac destdir="${bin.dir}" source="${versionJDK}" target="${versionTarget}" debug="true"
deprecation="false" optimize="false" failonerror="true" includeantruntime="false">
<src path="${src.dir}"/>
<classpath refid="lib-classpath"/>
</javac>
<echo message=" ...Compilation of source files OK" />
<echo message=" Generating JAR for Startup - StartupUtility.jar" />
<delete file="${out.dir}/${startup-util-name}" />
<!-- convert classpath to a flat list/string -->
<pathconvert property="lib.classpath" pathsep=" ">
<path refid="lib-classpath" />
<!--<flattenmapper />-->
</pathconvert>
<jar destfile = "${out.dir}/${startup-util-name}" basedir = "${bin.dir}" includes = "**/*">
<manifest >
<attribute name="Class-Path" value="${lib.classpath}" />
<attribute name="Main-Class" value="com.ihc.startup.util.StartupService"/>
</manifest>
</jar>
<echo message=" ...JAR Created for Startup" />
</target>
<target name="run" depends="build">
<java jar="${out.dir}/${startup-util-name}" fork="true"/>
</target>
Below is my build.properties file:
#Directories
build.dir=build
src.dir=src
libApp.dir=lib
out.dir=out
web.dir=WebContent/WEB-INF
bin.dir=classes
webcontent.dir=WebContent
#File Name
war-file-name=StartupService.war
startup-util-name=StartupUtil.jar
#Target Properties
versionJDK=1.7
versionTarget=1.7
When it tries to execute the target run it gives
Error: Could not find or load main class com.ihc.startup.util.StartupService
I strongly suspect that the problem is it can't find the dependencies, which means it can't properly load the main class. I've never seen absolute filenames given in a manifest before, nor am I convinced about how you're breaking the lines (although that may be valid). Given how unportable it is to use the absolute filenames, I strongly suggest you just use relative ones.
Change your manifest to just:
Manifest-Version: 1.0
Ant-Version: Apache Ant 1.9.2
Created-By: 1.7.0_79-b15 (Oracle Corporation)
Main-Class: com.ihc.startup.util.StartupService
Class-Path: bson-3.0.1.jar mongodb-driver-3.0.1.jar mongodb-driver-core-3.0.1.jar
Then put those jar files in the same directory as StartupUtil.jar
.
See more on this question at Stackoverflow