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). 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):
import helping.Dbg;
public class LaunchProgram{
public static void main(String[] args){
Dbg("Testing");
}
}
The contents of 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
.
When I do javac LaunchProgram.java
in cmd, I get the following error:
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?
Dbg
is a class, not a method, and as it's a helper class it won't have a main()
method of its own. Rather it should hav something like a log
method that does the logging and is called by the other class.
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
:
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. 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. Here is a skeleton code for your LaunchProgram
class which uses log4j to log a message:
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.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.