【问题标题】:How to execute the java program of apache thrift (with ant if possible)如何执行apache thrift的java程序(可能的话用ant)
【发布时间】:2013-10-05 12:20:11
【问题描述】:

如果有人能帮我用ant 编译它,我的系统上安装了thrift ~/thrift,那就太好了

我已经用命令行完成了以下操作。

我写了一个 apache thrift 程序并且可以在 eclipse 中执行它。我想从命令行执行。

我的基目录的树形结构是:

.
├── bin
├── lib
│   ├── commons-codec-1.6.jar
│   ├── commons-lang3-3.1.jar
│   ├── commons-logging-1.1.1.jar
│   ├── httpclient-4.2.5.jar
│   ├── httpcore-4.2.4.jar
│   ├── junit-4.4.jar
│   ├── libthrift-0.9.1.jar
│   ├── log4j-1.2.14.jar
│   ├── servlet-api-2.5.jar
│   ├── slf4j-api-1.5.8.jar
│   └── slf4j-log4j12-1.5.8.jar
└── src
    ├── FileMetaData.java
    ├── files
    ├── FileStoreImpl.java
    ├── FileStore.java
    ├── RFile.java
    ├── RFileMetadata.java
    ├── Server.java
    ├── Status.java
    ├── StatusReport.java
    └── SystemException.java

4 directories, 20 files

src/Server.java 具有要执行的 main 函数。

我使用以下命令编译:

javac -classpath "lib/libthrift-0.9.1.jar:lib/slf4j-log4j12-1.5.8.jar:lib/slf4j-api-1.5.8.jar:lib/log4j-1.2.14.jar:lib/commons-logging-1.1.1.jar" -d bin src/*.java -Xlint:none

它会生成以下警告:

Note: Some input files use unchecked or unsafe operations.
Note: Recompile with -Xlint:unchecked for details.

但我可以接受,因为我只想在本地执行它,并且我已经在 eclipse 中执行它并且知道它可以工作。

为了从命令行运行,我尝试了这个命令:

cd bin
java Server

或者这个

java -classpath "lib/libthrift-0.9.1.jar:lib/slf4j-log4j12-1.5.8.jar:lib/slf4j-api-1.5.8.jar:lib‌​/log4j-1.2.14.jar:lib/commons-logging-1.1.1.jar:bin" Server 

我收到此错误:

Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/thrift/transport/TTransportException
    at java.lang.Class.getDeclaredMethods0(Native Method)
    at java.lang.Class.privateGetDeclaredMethods(Class.java:2521)
    at java.lang.Class.getMethod0(Class.java:2764)
    at java.lang.Class.getMethod(Class.java:1653)
    at sun.launcher.LauncherHelper.getMainMethod(LauncherHelper.java:494)
    at sun.launcher.LauncherHelper.checkAndLoadMain(LauncherHelper.java:486)
Caused by: java.lang.ClassNotFoundException: org.apache.thrift.transport.TTransportException
    at java.net.URLClassLoader$1.run(URLClassLoader.java:366)
    at java.net.URLClassLoader$1.run(URLClassLoader.java:355)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(URLClassLoader.java:354)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
    ... 6 more

【问题讨论】:

  • 只是一个上下文:我必须将此作为作业提交,我应该提供一个脚本来编译文件并在讲师的另一台机器上运行它。

标签: java ant command-line compiler-errors javac


【解决方案1】:

你需要指定类路径,和你编译时使用的一样:

您需要留在项目的顶级文件夹(binlib 的父级)并将 bin 添加到类路径中。

java -classpath "bin:lib/libthrift-0.9.1.jar:lib/slf4j-log4j12-1.5.8.jar:lib/slf4j-api-1.5.8.jar:lib‌​/log4j-1.2.14.jar:lib/commons-logging-1.1.1.jar" Server 

您可以将您的类打包到一个自己的 jar 中并将其添加到类路径中。

编辑

示例 ant build.xml 文件

<?xml version="1.0" encoding="UTF-8"?>
<project name="project-template" basedir="." default="package">
    <!-- properties  -->
    <property name="src" location="src" />
    <property name="bin" value="bin" />
    <property name="lib" value="lib" />
    <property name="dist" value="dist" />
    <property name="java.version.target" value="1.6" />
    <property name="main.class" value="com.client.ClientMain" />
    <property name="jarfile" value="${ant.project.name}.jar" />

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

    <!-- target package: create the jar file -->
    <target name="package" depends="compile">
        <jar jarfile="${dist}/${jarfile}" basedir="${bin}">
            <zipgroupfileset dir="${lib}" includes="*.jar"/>
            <manifest>
                <attribute name="Main-Class" value="${main.class}"/>
            </manifest>
        </jar>
    </target>

    <!-- target compile: compile the source files -->
    <target name="compile" depends="prepare">
        <javac includeantruntime="false"
            srcdir="${src}" destdir="${bin}" debug="false" optimize="true" 
            target="${java.version.target}">
            <compilerarg value="-Xlint:all -verbose -deprecation"/>
            <classpath refid="compile.classpath"/>
        </javac>
    </target>

    <!-- Create build directories as needed -->
    <target name="prepare">
        <mkdir dir="${bin}" />
        <mkdir dir="${dist}" />
    </target>

    <target name="clean">
        <delete includeemptydirs="true" verbose="true">
            <fileset dir="${bin}" includes="**/*" />
            <fileset dir="${dist}" includes="**/*" />
        </delete>
    </target>
</project>

【讨论】:

  • @footy,我看到了问题,当你在bin 并尝试运行你的程序时,你无法访问lib/*,如果你这样做ls lib,这甚至都行不通。在bin 中,您必须将../ 添加到类路径中的每个条目中。最好返回到项目的顶层并添加bin 类路径。查看我的编辑。
  • 我是从顶级目录本身做的,并将 bin 添加到类路径中。还是不行
  • 如果我能得到ant 来编译和运行它会很棒。
  • @footy,当你有 Eclipse 时,为什么还要打扰 ant?只需将您的项目导出为Runnable jar 并选择Package required libraries into generated JAR
  • 我必须将此作为作业提交,并且我应该提供一个脚本来编译文件并在讲师的另一台机器上运行它。
猜你喜欢
  • 2014-06-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-01-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-08-30
相关资源
最近更新 更多