Solved

Java Package problems...

Posted on 2004-04-21
13
216 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
 
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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
reasons why a "inside Menu" would not appear for a web server 6 56
Systems talking to each other 5 124
compre toata in where clue oracle 4 56
MySQL  on Tomcat 8 27
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…
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 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 learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…

910 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now