• Post Reply Bookmark Topic Watch Topic
  • New Topic

to set classpath in a java program  RSS feed

 
jaiganesh javab
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hello,

help me to set classpath in a java program.....the actual question is

Write two classes (say "A" and "B") , in class "A" create an instance of class "B" .
Compile class "A" in some directory "D1" and class "B" in "D2" ,now

A.class and B.class are in two separate directories.
Try to run class "A" from directory "D1" (it will say class "B" not found).
if you set classpath to both directories then it will work (try it out!)

Try to set the class path from the class "A" itself (instead of specifying the classpath in the shell, try to set in your java prog! )
 
Paul Sturrock
Bartender
Posts: 10336
Eclipse IDE Hibernate Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why would you want to do this in the class itself? What happens if class B moves?
[ August 10, 2006: Message edited by: Paul Sturrock ]
 
Paul Clapham
Sheriff
Posts: 22701
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Are you asking whether it is possible to change the classpath after the JVM has started running the class? From what you posted, it wasn't clear whether you had any question at all.
 
Mr. C Lamont Gilbert
Ranch Hand
Posts: 1170
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Don't we have a forum section for homework?
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And nobody said "welcome to the ranch!" yet. Hi!

The way you have done it through setting classpath before starting the program is the normal thing to do. You can't really change the classpath from within the program once it's running.

Well, ok, you can by creating your own classloader. It's rather advanced and is not the answer to your general "how to get java going" question. So stick with the "It can't be done" answer for now.
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!