Solved

Importing packages/classes

Posted on 2002-03-19
12
228 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
SharePoint Admin?

Enable Your Employees To Focus On The Core With Intuitive Onscreen Guidance That is With You At The Moment of Need.

 

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

Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

Question has a verified solution.

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

Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
Viewers learn about the “while” loop and how to utilize it correctly in Java. Additionally, viewers begin exploring how to include conditional statements within a while loop and avoid an endless loop. Define While Loop: Basic Example: Explanatio…
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:

688 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