Jump to content

Java - Importing from a Jar file hav touble with set CLASSPATH

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
2 replies to this topic

#1
minifish

minifish

    Newbie

  • Members
  • Pip
  • 5 posts
Hello experts,

I'm try to import java file in unix.
Under bash, I tried
export CLASSPATH=/blah/blah/some.jar:/blah/some2.jar
The CLASSPATH variable was initially undefined.

and when I try to compile my java program, I get error message

error: error reading /blah/blah/some.jar; error in opening zip file

Any suggestions? or what I mit be doing wrong?

Thank u

-minifish

#2
navghost

navghost

    Newbie

  • Members
  • PipPip
  • 17 posts
Hi,
try compile Your classes with Ant. Here is a simple ant file:

build.xml
<project name="buildSimpleExecuteJarFile" default="build" basedir=".">
	<property file="${basedir}/ant/ant.properties"/>
	
	<path id="class.path">
		<fileset dir="${lib.dir}">
			<include name="some.jar"/>
			<include name="some2.jar"/>
		</fileset>
	    <pathelement path="${java.class.path}"/>
	 </path>
	
	<target name="init">
		<tstamp>
			<format property="start.TODAY" pattern="dd-MM-yyyy HH:mm:ss" />
		</tstamp>
		<echo message="Start of Ant script: ${start.TODAY}" />
		<echo message="Java version: ${ant.java.version}" />
	</target>
	
	<target name="compile">
		<javac srcdir="${src.dir}" destdir="${classes.dir}" classpathref="class.path" fork="no"/>
	</target>
	
	<target name="build" depends="init, clean, build-dir, compile">
		<jar destfile="${release.dir}/simpleExecuteFile.jar" basedir="${classes.dir}">
			<manifest>
				<attribute name="Built-By" value="${user.name}"/>
				<attribute name="Main-Class" value="${main.class}"/>
				<attribute name="Class-Path" value="${mf.classpath}"/>
		    </manifest>
		</jar>
	</target>
	
	<target name="build-dir">
		<mkdir dir="${classes.dir}"/>
		<mkdir dir="${release.dir}"/>
	</target>
	
	<target name="clean">
		<delete dir="${classes.dir}"/>
		<delete dir="${release.dir}"/>
	</target>
</project>

ant.properties
user.name=username

src.dir=${basedir}/src
classes.dir=${basedir}/classes
release.dir=${basedir}/release
lib.dir=${basedir}/lib

main.class=net.codecall.forum.SimpleExecuteJarFile

mf.classpath=\
${lib.dir}/some.jar \
${lib.dir}/some2.jar

Structure of Yours directories and files should looks like this:
/home
    /username
        /simpleapp
            /ant
                ant.properties
            /lib
                some.jar
                some2.jar
            /src
                /net
                    /codecall
                        /forum
                            SimpleExecuteJarFile.java
            build.xml

When You wont to run Ant script and build Your application then You have to write only two words in console:
ant build
or
ant
because the build task is set as default task.

I wrote this without testing ;) so if You have a problem with all of it then please write the post under the my ;)

Have fun!
"Work should be challenging and the challenge should be fun. Great just isn't good enough!"

#3
minifish

minifish

    Newbie

  • Members
  • Pip
  • 5 posts
Thank you very much with the great help, I got the CLASSPATH to work.