Solved

Java Package problems...

Posted on 2004-04-21
13
218 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
Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

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.

 
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

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

Question has a verified solution.

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

Suggested Solutions

INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
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…
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…
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

840 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