Solved

Importing packages/classes

Posted on 2002-03-19
12
225 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
Problems using Powershell and Active Directory?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

 

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

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
V.simple question about Android packages and libraries 1 20
iterator/ListIterator approach 17 27
Java string replace 11 47
Android development question 2 37
For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
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…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…

777 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