• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Issue when compiling a class which is importing a package

 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Dear All,

I am new to java and I am facing an issue when compiling a class which is importing a package.

Please find the below steps which I am doing.

Step1:

I created the below addition.java in a folder c:\java



so now I have addition.java in c:\java\addition.java
I also have a folder called pack inside the c:\java folder.


step2: Now compile the addition.java class
javac -d pack addition.java
After compilation when I can see the addition.class in c:\java\pack\subpack\innersubpack\ folder.
Until now it is fine

Step3: Now create a class which is trying to import the addition class



This UseAddition.java is saved in c:\java folder.


Step4:
Now when I am trying to compile the code it is failing
c:\java> javac UseAddition.java
UseAddition.java:1: package subpack.innersubpack does not exist
import subpack.innersubpack.*;

My question is How to compile this UseAddition.java code without moving the code into the c:\java\pack folder and keep it in the c:\java folder itself and compile.

Appreciate your help here.

Thanks&Regards
Madhu K.
 
Ranch Hand
Posts: 544
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello,
First of all keep the "addition.java" in appropriate package folder.
In your case it would be C:\Java\subpack\innersubpack\addition.java.

Secondly when you want to use that in another class i.e. UserAddition, you will need to set the classpath so that the "addition.class" can be located when dependancies are resolved. To do this set the classpath to the "addition.java".
This can be done by using

Please note that execute this command in "C:\java" so that "." will set the classpath to current directory.
The "UserAddition" class is not in any package so you have correctly placed the same in C:\Java, which should not be a problem.

Regards,
Amit
 
Marshal
Posts: 79151
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

amit punekar wrote: . . . Secondly when you want to use that in another class i.e. UserAddition, you will need to set the classpath so that the "addition.class" can be located . . .

Are you sure? I think that is unnecessary. Compiling classes in packages causes no end of confusion, so I have been to some old posts of mine and altered what I said earlier

Amended from what I previously wrote:to create a package: two ways to do it.

All your classes must have the package declared as the very first statement, before "import" statements.

1: Create a folder with the same name (all lower-case) as the package, and put your java files in that. Navigate to the folder one level up from your package, and compile the classes like this
javac packagename/MyClass.java jackagename/MyClass2.java
You still need to compile supplier classes before client classes.

2: Don't create a folder; navigate to the folder one level up from your package, which contains the classes, and compile the classes like this
javac -d . MyClass.java MyClass2.java
The same thing about supplier classes first applies.

To execute, navigate to the folder one level up (if you used method 2 you will already be there) and write java.mypackage.MyClassWithMainMethod command line arguments

Method 1: no folders, only the three .java classes.

campbell@queeg:~/java$ javac -d . Class1.java Class2.java Class3.java
campbell@queeg:~/java$ java pack1.Class1
null null
campbell@queeg:~/java$ java pack1.Class2
null null
campbell@queeg:~/java$ java pack2.Class3
null null

Method 2: Delete all the .class files, move the 3 .java files into their respective directories

campbell@queeg:~/java$ javac pack1/Class1.java pack1/Class2.java pack2/Class3.java
campbell@queeg:~/java$ java pack1.Class1
null null
campbell@queeg:~/java$ java pack1.Class2
null null
campbell@queeg:~/java$ java pack2.Class3
null null

Delete the Class3.class file and navigate to pack2 . . . and it fails because it can't find pack1
 
amit punekar
Ranch Hand
Posts: 544
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi there,
May be I did not word it properly.
What I meant to say that because UserAddition.java was not placed in the package where "addition.java" is put into, you will need to set the classpath so that class can be located.
May be I was not clear while writing the answer.
thanks for elaboration though.

Regards,
amit
 
Campbell Ritchie
Marshal
Posts: 79151
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
They are not in a different directory structure. You need to execute and compile multiple packages from the root of their directory structure.
 
Campbell Ritchie
Marshal
Posts: 79151
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I managed to get it to compile like this: Set a directory structure. At this point there are no .class files:

[campbell@campbell1110 addition]$ javac subpack/innersubpack/addition.java UseAddition.java&
[1] 15070
[campbell@campbell1110 addition]$ java UseAddition
The sum is 5.801
[1]+ Done

. . . and I hadn't noticed you were new the the Ranch. Sorry.

Welcome
 
reply
    Bookmark Topic Watch Topic
  • New Topic