Solved

Java Package problems...

Posted on 2004-04-21
13
219 Views
Last Modified: 2010-03-31
Hi,
 I am creating a package pkg with a class eg1.
I am trying to use this package in class eg11.
eg1 gets compiled; but,I get the following errors while compiling eg11.java
What could the problem be? Both the files are in folder 'pkg'.

pkg/eg11.java:1: package pkg does not exist
import pkg.eg1;
           ^
pkg/eg11.java:5: cannot resolve symbol
symbol  : class eg1
location: class eg11
       eg1 myobj1 = new eg1();
       ^
pkg/eg11.java:5: cannot resolve symbol
symbol  : class eg1
location: class eg11
       eg1 myobj1 = new eg1();
                        ^

/***/
contents of eg1.java
/***/

package pkg;
 public class eg1 {

    public void show() {
       System.out.println("Testing packages");
    }
    public void oddeven(int i) {
       if((i%2)==0)
          System.out.println ("The number: "+i  +"is even");
      else
          System.out.println ("The number: "+i  +"is odd");
     }
 }

/***/
contents of eg11.java
/***/

import pkg.eg1;

 public class eg11 {
   public static void main(String args[]) {
       eg1 myobj1 = new eg1();
       myobj1.show();
       myobj1.oddeven(286);
     }
 }
0
Comment
Question by:reachvikash
  • 3
  • 3
  • 2
  • +2
13 Comments
 
LVL 9

Accepted Solution

by:
Nick_72 earned 25 total points
ID: 10877504
Are the java files located in a folder named pkg? (they should be)

You don't need to import the class pkg.eg1 since both classes are in the same package.

/Nick
0
 
LVL 9

Expert Comment

by:Nick_72
ID: 10877511
>>You don't need to import the class pkg.eg1 since both classes are in the same package.

Hmm...guess I was a little quick on that one.
0
 
LVL 9

Expert Comment

by:Nick_72
ID: 10877527
eg1 should be in a folder named pkg.
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 1

Assisted Solution

by:sdyx
sdyx earned 25 total points
ID: 10877565
And the folder should be in the directory where eg11 is, for example:
c:\test\eg11.class
and
c:\test\pkg\eg1.class
0
 

Expert Comment

by:sanjayjay
ID: 10877864
If you compiling your java file in the command prompt you have to specify the classpath to your folder containing pkg folder so that the dependent classes can be found by the compiler.
0
 
LVL 30

Expert Comment

by:Mayank S
ID: 10877895
Make sure that your class-path is set to the parent folder of pkg. For example, if you have C:\TEST\MYCLASSES\pkg\eg1.class

- then the classpath should haveC:\TEST\MYCLASSES

set classpath=%classpath%;C:\TEST\MYCLASSES;.

Note - if you have the source-file eg1.java in the C:\TEST\MYCLASSES folder and compile it from there, it will make a pkg folder and the eg1.class file will be written there automatically. The class-path should be till C:\TEST\MYCLASSES. The eg11.java file should also be in C:\TEST\MYCLASSES.
0
 
LVL 13

Expert Comment

by:Webstorm
ID: 10878100
Hi reachvikash,

try to compile your class from its directory, for example :
  cd c:\test\pkg\
  javac -classpath c:\test;. eg1.java

0
 
LVL 13

Expert Comment

by:Webstorm
ID: 11676620
I suggest to split among all experts : Nick_72, sdyx, sanjayjay, mayankeagle, Webstorm
0
 
LVL 30

Expert Comment

by:Mayank S
ID: 11678076
There can be only a two-way split on this.
0
 
LVL 30

Expert Comment

by:Mayank S
ID: 11678092
Split: Nick and sdyx.
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering 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
Configure a Bean in an XML file 4 48
by zero exception 10 69
Fast way to search item into Java Array (Rhino compatible) 2 36
wild fly 8 startup error 2 37
Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
In this post we will learn different types of Android Layout and some basics of an Android App.
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:

685 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