Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: Java
I have a Ski.java file. I have put it manually in a folder named package1.
Then I made another folder outside the package1 but in the same hierarchy named package1.
Both package1 & package2 are in D:\src directory.
in package2, I made Mainclass.java & trying to import package1 and use Ski class in it.
 
then in command prompt, I have compiled Ski.java in its own folder, using following command
D:\src\package1>javac Ski.java
then I am trying to compile MainClass in its folder, like this:
 
D:\src\package2>javac MainClass.java
 
But it is not compiled.
Compile error is that package1 doesnot exist.
 
please give me a solution.
Posted 28-Jan-13 22:20pm
Edited 29-Jan-13 18:50pm
v2
Comments
Shubhashish_Mandal at 29-Jan-13 5:17am
   
Check the following things :
1. mention package declaration on top of each class
2. compile the class with their package name
D:\src>javac package2.MainClass.java
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

You need to add the -classpath option to your command, to tell the compiler where the Ski.class file is. Something like:
D:\src\package2>javac -classpath D:\src\package1 MainClass.java
  Permalink  
Comments
Tamanna9931 at 29-Jan-13 23:54pm
   
I have set the CLASSPATH in environment variables as D:\src
resulting in successful compilation but at runtime, throws exception NoClassDefFound
still the same problem...
nothing is going on...
is there any other solution...
Richard MacCutchan at 30-Jan-13 2:57am
   
Well it's obviously the same issue, the JVM is telling you it cannot find the class, so your CLASSPATH variable needs adjusting, or the actual class is not where you think.
Tamanna9931 at 30-Jan-13 3:52am
   
I told u what have I did... Is there any solution to this problem???
Richard MacCutchan at 30-Jan-13 3:55am
   
And I told you what the solution is. Go and look in your directories to see where all your class files are, and make sure they are i) created properly and ii) in the correct location when you try to run your program.
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

When compiling MainClass you need to make sure the compiler knows about the .class file from package1.
 
Try compiling MainClass like this instead;
D:\src\package2\javac -classpath ..\ MainClass.java
 
By adding a classpath option you can tell the compiler where to find jars and class files.
 
Hope this helps,
Fredrik
  Permalink  
Comments
Tamanna9931 at 29-Jan-13 23:55pm
   
I have set the CLASSPATH in environment variables as D:\src
resulting in successful compilation but at runtime, throws exception NoClassDefFound
still the same problem...
nothing is going on...
is there any other solution...
Fredrik Bornander at 30-Jan-13 4:26am
   
Execute it by specifying the class path:
D:\src\java -classpath . package2.MainClass
 
Note that that should be run at D:\src\, not in D:\src\package2.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 OriginalGriff 350
1 Jochen Arndt 190
2 Richard MacCutchan 135
3 Sergey Alexandrovich Kryukov 130
4 DamithSL 95
0 OriginalGriff 6,045
1 DamithSL 4,601
2 Maciej Los 4,087
3 Kornfeld Eliyahu Peter 3,480
4 Sergey Alexandrovich Kryukov 3,310


Advertise | Privacy | Mobile
Web01 | 2.8.141220.1 | Last Updated 29 Jan 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100