Java: Import error within single directory? - java

I'm new to Java.
All of my source files (eg. TreeJPanel.java, Tree.java) are in a single directory called jview, with dependencies between them. When I try to compile with javac jview/TreeJPanel.java I get this:
jview/TreeJPanel.java:39: cannot find symbol
symbol : class Tree
location: class TreeJPanel
protected Tree tree;
^
jview/TreeJPanel.java:41: cannot find symbol
symbol : class Tree
location: class TreeJPanel
public Tree getTree() {
^
jview/TreeJPanel.java:45: cannot find symbol
symbol : class Tree
location: class TreeJPanel
public void setTree(Tree tree) {
There's 15 similar errors. I thought I don't need explicit imports from within the same directory? What am I doing wrong? It is likely my question reveals a lack of conceptual understanding of Java - please feel free to point out. Thanks!

Was Tree.java first compiled into Tree.class? when TreeJPanel.java was trying to compile, it was searching for it.

Try compiling both files together:
javac jview/Tree.java jview/TreeJPanel.java

Cause for this is very simple just u should import the Tree Class into the TreeJpanel Class
Your code should look like this
import jview.Tree;
Class TreeJPanel { ....

Related

Compiling java project with multiple dirs and dependency hierarchy

Note: I can successfully compile everything else besides RegisterTest.java
I have the following project structure. I want to compile this using javac only, no build tools, no IDEs.
Each file is in a package corresponding to their directory structure, for example Drink.java is in Kassensystem/AbstractProducts which translates to package Kassensystem.AbstractProducts;
Project Structure
All abstract products implement the common interface Product which is in the Interfaces folder, so they naturally import Kassensystem.Interfaces.*. Finally the Products are the concrete classes derived from the abstract product classes, so they in turn import Kassensystem.AbstractProducts.*. Finally the Register class just calculates some prices and only depends on the Product interface. RegisterTest should test the whole system and instantiates some concrete classes and passes them to Register.
So the dependency hierarchy is as follows:
Dependency Hierarchy
I'm having trouble compiling this, how would I need to set the classpath to get the right dependency order (note I'm not using jar files)?
I tried setting the classpath such that the most abstract classes come first (Interfaces -> Abstract Classes -> Concrete Products -> Register) but it's always complaining at the import statements, that the package does not exist.
$ javac -cp "Interfaces/*.class:AbstractProducts/*.class:Products/*.class" RegisterTest.java
RegisterTest.java:3: error: package Kassensystem.Products does not exist
import Kassensystem.Products.*;
^
RegisterTest.java:4: error: package Kassensystem.Interfaces does not exist
import Kassensystem.Interfaces.*;
^
RegisterTest.java:8: error: cannot find symbol
var products = new Product[] { new Tomato(), new TomatoJuice() };
^
symbol: class Product
location: class RegisterTest
RegisterTest.java:8: error: cannot find symbol
var products = new Product[] { new Tomato(), new TomatoJuice() };
^
symbol: class Tomato
location: class RegisterTest
RegisterTest.java:8: error: cannot find symbol
var products = new Product[] { new Tomato(), new TomatoJuice() };
^
symbol: class TomatoJuice
location: class RegisterTest
RegisterTest.java:10: error: cannot find symbol
double total = Register.scan(products);
^
symbol: variable Register
location: class RegisterTest
6 errors

Compiling Errors in Netbeans with JFreeChart

When I run my project in Netbeans 8.1 nothing goes wrong. However, when I build it to a .jar file, there are 34 errors of missing packages and symbols all referring to JFreeChart. Couple of these errors:
C:NetBeansProjects\Program\src\org\jfree\chart\servlet\ChartDeleter.java:51: error: package javax.servlet.http does not exist
import javax.servlet.http.HttpSessionBindingEvent;
C:NetBeansProjects\Program\src\org\jfree\chart\servlet\ChartDeleter.java:52: error: package javax.servlet.http does not exist
import javax.servlet.http.HttpSessionBindingListener;
C:\NetBeansProjects\Program\src\org\jfree\chart\servlet\ChartDeleter.java:58: error: cannot find symbol
public class ChartDeleter implements HttpSessionBindingListener, Serializable {
symbol: class HttpSessionBindingListener
C:\NetBeansProjects\Program\src\org\jfree\chart\servlet\ChartDeleter.java:98: error: cannot find symbol
public void valueBound(HttpSessionBindingEvent event) {
symbol: class HttpSessionBindingEvent
location: class ChartDeleter
etc.....
My code is too long to post here (6000+ lines) and contains Java swing and some charts. Everything worked fine, but the charts made these errors appear. What's the reason for this?
"javax.servlet.http does not exist", add servletapi.jar to your classpath

Interface Implementation error : cannot find symbol

I am implementing the following sample interface:
package test1;
public interface MotorVehicle {
void run();
int getFuel();
}
In the class
package test1;
import test1.MotorVehicle;
public class Car implements MotorVehicle
{
int fuel;
public void run(){
System.out.println("Running");
}
public int getFuel(){
return this.fuel;
}
}
When I try to compile the class file , I get the following error :
Car.java:4: error: cannot find symbol
public class Car implements MotorVehicle
^
symbol: class MotorVehicle
1 error
Compile Steps:
Step:1 javac MotorVehicle.java
Step:2 javac Car.java
Both my interface and the class are in the same directory , why does ut come up with cannot find symbol error?
Edit:
As suggested , have changed the package , and tried to run the same code again . Still getting an error.
The problem is that you're in the wrong folder when compiling.
From the console screenshot, it is clear that you are inside /test1. However, the package test1; statement expects a folder inside the current folder named test1. It can't find that folder/package, so you get an error.
The solution is to go up one folder, so you end up in /src, then compile using the path to the file, e.g. javac test1/Car.java. Explanation: You are in the folder /src, the package statement inside the classes says they are inside the folder test1 which is inside /src. Now every package/path can be resolved.
And you shouldn't import things that are in the same package.
First of all as your package name is test you must keep your class and the interface in a folder named test.
Second thing since they are in the same folder named test remove import test.MotorVehicle; from the class defination
Suppose if your folder test resides in g:/ such that g:/test/contains class and the interface.
Then try opening the command prompt in g:/
then type the following commands
for compiling
javac test/Car.java
and for executing
java test.Car
Though you may get Error: Main method not found in class test.Car
as your class does not contain main mathod
You are going in to exact path by the use of cd command.Because of that interface is not accessible as class will try to find out it from package from current/running location.
For make this compile you have to specify fully (again Fully) qualified name of package during compilation.
For Example
If you class is in a.b.test package compile it like this
javac a/b/test/Car.java
First compile MotorVehicle as it doesn't have any dependencies. Then set the classpath
Before issuing javac Car.java compile statements you need to set the Classpath
Windows
set CLASSPATH=%CLASSPATH%;<PATH_TO_COMPILED_BINARY>/
Unix
export CLASSPATH=$CLASSPATH:<PATH_TO_COMPILED_BINARY>/
<PATH_TO_COMPILED_BINARY> should not include the package test1
Example :
C:/sourcecode/test1
Then <PATH_TO_COMPILED_BINARY> should be C:/sourcecode
Update
Removing the import test1.MotorVehicle will also fix the issue.
After Compiling Motorvehicle.java. you have to create a folder test1 and transfer the MotorVehicle.class into the folder test1 then compile the next file Car.java. This will solve your error

cannot find symbol error while compiling a java class containing other class type object

I have a java package which contains two classes. Class A and Class B. I need to create an object of A type in class B. I don't know what is happening. Please someone help me out.
package pack;
class A
class B
I'm using JDK1.5 and tomcat and placed them in java folder in my D drive.
D:\java\jdk1.5
D:\java\tomcat
Right now, my package folder is also in above location
D:\java\pack
Below is how i am compiling my java class files.
Step 1: Compiling A.java
D:\Java\pack>set path=D:\java\jdk1.5\bin (setting up path for jdk1.5 compiler)
D:\Java\pack>javac A.java (Successfuly compiled and formed A.class)
Step 1: Compiling B.java
D:\Java\pack>javac B.java (here, i get an error message )
Below is the ERROR message
Error Message
D:\Java\pack>javac B.java
B.java:9: cannot find symbol
symbol : class A
location: class pack.B
A a = new A(); //creating an object of A type
^
B.java:9: cannot find symbol
symbol : class A
location: class pack.B
A a = new A(); //creating an object of A type
^
2 errors
javac pack\A.java pack\B.java
will do the trick. The compiler has to be able to resolve everything in one invocation. If it's looking for
pack.B
then that corresponds to
pack\B.java
in the directory structure

java import user defined package - error: cannot find symbol

My directory structure is as follows:
/WorkingDirectory
MyCollection.java
/au/edu/au
/UserInterface
UserInterface.java
/Collection
Album.java
CDAlbum.java
DVDAlbum.java
CollectionFactory.java
Where Album.java is an interface implemented by CDAlbum.java and DVDAlbum.java. Each .java file has the appropriate
package au.edu.uow.UserInterface;
or
package au.edu.uow.Collection;
line.
In UserInterface.java I declare an ArrayList of type Album, which gives me compile errors of
.\au\edu\uow\UserInterface\UserInterface.java:9: error: cannot find symbol
private ArrayList<Album> myCollection;
^
symbol: class Album
location: class UserInterface
I am compiling from MyCollection.java, which has import statements for both packages.
what is causing this error? I tried adding
import au.edu.uow.Collection
to UserInterface.java, but to no avail.
You need either:
import au.edu.uow.Collection.*;
or:
import au.edu.uow.Collection.Album;
Using wildcard imports is more convenient but some consider this a bad practice. This is a matter of style; decide for yourself.
It is also considered good style for package names to be in all-lower-case, although this is not enforced by the compiler.
So, you folder structure looks like /au/edu/au & your trying to import from au.edu.uow
Apart from what JimN has suggested, I think you'll find that Java will complain that te package doesn't exist or the files in /au/edu/au are in the wrong package
Either change the directory structure or package structure to match each other

Categories