Java, Import Class From File - java

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.

Related

basic java programming (naming a file)

i got a run-time error on executing a file 'complement.java' that read
D:\java\files\java complement
Error:Could not find or load main class complement
the class structure follows as below
import java.io.*;
class billion
{
.
.
.
//class definition
}
what could be the cause for the prompt mentioned above?
First of all, this question can be easily answered by 5 seconds of quick googling, supposing you dont know java.
Anyway, the error only shows when your main class is missing a main method, which start the whole program. The main method is
public static void main(String[] args) {}
Add this to your main class, and start it again.
File name and class name need to be same. And the class you mention in java command should have a main method.
public static void main(String[] args) {}

How to run a Java HelloWorld program?

I have run into an error. I think my code is right. here it is... please tell me what I have done wrong!!!
public class HelloWorld {
public static void main(String[] args) {
System.out.println("Hello, World");
}
}
The error I keep getting is...
Error: Could not find or load main class javatest.java
The file name of the Java Class file must be the same one as your Class.
Class: public class HelloWorld {}
File: HelloWorld.java
The code seems right. Perhaps the issue in how you run it.
The convention is to keep the file name and the public class name as the same.
When compiling, use javac <filename>.java. Then to run simply use java <classname>. So, you would use java HelloWorld, HelloWorld being the class that contains the main method.

javac gives no warning when a class with default access contains public members

Regarding the example code below, although the Test() constructor inside the class Test is public, the class Test itself isn't public, and so the Test() constructor can't be called from outside its own package.
Does that make the public keyword redundant? If so, I wonder why javac doesn't issue a warning about the redundant use of public, when used inside a class whose access is implicitly declared as default ("package private")?
Test.java, package test -
package test;
class Test {
public Test() {}
}
Main.java, package main -
package main;
class Main {
public static void main(String[] args) {
new test.Test(); // Expected error
}
}
EDIT:
Just to be clear: it's when I compile Test.java, that I get no warning.
The user comments made to my original post have helped me solve this problem: I now realise that an IDE will give me the information that I was after, and that just using javac on its own will not. Thanks.

main method with class name and file name

My file name is Temp.java and inside it I have this. I'm using eclipse IDE
/*package*/ class Test {
public static void main(String args[]) {
System.out.println("test");
}
}
So I was unable to run this as java application. I change my class name to Temp
class Temp {
....
}
Now I can. Can someone explain me why ?
This is probably a limitation of Eclipse. The code runs well from command line.
As I understand, you are trying to embed your unit tests in the same file with the class under test. This is a nice idea and I totally concur with it. You can read more about how you can succeed in Ben J. Christensen's blog post. Generally, he suggests placing the tests in a static inner class, not a standalone class in the same file.
An example from the Netflix Hystrix framework: HystrixCircuitBreaker.UnitTest
The code below, located in Temp.java, compiles and runs fine with Netbeans:
class Whatever {
public static void main(String[] args) {
System.out.println("hello");
}
}
The problem is with eclipse, i think you are trying to run using right click -> run as -> Java Application, unfortunately eclipse is not showing this option if the class is not public.
But you can still run the class using Alt+Shift+X,J.
Its not the problem with Java, its with Eclipse.
The name of the file should be the same as the class name which is public and has the main() method. In your first case the file name Temp.java will compile and will create Test.class file not Temp.class because there is no Temp class declared in your file.
after .class file is created , run it with java Test
so here's an example
//Filename abc.java
public class hi
{
public static void main(String[] args)
{
System.out.println("Hell");
}
}
the output
abc.java:1: class hi is public, should be declared in a file named hi.java
public class hi
^
1 error
but if you do this
//Filename abc.java
class hi
{
public static void main(String[] args)
{
System.out.println("Hell");
}
}
it will create hi.class file so
D:\>java hi
Hell
The class (which main should be run) inside the .java file must have the same name as the file. If the class is not public (as in your case) the class will compile but it can't be run since Eclipse tries to load the class according to the file name.

Java: Why does adding the package specification to my class cause a run-time error?

So, my code is pretty simple. Just wanted to try and create a package.
// /home/user1/Code/packageTest/src/myPackage/Test.java
package myPackage;
public class Test
{
public static void main(String[] args)
{
System.out.println("Hello, world.");
}
}
let's say this code is in some directory myPackage.
If I comment out the first line (package specification) the code runs fine, prints the message. It compiles either way, but if it compiled with the package line not commented out, it causes a run-time error.
What do I need to do to successfully make a package? I can't seem to find the right search terms to turn up a real explanation on this, only stuff about package naming conventions, why they're used to separate namespaces, bla bla bla. The next part of this experiment was going to be trying import my own packages, obviously I didn't even get that far.
Something to do with the classpath perhaps...? What and where is my "base directory"? Any information on this would be greatly appreciated.
You should create the class in a java file in a directory with name myPackage. Then you should come out of that directory and compile it as follows
javac myPackage/Test.java
Then run it with fully-qualified-class-name (FQCN) as follows:
java myPackage.Test
E.g.
C:\Temp\test1>type myPackage\Test.java
package myPackage;
public class Test
{
public static void main(String[] args)
{
System.out.println("Hello, world.");
}
}
C:\Temp\test1>javac myPackage/Test.java
C:\Temp\test1>java myPackage.Test
Hello, world.
C:\Temp\test1>

Categories

Resources