Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java package - class not found, same directory

Tags:

java

I have two Java classes "giveMyOb" and "dataConn" declared in the same directory. Both are public classes. "giveMyOb" has a static method "getMine()". Inside dataConn, I called the static method as

giveMyOb.getMine();

When I try to compile dataConn.java, the following error is returned.

"Cannot find symbol

symbol: variable giveMyOb
location : class dataConn
giveMyOb.getMine(); "

It used to work earlier. But is not working now. Why is that?

Additional Information: JDK 1.6. Windows 7. 64 bit.

Update(30 days after the question): When compiled from Eclipse, the classes are referenced and it works. But the same won't work when compiling from command line. I was unable to figure out the reason and nothing logical comes to my mind!

like image 481
Sundeep Avatar asked May 20 '26 06:05

Sundeep


2 Answers

javac -classpath . *.java

ought to create both .class files at the same time. It's more complicated by packages. I'm assuming you have none.

Learn the Sun Java coding conventions. You aren't following them with those class names. They should start with a capital letter.

like image 186
duffymo Avatar answered May 21 '26 18:05

duffymo


Try this:

giveMyOb.java

public class giveMyOb {
    public static String getMine() {
        return "Yay, it works!";
    }
}

dataConn.java

public class dataConn {
    public static void main(String[] args) {
        System.out.println(giveMyOb.getMine());
    }
}

Then compile it all:

javac *.java

and run the main class:

java -cp . dataConn
// output: Yay, it works!

Note that Java's coding conventions recommend class names start with a capital.

If "it" still doesn't work, try removing the .class files manually then recompile again.

like image 41
Bart Kiers Avatar answered May 21 '26 18:05

Bart Kiers



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!