Solved

Importing packages/classes

Posted on 2002-03-19
12
226 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
Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

 

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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Spring Framework HTTPSession management 1 45
java stored proc example 9 46
check java version using powershell 13 182
Java syntax, or is it Selenium 6 30
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…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
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 arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…

820 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