0

I am trying to create an executable (double-click) jar using Ant. My classes are ChatClient and ChatServer, which is a simple multithreading chat server. I have created build.xml after reading tutorials but it seems like something is wrong here. The error I get is, Error: Could not find or load main class C:... (the path of jar file)

My project name is JamesPractice and the package name is SimpleChat. Classes are SimpleChatServer and SimpleChatClient

<?xml version="1.0"?>
<project name = "JamesPractice" default= "jar" basedir =".">
<property name = "src.dir" location = "src" />
<property name = "build.dir" location = "c:\Build" />
<property name = "project.name" value = "JamesPractice" />
<property name="lib.dir" location="lib" />

<target name = "clean">
    <delete dir="${build.dir}" />
</target>

<target name = "makedir">
    <mkdir dir= "${build.dir}" />
    <mkdir dir= "${build.dir}\classes" />
</target>

<target name = "compile" depends = "clean, makedir">
    <javac srcdir = "${src.dir}" destdir = "${build.dir}\classes" />
</target>

<target name = "jar" depends = "compile">
    <jar destfile = "${build.dir}/jars/${ant.project.name}.jar" basedir = "${build.dir}/classes" />
    <manifest file = "MANIFEST.MF">
        <attribute name = "Main-Class" value = "SimpleChat.SimpleChatServer"/>
        <attribute name = "Class-Path" value = "."/>
    </manifest>

</target>

MANIFEST.MF
Manifest-Version: 1.0
Ant-Version: Apache Ant 1.9.2
Created-By: 1.7.0_11-b21 (Oracle Corporation)
Main-Class: SimpleChat.SimpleChatClient

Editied I have merged two classes and it works fine. but the .jar still gives me the same error.. Going to test with HelloWorld to see if I can get this working...

jaycee
  • 1
  • 1
  • 2
  • 9
  • http://stackoverflow.com/questions/9874550/how-to-create-a-bundled-runnable-jar-using-ant – Jayan Nov 16 '14 at 16:09

1 Answers1

0

Move "manifest" part under <jar>...</jar> tag:

<target name = "jar" depends = "compile">
    <jar destfile = "${build.dir}/jars/${ant.project.name}.jar" basedir = "${build.dir}/classes">
        <manifest file = "MANIFEST.MF">
            <attribute name = "Main-Class" value = "SimpleChat.SimpleChatServer"/>
            <attribute name = "Class-Path" value = "."/>
        </manifest>
    </jar>
</target>

And it would be better to use Maven instead of old-fashion Ant. Maven becomes an industry standard several years ago.

ursa
  • 4,404
  • 1
  • 24
  • 38
  • Thank you, I moved under and got rid of file = "MANIFEST.MF" because the error says manifest doesn't support the "file" attribute. But I still get the same "could not load" main error. Can this be something wrong with class path or main-class? – jaycee Nov 16 '14 at 16:52
  • Seems you don't need Class-Path attribute in your build. Try to remove it. If it will not help - edit your question and add generated MANIFEST.MF content into it. – ursa Nov 16 '14 at 17:02
  • I still get the same error after removing class-path. I added MANIFEST.MF that was generated before removing it. – jaycee Nov 16 '14 at 17:18
  • Just to verify: do you have /SimpleChat/SimpleChatServer.class file in your jar file? – ursa Nov 16 '14 at 17:23
  • Yes, /SimpleChat/SimpleChatServer.class and /SimpleChat/SimpleChatClient.class are both in the jar file – jaycee Nov 16 '14 at 17:31
  • Just edited the MANIFEST.MF to the most current one (after applying fixes that was suggested). Hmm still no luck – jaycee Nov 16 '14 at 18:00
  • the last try I can suppose: can you start your application with classic "java -cp JamesPractice.jar SimpleChat.SimpleChatClient"? If no - the problem is in your class (also add it into question). If yes - no ideas... – ursa Nov 16 '14 at 18:20
  • still no luck, but added a comment that might be a problem – jaycee Nov 16 '14 at 18:37
  • Main class is a class, that contains "public static void main(String [] args) {}" method. See docs: https://docs.oracle.com/javase/tutorial/deployment/jar/appman.html – ursa Nov 16 '14 at 18:40