使用外部包 Java

Using external packages Java

我以前从未使用过Java。作为一名 C++ 程序员,包的概念对我来说似乎有点陌生。如果我从我下载 (quickfix) 并使用 Ant 编译的开源库导入自定义包,我如何确保我的 Java 代码找到它?

这是我尝试从 java 库中导入的内容,我使用 Ant 下载并构建了它。

import java.util.Iterator;

import quickfix.DataDictionary;
import quickfix.Field;
import quickfix.FieldMap;
import quickfix.FieldNotFound;
import quickfix.FieldType;
import quickfix.Group;
import quickfix.field.MsgType;
import quickfix.fix44.Message;

我用 "javac file.java" 编译,这是我得到的输出:

MessagePrinter.java:5: package quickfix does not exist
import quickfix.DataDictionary;

如何告诉编译器找到我刚刚编译的 quickfix 库?

关于Java,我是新手,所以请多多包涵。谢谢!

编辑:这是库的BUILD.XML文件

<?xml version="1.0" encoding="ISO-8859-1" ?>
<project name="master" basedir="." default="">
    <property file="${user.home}/.build"/>

    <property name="jvm.java4" value="/home/eric/apps/j2sdk1.4.2_19/bin/java"/>

    <property name="release.output.dir" value="${basedir}/dist"/>

    <property name="release.src.staging.dir"
              value="${release.output.dir}/staging/source/quickfixj" />

    <property name="release.bin.staging.dir"
              value="${release.output.dir}/staging/bin/quickfixj" />

    <property name="release.bin_src.staging.dir"
              value="${release.output.dir}/staging/bin_src" />

    <property name="staging.dir.java4"
              value="${release.output.dir}/java4/staging/bin/quickfixj"/>


    <property name="output.dir" value="classes" />

    <macrodef name="iterate">
        <attribute name="target" />
        <element name="properties" optional="true"/>
        <sequential>
            <subant target="@{target}" verbose="yes">
                <fileset dir="." includes="*/build.xml" />
                <property name="version" value="${version}"/>
                <properties/>
            </subant>
        </sequential>
    </macrodef>

    <target name="jar" description="Create module jar files" depends="query.version">
        <iterate target="jar"/>
    </target>

    <target name="clean" description="Clean files">
        <iterate target="clean"/>
    </target>

    <target name="test" description="Run tests" unless="skip.tests">
        <iterate target="test"/>
    </target>

    <target name="javadoc" description="Generate Javadocs" unless="skip.javadocs">
        <iterate target="javadoc"/>
    </target>

    <path id="core.lib.classpath">
        <fileset id="core.lib.fileset" dir="${core.lib.dir}">
            <include name="*.jar"/>
        </fileset>
    </path>

    <path id="optional.lib.classpath">
        <fileset dir="${optional.lib.dir}">
            <include name="**/*.jar" />
        </fileset>
    </path>

    <path id="test.lib.classpath">
        <fileset dir="${test.lib.dir}">
            <include name="*.jar" />
        </fileset>
    </path>

    <path id="compile.classpath">
        <pathelement location="${product.classes.dir}" />
        <pathelement location="${test.classes.dir}" />
        <path refid="core.lib.classpath" />
        <path refid="test.lib.classpath" />
        <path refid="optional.lib.classpath" />
    </path>

    <path id="test.classpath">
        <pathelement location="${jar.output.dir}/${quickfixj.jar}" />
        <pathelement location="${test.classes.dir}" />
        <path refid="core.lib.classpath" />
        <path refid="test.lib.classpath" />
        <path refid="optional.lib.classpath" />
    </path>

    <macrodef name="initdir">
        <attribute name="dir" />
        <sequential>
            <delete dir="@{dir}" failonerror="no" />
            <mkdir dir="@{dir}" />
        </sequential>
    </macrodef>

    <macrodef name="convertUnixLineEndings">
        <element name="files" implicit="true"/>
        <sequential>
            <replaceregexp flags="smg" match="\r$" replace="" byline="false">
                <files/>
            </replaceregexp>
        </sequential>
    </macrodef>

    <macrodef name="convertUnixScripts">
        <attribute name="dir" />
        <sequential>
            <convertUnixLineEndings>
                <fileset dir="@{dir}" includes="**/*.sh" />
            </convertUnixLineEndings>
        </sequential>
    </macrodef>

    <!-- Create release archives -->
    <target name="release.timestamped"
            description="release files with timestamp for version"
            depends="create.timestamp.version">
        <antcall target="release.shared"/>
        <antcall target="release.shared.java4"/>
    </target>

    <target name="create.timestamp.version">
        <tstamp>
            <format property="timestamp" pattern="yyyyMMddHHmmss" />
        </tstamp>
        <property name="release.prefix" value="quickfixj-${timestamp}" />
        <property name="version" value="SNAPSHOT-${timestamp}"/>
    </target>

    <target name="release.versioned"
            description="release files with explicit version"
            depends="query.version">
        <antcall target="release.shared"/>
        <antcall target="release.shared.java4"/>
    </target>

    <target name="query.version" unless="release.prefix">
        <input message="Enter version number" addproperty="version" />
        <property name="release.prefix" value="quickfixj-${version}" />
    </target>

    <target name="release.shared" depends="jar,test,javadoc">
        <initdir dir="${release.output.dir}" />

        <antcall target="release.source"/>
        <antcall target="release.binaries"/>

        <antcall target="test_zip_bin_release"/>
        <antcall target="test_tar_bin_release"/>
        <antcall target="test_tar_src_release"/>
        <antcall target="test_zip_src_release"/>
    </target>

    <target name="release.binaries">
        <copy todir="${release.bin.staging.dir}">
            <fileset dir="." includes="LICENSE"/>
        </copy>

        <mkdir dir="${release.bin.staging.dir}" />
        <iterate target="export_binaries">
            <properties>
                <property name="release.bin.staging.dir" value="${release.bin.staging.dir}"/>
                <property name="staging.dir.java4" value="${staging.dir.java4}"/>
            </properties>
        </iterate>

        <mkdir dir="${release.bin_src.staging.dir}" />
        <iterate target="export_binary_src">
            <properties>
                <property name="release.bin_src.staging.dir" value="${release.bin_src.staging.dir}"/>
            </properties>
        </iterate>

        <property name="release.bin_src.zip" value="${release.bin.staging.dir}/src.zip"/>
        <zip destfile="${release.bin_src.zip}"
             basedir="${release.bin_src.staging.dir}" />

        <convertUnixScripts dir="${release.bin.staging.dir}"/>

        <!-- Create binary releases -->
        <property name="release.bin"
                  value="${release.output.dir}/${release.prefix}-bin" />

        <property name="release.bin.tar" value="${release.bin}.tar" />
        <tar destfile="${release.bin.tar}.gz"
             longfile="gnu"
             compression="gzip"
             basedir="${release.bin.staging.dir}/.." />

        <property name="release.bin.zip" value="${release.bin}.zip" />
        <zip destfile="${release.bin.zip}"
             basedir="${release.bin.staging.dir}/.." />
    </target>

    <target name="release.source">
        <mkdir dir="${release.src.staging.dir}" />

        <copy todir="${release.src.staging.dir}">
            <fileset dir="." includes="*" excludes=".settings,classes,dist,build.properties,target"/>
        </copy>

        <iterate target="export_sources">
            <properties>
                <property name="release.src.staging.dir" value="${release.src.staging.dir}"/>
            </properties>
        </iterate>
        <convertUnixScripts dir="${release.src.staging.dir}"/>

        <!-- Build source archives -->
        <property name="release.src"
                  value="${release.output.dir}/${release.prefix}-src" />

        <property name="release.src.tar" value="${release.src}.tar" />
        <tar destfile="${release.src.tar}.gz"
             longfile="gnu"
             compression="gzip"
             basedir="${release.src.staging.dir}/.." />

        <property name="release.src.zip" value="${release.src}.zip" />
        <zip destfile="${release.src.zip}"
             basedir="${release.src.staging.dir}/.." />
    </target>

    <macrodef name="test_executor">
        <attribute name="dir"/>
        <sequential>
            <java  fork="true" outputproperty="executor.output" classname="quickfix.examples.executor.Executor" inputstring="exit\n">
                <classpath>
                    <fileset dir="@{dir}">
                        <include name="lib/*.jar"/>
                        <include name="lib/optional/mx4j-*.jar"/>
                        <include name="*.jar"/>
                    </fileset>
                </classpath>
            </java>
            <echo message="${executor.output}" />
            <condition property="executor.success">
                <contains string="${executor.output}" substring="Logging out all sessions"/>
            </condition>
            <fail message="Executor failed" unless="executor.success"/>
        </sequential>
    </macrodef>

    <macrodef name="test_bin_contents">
        <attribute name="dir"/>
        <sequential>
            <condition property="dirs.exist">
                <and>
                    <available file="@{dir}/quickfixj/bin" />
                    <available file="@{dir}/quickfixj/doc/api"/>
                    <available file="@{dir}/quickfixj/doc/usermanual"/>
                    <available file="@{dir}/quickfixj/etc/FIX40.xml"/>
                    <available file="@{dir}/quickfixj/etc/FIX41.xml"/>
                    <available file="@{dir}/quickfixj/etc/FIX42.xml"/>
                    <available file="@{dir}/quickfixj/etc/FIX43.xml"/>
                    <available file="@{dir}/quickfixj/etc/FIX44.xml"/>
                    <available file="@{dir}/quickfixj/etc/sql/mssql"/>
                    <available file="@{dir}/quickfixj/etc/sql/mysql"/>
                    <available file="@{dir}/quickfixj/etc/sql/oracle"/>
                    <available file="@{dir}/quickfixj/etc/sql/postgresql"/>
                    <available file="@{dir}/quickfixj/lib"/>
                    <available file="@{dir}/quickfixj/quickfixj-core-${version}.jar"/>
                    <available file="@{dir}/quickfixj/quickfixj-all-${version}.jar"/>
                    <available file="@{dir}/quickfixj/quickfixj-msg-fix40-${version}.jar"/>
                    <available file="@{dir}/quickfixj/quickfixj-msg-fix41-${version}.jar"/>
                    <available file="@{dir}/quickfixj/quickfixj-msg-fix42-${version}.jar"/>
                    <available file="@{dir}/quickfixj/quickfixj-msg-fix43-${version}.jar"/>
                    <available file="@{dir}/quickfixj/quickfixj-msg-fix44-${version}.jar"/>
                    <available file="@{dir}/quickfixj/quickfixj-examples-${version}.jar"/>
                    <available file="@{dir}/quickfixj/src.zip"/>
                    <available file="@{dir}/quickfixj/LICENSE"/>
                </and>
            </condition>
            <fail message="Missing files or directories in bin release" unless="dirs.exist"/>
        </sequential>
    </macrodef>

    <target name="test_zip_bin_release">
        <property name="zip.bin.dir" value="${release.output.dir}/zip-bin"/>
        <unzip src="${release.output.dir}/${release.prefix}-bin.zip" dest="${zip.bin.dir}" />
        <test_executor dir="${zip.bin.dir}/quickfixj"/>
        <test_bin_contents dir="${zip.bin.dir}"/>
    </target>

    <target name="test_tar_bin_release">
        <property name="tar.bin.dir" value="${release.output.dir}/tar-bin"/>
        <untar compression="gzip" src="${release.output.dir}/${release.prefix}-bin.tar.gz" dest="${tar.bin.dir}" />
        <test_executor dir="${tar.bin.dir}/quickfixj"/>
        <test_bin_contents dir="${tar.bin.dir}"/>
    </target>

    <target name="test_zip_src_release">
        <unzip src="${release.output.dir}/${release.prefix}-src.zip" dest="${release.output.dir}/zip-src" />
        <mkdir dir="${release.output.dir}/zip-src/quickfixj/output/ant/testreports" />
        <test_executor dir="${release.output.dir}/zip-bin/quickfixj"/>
    </target>

    <target name="test_tar_src_release">
        <untar compression="gzip" src="${release.output.dir}/${release.prefix}-src.tar.gz" dest="${release.output.dir}/tar-src" />
        <mkdir dir="${release.output.dir}/tar-src/quickfixj/output/ant/testreports" />
        <ant dir="${release.output.dir}/tar-src/quickfixj/core" antfile="build.xml" target="test.unit" />
    </target>

    <!-- ========================================================================= -->
    <!-- Java 4 Release Creation -->
    <!-- ========================================================================= -->

    <path id="retrotranslator.classpath">
        <fileset dir="" includes="**/*.jar"/>
    </path>

         <taskdef name="retrotranslator" classpathref="retrotranslator.classpath"
                        classname="net.sf.retrotranslator.transformer.RetrotranslatorTask">
         </taskdef>


     <target name="release.shared.java4" unless="skip_jdk4">
        <echo>Building Java4 version</echo>
        <property name="release.bin.java4"
                  value="${release.output.dir}/${release.prefix}-java4-bin" />

        <delete dir="${staging.dir.java4}"/>

        <!-- Copy Java 5 binary staging -->
        <copy todir="${staging.dir.java4}">
            <fileset dir="${release.bin.staging.dir}">
                <exclude name="quickfixj-*.jar"/>
                <exclude name="lib/**"/>
            </fileset>
        </copy>

        <!-- Set up Java 1.4-related third-party libraries -->
        <copy todir="${staging.dir.java4}/lib">
            <fileset dir="core/src/main/lib/java4">
                <exclude name="retrotranslator/**"/>
            </fileset>
        </copy>

        <retrotranslate-jar jar="quickfixj-all-${version}.jar"/>
        <retrotranslate-jar jar="quickfixj-core-${version}.jar"/>
        <retrotranslate-jar jar="quickfixj-examples-${version}.jar"/>
        <retrotranslate-jar jar="quickfixj-msg-fix40-${version}.jar"/>
        <retrotranslate-jar jar="quickfixj-msg-fix41-${version}.jar"/>
        <retrotranslate-jar jar="quickfixj-msg-fix42-${version}.jar"/>
        <retrotranslate-jar jar="quickfixj-msg-fix43-${version}.jar"/>
        <retrotranslate-jar jar="quickfixj-msg-fix44-${version}.jar"/>
        <retrotranslate-jar jar="quickfixj-msg-fix50-${version}.jar"/>
        <retrotranslate-jar jar="quickfixj-msg-fixt11-${version}.jar"/>

        <!-- Create and do basic testing on binary archives -->

        <tar destfile="${release.bin.java4}.tar.gz"
             longfile="gnu"
             compression="gzip"
             basedir="${staging.dir.java4}/.." />

        <property name="tar.bin.dir.java4" value="${release.output.dir}/java4/tar-bin"/>
        <untar compression="gzip" src="${release.bin.java4}.tar.gz" dest="${tar.bin.dir.java4}" />
        <test_executor_java4 dir="${tar.bin.dir.java4}/quickfixj"/>
        <test_bin_contents dir="${tar.bin.dir.java4}"/>

        <zip destfile="${release.bin.java4}.zip"
             basedir="${staging.dir.java4}/.." />

        <property name="zip.bin.dir.java4" value="${release.output.dir}/java4/zip-bin"/>
        <unzip src="${release.bin.java4}.zip" dest="${zip.bin.dir.java4}" />
        <test_executor_java4 dir="${zip.bin.dir.java4}/quickfixj"/>
        <test_bin_contents dir="${zip.bin.dir.java4}"/>

        <property name="test.dir.java4" value="${release.output.dir}/java4/test"/>
        <property name="test.classes.java4" value="${test.dir.java4}/classes"/>
        <mkdir dir="${test.classes.java4}"/>

        <antcall target="test.java4"/>
    </target>

    <target name="test.java4">
        <!-- Retrotranslate the test classes -->
        <retrotranslator
            target="1.4"
            destdir="${test.classes.java4}"
            uptodatecheck="On"
            lazy="false"
            embed="org.quickfixj.java4"
            verify="false">
            <classpath>
                <fileset dir="core/src/main/lib/java4">
                    <include name="*.jar"/>
                </fileset>
            </classpath>
            <fileset dir="core/target/classes/test"/>
        </retrotranslator>

        <property name="junit.output.dir.java4" value="${test.dir.java4}/junit"/>

        <antcall target="test.unit.java4"/>
        <antcall target="test.acceptance.java4"/>

    </target>

    <target name="test.unit.java4">
        <mkdir dir="${junit.output.dir.java4}"/>
        <junit jvm="${jvm.java4}" fork="yes" dir="${basedir}" tempdir="${junit.output.dir.java4}"
                printsummary="true" showoutput="false"
                errorproperty="utests.failed" failureproperty="utests.failed"
                haltonfailure="yes">
            <formatter type="xml" usefile="true" />
            <!-- TODO refactor class path -->
            <!-- TODO refactor junit dir property -->
            <classpath>
                <pathelement location="core/src/test/java"/>
                <pathelement location="${test.classes.java4}"/>
                <fileset dir="${staging.dir.java4}/lib" includes="*.jar"/>
                <fileset dir="${staging.dir.java4}" includes="*.jar"/>
                <fileset dir="core/src/test/lib" includes="*.jar">
                    <exclude name="mockito*"/>
                    <exclude name="hamcrest*"/>
                    <exclude name="junit*"/>
                </fileset>
                <fileset dir="core/src/main/lib/java4"
                    includes="retrotranslator/*runtime*.jar"/>
                <fileset dir="core/src/test/lib/java4" includes="*.jar"/>
                <fileset dir="core/src/main/lib/java4/optional" includes="*.jar"/>
            </classpath>
            <batchtest todir="${junit.output.dir.java4}">
                <fileset dir="core/src/test/java">
                    <include name="quickfix/*Test.java" />
                    <include name="quickfix/mina/**/*Test.java" />
                    <include name="quickfix/util/**/*Test.java" />
                    <exclude name="**/*ForTest.java" />
                    <exclude name="**/Abstract*Test.java" />
                    <exclude name="quickfix/Api*.java" />
                    <!-- Exclude following tests because they use JUnit 4 -->
                    <exclude name="**/DefaultDataDictionaryProviderTest.java"/>
                    <exclude name="**/DefaultMessageFactoryTest.java"/>
                    <exclude name="**/DefaultSessionFactoryTest.java"/>
                    <exclude name="**/FileLogTest.java"/>
                    <exclude name="**/MessageCrackerTest.java"/>
                    <exclude name="**/MessageTest.java"/>
                    <exclude name="**/SessionTest.java"/>
                    <exclude name="**/SessionResetTest.java"/>
                    <exclude name="**/SocketInitiatorTest.java"/>
                    <exclude name="**/SSLAndNonSSLTest.java"/>
                    <exclude name="**/AcceptorIoHandlerTest.java"/>
                    <exclude name="**/ThreadPerSessionEventHandlingStrategyTest.java"/>
                    <exclude name="**/FIXMessageDecoderTest.java"/>
                    <exclude name="**/MessageUtilsTest.java"/>
                </fileset>
            </batchtest>
        </junit>
    </target>

    <target name="test.acceptance.java4">
        <property name="atest.skipslow" value="false" />
        <property name="atest.classname" value="quickfix.test.acceptance.AcceptanceTestSuite" />
        <property name="atest.report" value="${junit.output.dir.java4}/TEST-${atest.classname}.xml" />
        <property name="atest.heartbeat" value="5" />
        <property name="atest.timeout" value="60000" />
        <property name="atest.reconnectDelay" value="5" />
        <property name="atest.skipslow" value="false" />

        <junit fork="true" dir="${basedir}" tempdir="${junit.output.dir.java4}"
            printsummary="true" showoutput="yes"
            errorproperty="atests.failed" failureproperty="atests.failed" haltonfailure="yes"
            maxmemory="512m">
            <sysproperty key="atest.heartbeat" value="${atest.heartbeat}" />
            <sysproperty key="atest.timeout" value="${atest.timeout}" />
            <sysproperty key="atest.reconnectDelay" value="${atest.reconnectDelay}" />
            <sysproperty key="atest.skipslow" value="${atest.skipslow}" />
            <formatter type="xml" usefile="true" />
            <classpath>
                <pathelement location="core/src/test/java"/>
                <pathelement location="${test.classes.java4}"/>
                <fileset dir="${staging.dir.java4}/lib" includes="*.jar"/>
                <fileset dir="${staging.dir.java4}" includes="*.jar"/>
                <fileset dir="core/src/test/lib" includes="*.jar"/>
                <fileset dir="core/src/main/lib/optional"
                    includes="jcl*.jar,proxool*.jar,sleepycat*.jar"/>
            </classpath>
            <test todir="${junit.output.dir.java4}" name="${atest.classname}" />
        </junit>
        <!-- remove the logged output because it's too large for junitreport -->
        <replaceregexp file="${atest.report}" flags="s" byline="false"
            match="system-err>.*&lt;/system-err"
            replace="system-err>&lt;![CDATA[]]>&lt;/system-err" />
    </target>

    <macrodef name="retrotranslate-jar">
        <attribute name="jar"/>
        <sequential>
            <retrotranslator
                target="1.4"
                srcjar="${release.bin.staging.dir}/@{jar}"
                destjar="${staging.dir.java4}/@{jar}"
                uptodatecheck="On"
                lazy="false"
                embed="org.quickfixj.java4"
                verify="false">
                <classpath>
                    <fileset dir="core/src/main/lib/java4">
                        <include name="*.jar"/>
                    </fileset>
                </classpath>
            </retrotranslator>
        </sequential>
    </macrodef>

    <macrodef name="test_executor_java4">
        <attribute name="dir"/>
        <sequential>
            <java jvm="${jvm.java4}" fork="true"
                taskname="java 1.4"
                outputproperty="executor.output"
                classname="quickfix.examples.executor.Executor"
                inputstring="exit\n">
                <classpath>
                    <fileset dir="@{dir}">
                        <include name="lib/*.jar"/>
                        <include name="lib/optional/mx4j-3.0.2.jar"/>
                        <include name="*.jar"/>
                    </fileset>
                </classpath>
            </java>
            <echo message="${executor.output}" />
            <condition property="executor.success">
                <contains string="${executor.output}" substring="Logging out all sessions"/>
            </condition>
            <fail message="Executor failed" unless="executor.success"/>
        </sequential>
    </macrodef>

    <target name="copy.dist" if="dist.copy.dir">
        <copy todir="${dist.copy.dir}/${version}" verbose="true" overwrite="true">
            <fileset dir="${release.output.dir}">
                <include name="quickfixj*.zip"/>
                <include name="quickfixj*.gz"/>
            </fileset>
        </copy>
    </target>

    <target name="upload" description="Send distribution of SourceForge">
        <ftp server="upload.sf.net" remotedir="incoming"
            userid="anonymous" password="guest" verbose="true">
            <fileset dir="dist" includes="*.zip,*.gz"/>
        </ftp>
    </target>
</project>

就像您告诉 C++ 编译器您的 LIBPATH 的方式一样,有一种方法可以告诉 java 编译器关于 java 术语中称为 classpath 的库路径.

因此,您按照此处所述添加类路径选项

javac -cp "pathtoquickfixjar" file.java