Solved

Java Package problems...

Posted on 2004-04-21
13
215 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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 30

Expert Comment

by:mayankeagle
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:mayankeagle
ID: 11678076
There can be only a two-way split on this.
0
 
LVL 30

Expert Comment

by:mayankeagle
ID: 11678092
Split: Nick and sdyx.
0

Featured Post

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
countX 22 70
where is session ID cache stored 1 40
Problem to start Neon 20 54
Cipher Configuration on Apache HTTPD 4 24
After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…

760 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