Solved

Importing packages/classes

Posted on 2002-03-19
12
223 Views
Last Modified: 2013-11-23
I have 2 java classes.

The first (1):
package editor;
public class Edt
{
     public Edt() {
          System.out.println("Constructor EDT()");
     }

     public void say(String s) {
          System.out.println(s);
     }
}

Second (2):
import editor.*;
public class UseEdt
{
     public static void main(String[] args) {
          Edt e = new Edt();

          e.say("Hello World");
     }
}
If I use in (2) import editor.*; I have the compiler error "cannot resolve symbol: constructor Edt()" and "cannot resolve symbol: method say()".

If I use in (2) import editor.Edt; all works fine.

What happens?
0
Comment
Question by:sandro99
  • 4
  • 2
  • 2
  • +4
12 Comments
 
LVL 9

Expert Comment

by:Venci75
ID: 6879579
How did you compile these classes? Are you using an IDE for this? I think that in your first attempt to compile your class (2) the class (1) was not compiled. But when you used the second way to import this class - the tool you are using understood that this class had to be compiled.
Did you try to compile (2) with import editor.*; after you had compiled it with import editor.Edt;? I think it will compile.
0
 
LVL 4

Expert Comment

by:m_onkey_boy
ID: 6880581
You don't have to import the package a class is in.  Just remove the import editor.*; line as it is not needed.
0
 
LVL 92

Expert Comment

by:objects
ID: 6881198
What directories are the files in?
Edt.java should be in a directory editor.
Also what is your classpath set to?
0
 

Expert Comment

by:cini_gm
ID: 6882239
First you create one directory called editor. For eg: create editor directory in d:\java. Save Edt.java file into that directory. ie, editor directory.
 In the compile time goto the editor directory.
ie, d:\java\editor
 The compile Edt.java
  ie, javac Edt.java
Then save your UseEdt file in d:\java. In the compile time, goto d:\java directory and compile UseEdt.java class.
 ie, d:\java
     javac UseEdt.java
  Then run
    java UseEdt
0
 

Expert Comment

by:sandro901
ID: 6883951
cini_gm, you answer is right, can you response again to I rank you (I've lost some emails).

I have the 2 classes in the same directory.

I'm using JDK 1.4, I'm compiling using "javac -d . Edt.java" and "javac -d . UseEdt.java".

It puts the class Edt.class in the right directory (create then if doesn't exist), but doesn't work.

Somebody know what happens, I already have the solution, but don't understand why my way doesn't work.
0
 

Accepted Solution

by:
cini_gm earned 50 total points
ID: 6885119
No problem in saving the classes in the same directory. Suppose your java files are in d:\java. Go to that directory and compile Edt.java like javac -d . Edt.java.
Then one directory will create and store Edt.class in that directory.
 Then you set the path like this:
 set classpath=d:\java;
 Make one change in your UseEdt file.
 Instead of import editor.*;, pls give import editor.Edt;
 Then compile and run UseEdt.


0
6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

 
LVL 92

Expert Comment

by:objects
ID: 6886911
cini_gm,

 "javac -d . Edt.java" will save the the classfile in the directory d:\java\editor, not d:\java. Due to it being in the editor package.

sandro99,

Your problem is not caused by the import statement, both forms are correct. It sounds like the compiler can find the class files, but it cannot find the source files (if the class files don't exist). This is due to your use of the -d option and not storing source files in the correct directory.
0
 

Expert Comment

by:sandro901
ID: 6887116
It seems that you are writing without testing. Can someone test and write ?

I already have a solution, but I want to know why it happens, why I can't simply use "import editor.*".

My routine, suppose my java files are in d:\java, and is my current directory:

1 - write the 2 file .java in the directory d:\java;

2 - compile Edt.java with "javac -d . Edt.java"

3 - compile UseEdt.java with "javac -d . UseEdt.java" (the error happens)

My classpath is something like CLASSPATH=.;OTHER_DIRS, it had the current dir.

It seems to be a JDK bug ? Someone can use another compiler ?

Thanks all.
0
 
LVL 92

Expert Comment

by:objects
ID: 6887196
As I stated above your problem is because Edt.java is not in the directory 'editor'.

You can use "import editor.*", you just need to ensure your source is in the correct directory.

I don't think it is a JDK bug.

0
 
LVL 1

Expert Comment

by:saurav17
ID: 6888015


Hi sandro,

okay put your UseEdt at C: for instance.

Now, create a directory called editor at c:\\.

place ur package editor inside ur editor folder.


Now try what u want to do everything will work fine.

I tried this with ur program it works fine.

If u stil find any problem add ur package to ur classpath.


Regards,
K.J.S.
0
 
LVL 92

Expert Comment

by:objects
ID: 6889760
K.J.S,

That has already been said.
0
 

Expert Comment

by:SpideyMod
ID: 8289924
Force Accepted

SpideyMod
Community Support Moderator @Experts Exchange
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

708 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now