[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 233
  • Last Modified:

Java Package problems...

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
reachvikash
Asked:
reachvikash
  • 3
  • 3
  • 2
  • +2
2 Solutions
 
Nick_72Commented:
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
 
Nick_72Commented:
>>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
 
Nick_72Commented:
eg1 should be in a folder named pkg.
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
sdyxCommented:
And the folder should be in the directory where eg11 is, for example:
c:\test\eg11.class
and
c:\test\pkg\eg1.class
0
 
sanjayjayCommented:
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
 
Mayank SAssociate Director - Product EngineeringCommented:
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
 
WebstormCommented:
Hi reachvikash,

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

0
 
WebstormCommented:
I suggest to split among all experts : Nick_72, sdyx, sanjayjay, mayankeagle, Webstorm
0
 
Mayank SAssociate Director - Product EngineeringCommented:
There can be only a two-way split on this.
0
 
Mayank SAssociate Director - Product EngineeringCommented:
Split: Nick and sdyx.
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

  • 3
  • 3
  • 2
  • +2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now