Created a jar archive using ANT. Build code:

<?xml version="1.0"?> <project name="HelloWorld" default="run"> <!-- define names of directories --> <property name="src" location="src"/> <property name="build" location="build"/> <property name="classes" location="${build}/classes"/> <!-- define all targets --> <target name="hello"> <echo>Hello, World!</echo> </target> <target name="compile"> <mkdir dir="${classes}"/> <javac srcdir="${src}" destdir="${classes}" includeAntRuntime="false"/> </target> <target name="run" depends="hello, compile"> <java classname="${ant.project.name}" classpath="${classes}"/> </target> <target name="clean"> <delete dir="${build}"/> </target> <target name="package" depends="compile"> <jar destfile="${build}/${ant.project.name}.jar" basedir="${classes}"> <manifest> <attribute name="Main–Class" value="${ant.project.name}"/> </manifest> </jar> </target> </project> 

A HelloWorld.jar file was created with the manifest MANIFEST.MF with the code:

 Manifest-Version: 1.0 Ant-Version: Apache Ant 1.10.1 Created-By: 1.8.0_131-b11 (Oracle Corporation) Main–Class: HelloWorld 

Now when by hand in the console from the place where the folder SRC and BUILD run:

 java -jar build/HelloWorld.jar 

then the following error appears:

 Error: An unexpected error occurred while trying to open file build/HelloWorld.jar 

environment variables rechecked JAVA_HOME and PATH specified path to the bin folder of JDK1.8

Help me figure out why the HelloWorld file isn't running?

  • The manifest is in the correct way META-INF/MANIFEST.MF ? - Alex Chermenin
  • Yes, here it is: - Paul
  • C: \ Users \ Sunrise \ IdeaProjects \ EPAMAUTUMN2017 \ build \ HelloWorld.zip \ META-INF - Paul
  • and if you specify the path through -cp, then everything works: java -cp HelloWorld.jar HelloWorld - Pavel
  • I thought maybe because one of the directories in Russian moved to another directory: / d / XXXX / build. Nothing has changed - Paul

2 answers 2

The problem is this: in the configuration of the assembly in the string

 <attribute name="Main–Class" value="${ant.project.name}"/> 

the symbol – is not a hyphen, but a short dash . Therefore, this attribute is not readable when attempting to run. Most likely the fact is that this configuration was copied from somewhere in this form.

To solve the problem, correct the hyphen in this line.

    In general, I don’t know what happened ((((But I rewrote the attribute name in the manifest in the xml file from the keyboard (Stehr "Main-Class" and wrote "Main-Class" again) and everything worked fine about the miracle. And in I uploaded the xml file and copy the attribute name "Main-Class" and paste it into my xml, clear, re-assemble the jarnik, start and throw an error again !!! I re-write again from the keyboard, clear, collect, start - everything works. Some magic ( ((