?
Solved

Importing packages/classes

Posted on 2002-03-19
12
Medium Priority
?
229 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Get real performance insights from real users

Key features:
- Total Pages Views and Load times
- Top Pages Viewed and Load Times
- Real Time Site Page Build Performance
- Users’ Browser and Platform Performance
- Geographic User Breakdown
- And more

 

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 200 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
 
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

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
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 …
Suggested Courses
Course of the Month15 days, 7 hours left to enroll

741 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