简体   繁体   English

在终端编译 Java 程序导致日志错误

[英]Compiling Java program in terminal causes log error

I am trying to compile a Java program in the terminal:我正在尝试在终端中编译一个 Java 程序:

Long version containing all the imports:包含所有导入的长版本:

javac -classpath bin:$OSPL_HOME/jar/dcpssaj5.jar:$OSPL_HOME/jar/dcpssaj5-sources.jar:$OSPL_HOME/jar/dcpssaj-osgi-bundle.jar:$OSPL_HOME/jar/dcpssaj-sources.jar:$OSPL_HOME/jar/dcpssaj.jar:$OSPL_HOME/jar/osplconf.jar:$OSPL_HOME/jar/tooling_common.jar:$OSPL_HOME/jar/dcpsprotobuf.jar:$OSPL_HOME/jar/cmdataadapter.jar:$OSPL_HOME/jar/cmapi.jar:$OSPL_HOME/jar/ddsface.jar:Libraries/httpclient-4.5.12.jar:Libraries/jersey-client-1.19.4.jar:Libraries/jersey-client-1.19.jar:Libraries/jersey-core-1.19.4.jar:Libraries/json-simple-1.1.1.jar:Libraries/json-simple-3.1.0.jar:Libraries/jsr311-api-1.1.1.jar:Libraries/mqtt-client-0.0.6.jar:Libraries/paho-mqtt-client-1.13.0.jar -sourcepath src/ src/afarcloud/Main/afarcloud_main.java

Without all the imports:没有所有的进口:

javac -classpath bin:(jar_files) -sourcepath src/ src/afarcloud/Main/afarcloud_main.java

I have added what I thought would be all the necessaries jars, however I get the following error related to a logger when I try to run the code on my Ubuntu computer:我添加了我认为所有必需的 jars,但是当我尝试在我的 Ubuntu 计算机上运行代码时,我收到以下与记录器相关的错误:

src/afarcloud/Log/UtilLog.java:3: error: package org.apache.log4j does not exist
import org.apache.log4j.Logger;
                       ^
src/afarcloud/Log/UtilLog.java:7: error: cannot find symbol
    private static Logger log = Logger.getLogger(UtilLog.class);
                   ^
  symbol:   class Logger
  location: class UtilLog
src/afarcloud/Log/UtilLog.java:7: error: cannot find symbol
    private static Logger log = Logger.getLogger(UtilLog.class);
                                ^
  symbol:   variable Logger
  location: class UtilLog
src/afarcloud/Log/UtilLog.java:10: error: cannot find symbol
        log = Logger.getLogger(clase);
              ^
  symbol:   variable Logger
  location: class UtilLog
4 errors

What confuses me is that there is no folder called "Log" under src/afarcloud, so I am pretty confused about how to proceed... Any help is highly appreciated, thank you.让我感到困惑的是,在 src/afarcloud 下没有名为“Log”的文件夹,所以我很困惑如何继续……非常感谢任何帮助,谢谢。

You're missing JAR dependency on org.apache.log4j.您缺少 JAR 对 org.apache.log4j 的依赖。

Of course, this JAR can have further dependencies.当然,这个JAR可以有更多的依赖。 This can be endless game.这可能是无休止的游戏。 Perhaps you can use Maven and mavenized project, which will take care of dependency tree for you.也许您可以使用 Maven 和 mavenized 项目,它将为您处理依赖关系树。

Looks that you're missing import statement.看起来您缺少导入语句。 As a conclusion, all Logger Objects cannot be find and any new instances cant be created.作为结论,无法找到所有 Logger 对象,也无法创建任何新实例。

Try to use import org.apache.log4j.Logger;尝试使用import org.apache.log4j.Logger; . .

Or import via CLI.或通过 CLI 导入。 https://logging.apache.org/log4j/2.x/download.html https://logging.apache.org/log4j/2.x/download.html

Instead of mentioning the jar files of a directory individually, you should use wildcard character * to specify all jars of the directory eg而不是单独提及目录的 jar 文件,您应该使用通配符*指定目录的所有 jars 例如

javac -d bin -cp .:$OSPL_HOME/jar/*:Libraries/*:lib/* -sourcepath src src/afarcloud/Main/afarcloud_main.java

where在哪里

  1. -d bin specifies the directory where the compiled class will be put. -d bin指定编译后的 class 的存放目录。
  2. . with -cp or -classpath includes the current directory in the classpath.使用-cp-classpath将当前目录包含在类路径中。
  3. : specifies the separator to use multiple locations in the classpath. :指定在类路径中使用多个位置的分隔符。
  4. $OSPL_HOME/jar/* specifies all the files in the directory, $OSPL_HOME/jar . $OSPL_HOME/jar/*指定目录$OSPL_HOME/jar中的所有文件。 Alternatively, you can use $OSPL_HOME/jar/*.jar to specify just jar files.或者,您可以使用$OSPL_HOME/jar/*.jar仅指定 jar 文件。

Note: Make sure you also specify the location of the apache-logging-log4j.jar in your command because the error shows that you have missed to include this jar.注意:确保您还在命令中指定了apache-logging-log4j.jar的位置,因为错误表明您错过了包含此 jar。

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

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