• 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
  • Paul Clapham
  • Tim Cooke
  • Jeanne Boyarsky
  • Liutauras Vilda
Sheriffs:
  • Frank Carver
  • Henry Wong
  • Ron McLeod
Saloon Keepers:
  • Tim Moores
  • Frits Walraven
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Himai Minh

to set classpath in a java program

 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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! )
 
Bartender
Posts: 10336
Hibernate Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
Marshal
Posts: 27365
88
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 1170
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Don't we have a forum section for homework?
 
(instanceof Sidekick)
Posts: 8791
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
did you find the answers?..
 
Marshal
Posts: 76395
364
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to the Ranch

Since some of the posters are no longer active, and one of them has died, you may not get a reply from them.

There are several different ways to do this. Just one possibility:-
I would suggest you create the two directories as subdirectories of a third directory, and use those as packages.Working from myJavaDirectory writeThen create the following two files with a text editor:- package1/A.java package2/B.java
Edit the two files and compile the dependency first:- Note the original question had the compiling instruction the wrong way round: it should be B first.
Execute the main method in class A with:-Note the different instructions with / and .
 
desin lee
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
instead of specifying the classpath in the shell, try to set in your java prog??
any source code available??
 
Campbell Ritchie
Marshal
Posts: 76395
364
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Why are you so insistent on setting a CLASSPATH? It is possible to set a CLASSPATH in the manifest file in a .jar.
 
You are HERE! The other map is obviously wrong. Better confirm with this tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic