• 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
  • Tim Cooke
  • Devaka Cooray
  • Ron McLeod
  • Jeanne Boyarsky
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
  • Tim Holloway
Bartenders:
  • Martijn Verburg
  • Frits Walraven
  • Himai Minh

Help in Packages and claspaths

 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
I have created a class in a package called newtest and I have saved it as D:\newtest\someException.java and compiled and no errors,
package newtest;
public class someException extends Exception
{
String errorcode;
String errordesc;
public someException(String ecode,String edesc)
{
errorcode = ecode;
errordesc = edesc;
severity = sev;
}
public String geterrorcode()
{
return errorcode;
}
}
but when I want to use the package in another normal java program which is stored in D:\testing\test\test.java.
It's giving NoClassDef found runtime errors.It's compiling properly when give
javac -classpath D:\newtest; test.java

import newtest.*;
class test
{
public static void main(String[] args)
{
try
{
throw new someException("100","Hello","world");
}
catch(someException e1)
{
String s1=e1.geterrorcode();
System.out.println(s1);
}
}
}
Can anyone tell me how to use packages and set classpaths without these problem
 
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
When you have to access a file defined in a package, one has to put the path till the root of the package ( in your case for package newtest, D:\) in the classpath environment variable. U can do this from commandline (set CLASSPATH=%CLASSPATH;D;\; ) . Or U have to give this class path as additional parameter in ur javac command.(as U successfully did)
Ur 'test' class doesn't have any package. So it belongs to the default package. If U have to acces this file from some other file in some other package, U wud have to put the path of the test file in the CLASSPATH variable.
Coming to ur specific example, when U try compiling 'test.java', it encounters a constructor call ( someException()). The local folder is searched for such a class. If Ur 'somException' class also didn't have a package(so default package) and was present in the same folder, then test.java wud have compiled with no probs.
But here ur 'someException' is not in defualt package. (As someException is in a package called newtest, the class file must reside in a folder names the same as that of the package. ie, newtset -Hope U know this... ). But that is Ok, U have imported its package. So it goes and searches for a folder named 'newtest' ( if there is a package heirarchy 'a.b.c.newtest', then looks for a folder heirarchy 'a\b\c\newtest') in the folder in which ur test class is.
If that fails, it checks whether there is any class path variable set in the environment. If yes, it appends the folder heirarchy corresponding to the imported path to each entry in the classpath. And then searches for the class. So now once U give D:\ in ur classpath, it will append newtest to that and serach for someException.class there ( D:\newtest\).
Hope I didn't confuse things further ( I have that bad reputation.. )
One last thing, the code that U have given along with ur Q above does not complie.
"severity = sev;". Neither ur method nor ur class has the required parameters.
But ur test class uses three parameters in the call also...
Maybe U copied an older version of ur code..
[ January 27, 2002: Message edited by: Anil KC ]
 
priya anantha
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Anil,
As you said "severity = sev;". is the older program. You are right.I have tried as you said.
I gave javac -classpath D:\; test.java by staying in the directory where test.class is residing.It's working now. Thank you very much.
Is there any way where I can register my package newtest like java.util.* packages so that I can make it available across network.
Pls help me.
 
High Plains Drifter
Posts: 7289
Netbeans IDE VI Editor
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello Priya -
Please take a moment to read JavaRanch's naming policy and re-register appropriately.
Thanks,
[ January 27, 2002: Message edited by: Michael Ernest ]
 
priya anantha
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Mr.Michael,
I have changed the name as Priya. If there is anything I need to change,pls tell me I will change that.
 
no wonder he is so sad, he hasn't seen this tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic