包org.apache.thrift不存在

时间:2013-03-15 15:07:45

标签: java jar import thrift

我正在尝试编写一个使用thrift的基本java应用程序。我缺少的是一个工作jar文件。

我从官方网站下载页面下载了0.8.0 gz版本的thrift,并自己创建了jar存档(jar cf thrift-0.8.0.jar src/org)。我已将新的jar文件移动到我的项目目录中。然后,我手动将这个jar文件添加到项目中 - 我在NetBeans IDE中工作。一切似乎应该工作,但它没有 - 红色错误消息发生在我想要使用thrift libs的任何地方。这是我的代码:

package com.blogspot.symfonyworld.thrift;

import com.blogspot.symfonyworld.thrift.server.MainService;
import org.apache.thrift.TException;

public class MainServiceHandler implements MainService.Iface {

  @Override
    public int add(int n1, int n2) throws TException {
        return n1 + n2;
    }

    @Override
    public int sub(int n1, int n2) throws TException {
        return n1 + n2;
    }

}

和另一个文件:

/**
 * Autogenerated by Thrift Compiler (0.8.0)
 *
 * DO NOT EDIT UNLESS YOU ARE SURE THAT YOU KNOW WHAT YOU ARE DOING
 *  @generated
 */
package com.blogspot.symfonyworld.thrift.server;

import org.apache.thrift.scheme.IScheme;
import org.apache.thrift.scheme.SchemeFactory;
import org.apache.thrift.scheme.StandardScheme;

import org.apache.thrift.scheme.TupleScheme;
import org.apache.thrift.protocol.TTupleProtocol;
import java.util.List;
import java.util.ArrayList;
import java.util.Map;
import java.util.HashMap;
import java.util.EnumMap;
import java.util.Set;
import java.util.HashSet;
import java.util.EnumSet;
import java.util.Collections;
import java.util.BitSet;
import java.nio.ByteBuffer;
import java.util.Arrays;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class MainService {

  public interface Iface {

    public int add(int n1, int n2) throws org.apache.thrift.TException;

    public int sub(int n1, int n2) throws org.apache.thrift.TException;

  }

  public interface AsyncIface {

    public void add(int n1, int n2, org.apache.thrift.async.AsyncMethodCallback<AsyncClient.add_call> resultHandler) throws org.apache.thrift.TException;

    public void sub(int n1, int n2, org.apache.thrift.async.AsyncMethodCallback<AsyncClient.sub_call> resultHandler) throws org.apache.thrift.TException;

  }

当我运行build时,出现以下错误:

ant -f /var/www/laughing-thrift-duck jar
init:
Deleting: /var/www/laughing-thrift-duck/build/built-jar.properties
deps-jar:
Updating property file: /var/www/laughing-thrift-duck/build/built-jar.properties
Compiling 2 source files to /var/www/laughing-thrift-duck/build/classes
/var/www/laughing-thrift-duck/src/com/blogspot/symfonyworld/thrift/MainServiceHandler.java:4: package org.apache.thrift does not exist
import org.apache.thrift.TException;
/var/www/laughing-thrift-duck/src/com/blogspot/symfonyworld/thrift/server/MainService.java:9: package org.apache.thrift.scheme does not exist
import org.apache.thrift.scheme.IScheme;
/var/www/laughing-thrift-duck/src/com/blogspot/symfonyworld/thrift/server/MainService.java:10: package org.apache.thrift.scheme does not exist
import org.apache.thrift.scheme.SchemeFactory;
/var/www/laughing-thrift-duck/src/com/blogspot/symfonyworld/thrift/server/MainService.java:11: package org.apache.thrift.scheme does not exist
import org.apache.thrift.scheme.StandardScheme;
/var/www/laughing-thrift-duck/src/com/blogspot/symfonyworld/thrift/server/MainService.java:13: package org.apache.thrift.scheme does not exist
import org.apache.thrift.scheme.TupleScheme;
/var/www/laughing-thrift-duck/src/com/blogspot/symfonyworld/thrift/server/MainService.java:14: package org.apache.thrift.protocol does not exist
import org.apache.thrift.protocol.TTupleProtocol;
# and many many more...

主要问题是:包org.apache.thrift不存在 - 怎么来的?!

有人能指出我在将自定义jar导入项目时遇到了什么问题吗?我认为这与节俭没有任何关系,但是通常会导入罐子。

2 个答案:

答案 0 :(得分:9)

  

我从官方网站下载页面下载了0.8.0 gz版本的thrift并自行创建了jar存档(jar cf thrift-0.8.0.jar src / org)

你为什么要创建自己的jar?为什么用源文件创建它? jar库必须至少包含准备使用的字节码类。

你必须让thrift将自己编译成jar然后再使用它:

  • 浏览/lib/java
  • 在终端
  • 中运行ant
  • 检查构建文件夹,您会看到.jar
  • 使用那个

答案 1 :(得分:0)

当您使用命令jar cf thrift-0.8.0.jar src/org创建jar文件时,org包(及其下面)的类存储在src / org而不是org中,因此Java无法找到它。尝试使用此命令而不是jar cf thrift-0.8.0.jar -C src org,这应该产生正确的结构。

您可以通过运行jar tf thrift-0.8.0.jar进行验证并查看内容。