用外部库在linux上编译java程序

时间:2015-05-31 15:13:04

标签: java linux compilation smack

我正在尝试使用SMACK为XMPP库编译一个java文件。在java文件的文件夹中,我已经包含了一个Libs目录,包括SMACK的所有* .jar文件,但是当我尝试时

javac filename

我得到了这个输出:

SmackCcsClient.java:6: error: package org.jivesoftware.smack does not exist
import org.jivesoftware.smack.ConnectionConfiguration;
                             ^
SmackCcsClient.java:7: error: package org.jivesoftware.smack.ConnectionConfiguration does not exist
import org.jivesoftware.smack.ConnectionConfiguration.SecurityMode;
                                                     ^
SmackCcsClient.java:8: error: package org.jivesoftware.smack does not exist
import org.jivesoftware.smack.ConnectionListener;
                             ^
SmackCcsClient.java:9: error: package org.jivesoftware.smack does not exist
import org.jivesoftware.smack.PacketInterceptor;
                             ^
SmackCcsClient.java:10: error: package org.jivesoftware.smack does not exist
import org.jivesoftware.smack.PacketListener;
                             ^
SmackCcsClient.java:11: error: package org.jivesoftware.smack does not exist
import org.jivesoftware.smack.XMPPConnection;
                             ^
SmackCcsClient.java:12: error: package org.jivesoftware.smack does not exist
import org.jivesoftware.smack.XMPPException;
                             ^
SmackCcsClient.java:13: error: package org.jivesoftware.smack.filter does not exist
import org.jivesoftware.smack.filter.PacketTypeFilter;
                                    ^
SmackCcsClient.java:14: error: package org.jivesoftware.smack.packet does not exist
import org.jivesoftware.smack.packet.DefaultPacketExtension;
                                    ^
SmackCcsClient.java:15: error: package org.jivesoftware.smack.packet does not exist
import org.jivesoftware.smack.packet.Message;
                                    ^
SmackCcsClient.java:16: error: package org.jivesoftware.smack.packet does not exist
import org.jivesoftware.smack.packet.Packet;
                                    ^
SmackCcsClient.java:17: error: package org.jivesoftware.smack.packet does not exist
import org.jivesoftware.smack.packet.PacketExtension;
                                    ^
SmackCcsClient.java:18: error: package org.jivesoftware.smack.provider does not exist
import org.jivesoftware.smack.provider.PacketExtensionProvider;
                                      ^
SmackCcsClient.java:19: error: package org.jivesoftware.smack.provider does not exist
import org.jivesoftware.smack.provider.ProviderManager;
                                      ^
SmackCcsClient.java:20: error: package org.jivesoftware.smack.util does not exist
import org.jivesoftware.smack.util.StringUtils;
                                  ^
SmackCcsClient.java:21: error: package org.json.simple does not exist
import org.json.simple.JSONValue;
                      ^
SmackCcsClient.java:22: error: package org.json.simple.parser does not exist
import org.json.simple.parser.ParseException;
                             ^
SmackCcsClient.java:23: error: package org.xmlpull.v1 does not exist
import org.xmlpull.v1.XmlPullParser;
                     ^
SmackCcsClient.java:52: error: cannot find symbol
    XMPPConnection connection;
    ^
  symbol:   class XMPPConnection
  location: class SmackCcsClient
SmackCcsClient.java:53: error: cannot find symbol
    ConnectionConfiguration config;
    ^
  symbol:   class ConnectionConfiguration
  location: class SmackCcsClient
SmackCcsClient.java:306: error: cannot find symbol
    public void connect(String username, String password) throws XMPPException {
                                                                 ^
  symbol:   class XMPPException
  location: class SmackCcsClient
SmackCcsClient.java:58: error: cannot find symbol
    class GcmPacketExtension extends DefaultPacketExtension {
                                     ^
  symbol:   class DefaultPacketExtension
  location: class SmackCcsClient
SmackCcsClient.java:77: error: cannot find symbol
        public Packet toPacket() {
               ^
  symbol:   class Packet
  location: class SmackCcsClient.GcmPacketExtension
SmackCcsClient.java:70: error: method does not override or implement a method from a supertype
        @Override
        ^
SmackCcsClient.java:78: error: cannot find symbol
            return new Message() {
                       ^
  symbol:   class Message
  location: class SmackCcsClient.GcmPacketExtension
SmackCcsClient.java:117: error: cannot find symbol
                GCM_NAMESPACE, new PacketExtensionProvider() {
                                   ^
  symbol:   class PacketExtensionProvider
  location: class SmackCcsClient
SmackCcsClient.java:116: error: cannot find symbol
        ProviderManager.getInstance().addExtensionProvider(GCM_ELEMENT_NAME,
        ^
  symbol:   variable ProviderManager
  location: class SmackCcsClient
SmackCcsClient.java:145: error: cannot find symbol
        Packet request = new GcmPacketExtension(jsonRequest).toPacket();
        ^
  symbol:   class Packet
  location: class SmackCcsClient
SmackCcsClient.java:276: error: cannot find symbol
        return JSONValue.toJSONString(message);
               ^
  symbol:   variable JSONValue
  location: class SmackCcsClient
SmackCcsClient.java:294: error: cannot find symbol
        return JSONValue.toJSONString(message);
               ^
  symbol:   variable JSONValue
  location: class SmackCcsClient
SmackCcsClient.java:307: error: cannot find symbol
        config = new ConnectionConfiguration(GCM_SERVER, GCM_PORT);
                     ^
  symbol:   class ConnectionConfiguration
  location: class SmackCcsClient
SmackCcsClient.java:308: error: cannot find symbol
        config.setSecurityMode(SecurityMode.enabled);
                               ^
  symbol:   variable SecurityMode
  location: class SmackCcsClient
SmackCcsClient.java:319: error: cannot find symbol
        XMPPConnection.DEBUG_ENABLED = true;
        ^
  symbol:   variable XMPPConnection
  location: class SmackCcsClient
SmackCcsClient.java:321: error: cannot find symbol
        connection = new XMPPConnection(config);
                         ^
  symbol:   class XMPPConnection
  location: class SmackCcsClient
SmackCcsClient.java:324: error: cannot find symbol
        connection.addConnectionListener(new ConnectionListener() {
                                             ^
  symbol:   class ConnectionListener
  location: class SmackCcsClient
SmackCcsClient.java:353: error: cannot find symbol
        connection.addPacketListener(new PacketListener() {
                                         ^
  symbol:   class PacketListener
  location: class SmackCcsClient
SmackCcsClient.java:397: error: cannot find symbol
        }, new PacketTypeFilter(Message.class));
               ^
  symbol:   class PacketTypeFilter
  location: class SmackCcsClient
SmackCcsClient.java:397: error: cannot find symbol
        }, new PacketTypeFilter(Message.class));
                                ^
  symbol:   class Message
  location: class SmackCcsClient
SmackCcsClient.java:400: error: cannot find symbol
        connection.addPacketInterceptor(new PacketInterceptor() {
                                            ^
  symbol:   class PacketInterceptor
  location: class SmackCcsClient
SmackCcsClient.java:405: error: cannot find symbol
        }, new PacketTypeFilter(Message.class));
               ^
  symbol:   class PacketTypeFilter
  location: class SmackCcsClient
SmackCcsClient.java:405: error: cannot find symbol
        }, new PacketTypeFilter(Message.class));
                                ^
  symbol:   class Message
  location: class SmackCcsClient
SmackCcsClient.java:447: error: cannot find symbol
    } catch (XMPPException e) {
             ^
  symbol:   class XMPPException
  location: class SmackCcsClient
42 errors

如何正确编译? smack库需要一些额外的命令吗?

更新:

我创建了以下文件夹结构:

root
->bin
->src
->lib

我尝试使用不同的javac命令进行编译,如:

javac -d bin -sourcepath src -cp :lib src/SmackCcsClient.java

我尝试了各种变化,但每次都会得到相同的输出。

更新2:

运行命令:

javac -d bin -sourcepath src -cp lib/*.jar src/SmackCcsClient.java

给出了以下输出:

javac: invalid flag: lib/smack-android-4.1.1-javadoc.jar
Usage: javac <options> <source files>
use -help for a list of possible options

我正在学习不同的教程,但我无法弄清楚如何正确地做到这一点。

1 个答案:

答案 0 :(得分:3)

正确的命令是(假设所有需要的.jar文件实际上在lib/文件夹中,并且所有源都在src/文件夹中:

javac -d bin -sourcepath src -cp $(find lib -iname *.jar | xargs | tr " " ":") src/SmackCcsClient.java

棘手的部分($( )内)正在a.jar:b.jar:c.jar文件夹中的所有jar文件中构建类似:(没有空格,由lib/分隔)的内容。您使用-cp lib/*.jar的尝试失败,因为有空格而不是:分隔jar文件,而javac认为它找到的第二个jar文件实际上是您要编译的java源代码。然后抱怨它不知道如何编译jar文件。

更简单的版本(依赖于lib/中没有子目录)是

javac -d bin -sourcepath src -cp $(echo lib/*.jar | tr " " ":") src/SmackCcsClient.java