I have been struggling with this for two days now. I created a very simple HelloWorld class to test if I can get this working but I was not able to. I get Error- Could not find or load main class... It works from Eclipse or run task from the script. But double-clicking .jar or running it from CMD gives me the error. What are some possible reasons for this error? Class-path? environmental variables? directory structure? Please help!
package com.hellojava;
public class HelloWorld {
public static void main(String[] args)
{
System.out.println("Hello World!");
}
}
My build.xml
<project name="TestProject" basedir="." default="main">
<property name="src.dir" value="src"/>
<property name="build.dir" value="build"/>
<property name="classes.dir" value="${build.dir}/classes"/>
<property name="jar.dir" value="${build.dir}/jar"/>
<property name="main-class" value="com.hellojava.HelloWorld"/>
<target name="clean">
<delete dir="${build.dir}"/>
</target>
<target name="compile">
<mkdir dir="${classes.dir}"/>
<javac srcdir="${src.dir}" destdir="${classes.dir}"/>
</target>
<target name="jar" depends="compile">
<mkdir dir="${jar.dir}"/>
<jar destfile="${jar.dir}/${ant.project.name}.jar" basedir="${classes.dir}">
<manifest>
<attribute name="Main-Class" value="${main-class}"/>
</manifest>
</jar>
</target>
<target name="run" depends="jar">
<java jar="${jar.dir}/${ant.project.name}.jar" fork="true"/>
</target>
<target name="clean-build" depends="clean,jar"/>
<target name="main" depends="clean,run"/>
run:
[java] Hello World!
main:
BUILD SUCCESSFUL
Total time: 5 seconds
Manifest-Version: 1.0
Ant-Version: Apache Ant 1.9.2
Created-By: 1.7.0_11-b21 (Oracle Corporation)
Main-Class: com.hellojava.HelloWorld
**Edited: java -jar TestProject.jar works but javaw -jar TestProject.jar does not. However, I solved the problem- see answer I posted.