简体   繁体   English

使用外部库在Linux上编译Java程序

[英]Compile java program on linux with external libraries

I am trying to compile a java file using SMACK for the XMPP libraries. 我正在尝试使用SMACK为XMPP库编译Java文件。 In the folder of the java file i have included a Libs directory including all the *.jar files for SMACK but when i try 在java文件的文件夹中,我包括一个Libs目录,其中包括用于SMACK的所有* .jar文件,但是当我尝试时

javac filename

i get this output: 我得到这个输出:

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

how do i compile this correctly? 我该如何正确编译? is there some extra command needed for the smack library? smack库是否需要一些额外的命令?

UPDATE: 更新:

i have created the following folder structure: 我创建了以下文件夹结构:

root
->bin
->src
->lib

I have tried compiling with different javac commands like: 我尝试使用不同的javac命令进行编译,例如:

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

I have tried various variations but i keep getting the same output every time. 我尝试了各种变化,但是每次都保持相同的输出。

UPDATE 2: 更新2:

running the command: 运行命令:

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

gave the following output: 给出以下输出:

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

I am following different tutorials but i cannot figure out how to do this correctly. 我正在遵循不同的教程,但我无法弄清楚如何正确执行此操作。

A correct command would be (assuming all needed .jar files are actually in the lib/ folder, and all sources are in the src/ folder): 正确的命令是(假设所有需要的.jar文件实际上位于lib/文件夹中,而所有源均位于src/文件夹中):

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

The tricky part (within $( ) ) is building something like a.jar:b.jar:c.jar (no spaces, separated by : ) from all the jar-files in the lib/ folder. 棘手的部分(在$( )内)从lib/文件夹中的所有jar文件构建类似a.jar:b.jar:c.jar (无空格,用:分隔)的东西。 Your attempt using -cp lib/*.jar failed because there were spaces instead of : separating the jar-files, and javac thought that the second jar-file it found was actually java source that you wanted to compile. 您使用-cp lib/*.jar尝试失败了,因为使用空格而不是:分隔jar文件,而javac认为找到的第二个jar文件实际上是您想要编译的Java源代码。 And then complained that it did not know how to compile jar-files. 然后抱怨说它不知道如何编译jar文件。

A simpler version (that relies on there being no sub-directories in lib/ ) is 一个更简单的版本(依赖于lib/没有子目录)是

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM