java 包 org.apache.thrift 不存在
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15435764/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
package org.apache.thrift does not exist
提问by ducin
I'm trying to write a basic java application that would use thrift. The thing I'm missing is a working jar file.
我正在尝试编写一个使用 thrift 的基本 Java 应用程序。我缺少的是一个有效的 jar 文件。
I've downloaded 0.8.0 gz version of thrift from the official website download pages and created the jar archive on my own (jar cf thrift-0.8.0.jar src/org
). I've moved the new jar file into my project directory. Then, I've added this jar file manually to the project - I'm working in NetBeans IDE. Everything seems as it should work, but it doesn't - red error messages occur everywhere I want to use thrift libs. This is piece of my code:
我已经从官网下载页面下载了 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;
}
}
and the other file:
和另一个文件:
/**
* 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;
}
When I run build, I get following errors:
当我运行构建时,出现以下错误:
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...
The main question is: package org.apache.thrift does not exist- how come?!
主要问题是:包 org.apache.thrift 不存在- 怎么会?!
Can someone point me on what did I do wrong with importing a custom jar into my project? I think this has nothing to do with thrift precisely, but with importing jars in general.
有人可以指出我在将自定义 jar 导入我的项目时做错了什么吗?我认为这与节俭无关,而是与一般的进口罐子有关。
回答by Hyman
I've downloaded 0.8.0 gz version of thrift from the official website download pages and created the jar archive on my own (jar cf thrift-0.8.0.jar src/org)
我已经从官网下载页面下载了 0.8.0 gz 版本的 thrift 并自己创建了 jar 存档(jar cf thrift-0.8.0.jar src/org)
Why did you create your own jar? And why you created it with source files? A jar library must contain at least the bytecode classes, ready to be used.
你为什么要创建自己的罐子?为什么用源文件创建它?一个 jar 库必须至少包含准备使用的字节码类。
You must let thrift compile itself into a jar and then use it:
你必须让 thrift 自己编译成一个 jar 然后使用它:
- browse to
/lib/java
- run
ant
in terminal - check the build folder and you will see a
.jar
- use that one
- 浏览到
/lib/java
ant
在终端运行- 检查构建文件夹,您将看到一个
.jar
- 使用那个
回答by Raymond Tau
When you use the command jar cf thrift-0.8.0.jar src/org
to create the jar file, the classes for the org package (and under it) are stored in the src/org instead of org, so Java can't find it. Try to use this command instead jar cf thrift-0.8.0.jar -C src org
, which should produce the correct structure.
当您使用该命令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
,它应该会产生正确的结构。
You may verify it by running jar tf thrift-0.8.0.jar
and see the content.
您可以通过运行来验证它jar tf thrift-0.8.0.jar
并查看内容。