• Post Reply Bookmark Topic Watch Topic
  • New Topic

how to make sure the ClassLoader can find my classes when use newInstance().  RSS feed

 
ZhiYuan Lin
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi
I uses the two path to get a new object:
one:

the other:

In the test, I use JCreator, and use javac java , it is ok.
but when I put it to my projects, It is throw ClassNotFoundException
I use to put all the classes that I need. Is OK?
In the test, I still have put it in the path as in real project. It still can work.
how to make sure the ClassLoader can find my classes???
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24217
38
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You've got to put all the relevant classes on your CLASSPATH. The fact that you're using Class.forName() doesn't make any difference.
 
ZhiYuan Lin
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thank you!
but it is still can't work
in the method main()

because they are part of my project, i think don't need to put them explicitly on the classpath
I have put it on classpath, it is can't work too
java -classpath app\system\dao\entity\UserDAOEntity.class app.system.dao.DAOFactory
 
Azriel Abramovich
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm not too sure I understood the problem, but: did you try, when using Class.forName() to use it with a specific ClassLoader. There is overloading of this method enabling you to specify which loader.
I have had (and still) some problems with class loaders. You have to make sure that when you load a class (or rather try to) it hass not yet been loaded by it's parent. When this is the case thanks to the delegation model of class loading you get the class from the parent class loader and not the loader you expect...
Well, I hope I even helped and not just blabbared.
 
ZhiYuan Lin
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi Azriel:
thank you.
I don't use specify ClassLoader.
I just use the to make code more clear and easily to extend.

but it can't work
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24217
38
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

java -classpath app\system\dao\entity\UserDAOEntity.class app.system.dao.DAOFactory
This is totally wrong. See HowToSetTheClasspath in the JavaRanch FAQ.
 
ZhiYuan Lin
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi Ernest :
thank you again
en, I see, but I known how to set the classpath in my system, it is error?
java -classpath app\system\dao\entity\UserDAOEntity.class app.system.dao.DAOFactory
o, I will find the right form.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!