简体   繁体   English

Java,从文件导入类

[英]Java, Import Class From File

I'm writing a java program and wanted to make a class that can be called on to make a quick log to the cmd (I'm still in my testing phase, figuring stuff out). 我正在编写一个Java程序,想要创建一个可以调用的类以快速登录到cmd(我仍处于测试阶段,正在弄清楚东西)。 I have a file and a folder with another file in it. 我有一个文件和一个文件夹,其中另一个文件。

  • LaunchProgram.java
  • helping
    • Dbg.class
    • Dbg.java

The summarized contents of LaunchProgram.class (the stuff that is relevant): LaunchProgram.class的摘要内容(相关的内容):

import helping.Dbg;

public class LaunchProgram{
    public static void main(String[] args){
        Dbg("Testing");
    }
}

The contents of Dbg.class : Dbg.class的内容:

package helping;

public class Dbg{
    public static void main(String message){
        System.out.println(message);
    }
}

When I do javac Dbg.java in cmd, it runs without any error, producing Dbg.class . 当我在cmd中执行javac Dbg.java时,它运行无误,生成Dbg.class

When I do javac LaunchProgram.java in cmd, I get the following error: 当我在cmd中执行javac LaunchProgram.java时,出现以下错误:

LaunchProgram.java:5: error: cannot find symbol
                Dbg("Testing");
                ^
symbol:   method Dbg(String)
location: class LaunchProgram

I'm not sure what's happened to cause this, and I've looked everywhere about this but can't find a solution. 我不确定是什么原因导致的,我已经到处都在寻找,但是找不到解决方案。 Does anyone know what is causing this issue and how to fix it? 有谁知道导致此问题的原因以及如何解决此问题?

  1. Dbg is a class, not a method, and as it's a helper class it won't have a main() method of its own. Dbg是一个类,而不是方法,并且它是一个帮助器类,因此它没有自己的main()方法。 Rather it should hav something like a log method that does the logging and is called by the other class. 相反,它应该具有类似于log方法的功能,该方法执行日志记录并由其他类调用。

  2. I suspect you're not compiling the code correctly. 我怀疑您没有正确编译代码。 You need to do this, in the directory that contains both LaunchProgram.java and the directory helping : 你需要做到这一点,同时包含目录LaunchProgram.java和目录helping

     javac helping/Dbg.java javac LaunchProgram.java 

    Actually you don't need the first line at all. 实际上,您根本不需要第一行。 The second line will compile both classes. 第二行将编译两个类。 Both commands will put the corresponding .class files into the right directories. 这两个命令都会将相应的.class文件放入正确的目录中。 Then to run it: 然后运行它:

     java LaunchProgram 

Basically you should always be in the directory that is at the head of the package structure. 基本上,您应该始终位于包结构开头的目录中。

Here is corrected code for what you were trying to do: 这是您尝试执行的更正代码:

public class LaunchProgram {
    public static void main(String[] args){
        Dbg.log("Testing");
    }
}

public class Dbg {
    public static void log(String message){
        System.out.println(message);
    }
}

But Apache log4j is a much better way to do logging in your application. 但是Apache log4j是在应用程序中进行记录的更好的方法。 Here is a skeleton code for your LaunchProgram class which uses log4j to log a message: 这是您的LaunchProgram类的框架代码,它使用log4j记录消息:

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

public class LaunchProgram {
    static final Logger logger = LogManager.getLogger(LaunchProgram.class.getName());

    public static void main(String[] args){
        logger.info("Testing");
    }
}

Note that you don't need a separate class to log, but rather you can log directly from the class you need to record a message. 请注意,您不需要单独的类来进行记录,而是可以直接从需要记录消息的类中进行记录。

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

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