Perhaps silly, but is it possible ...

D

D. Alvarado

to load a file into the classpath AFTER you have started your Java
program? That is, I know the path to a file, but that file does not
appear in the CLASSPATH enviornment variable or in any -CLASSPATH
directive passed to the java invocation. Perhaps a bit silly, but I
have no control over the environment or runtime setting s, which is
why I'm seeking a back door.

If the answer is yes, how could this be done?

Thanks in advance, - Dave
 
S

Stefan Wischnewski

D. Alvarado said:
to load a file into the classpath AFTER you have started your Java
program? That is, I know the path to a file, but that file does not
appear in the CLASSPATH enviornment variable or in any -CLASSPATH
directive passed to the java invocation. Perhaps a bit silly, but I
have no control over the environment or runtime setting s, which is
why I'm seeking a back door.

If the answer is yes, how could this be done?

Thanks in advance, - Dave
Not silly at all. Take a look at the URLClassLoader.

Stefan
 
J

John Davison

D. Alvarado said:
to load a file into the classpath AFTER you have started your Java
program? That is, I know the path to a file, but that file does not
appear in the CLASSPATH enviornment variable or in any -CLASSPATH
directive passed to the java invocation. Perhaps a bit silly, but I
have no control over the environment or runtime setting s, which is
why I'm seeking a back door.

If the answer is yes, how could this be done?

Thanks in advance, - Dave

You don't load files into the classpath. That doesn't make sense. All
the classpath does (AFAIK) is supply the JVM with directories to search
through for Java .class files.

If you need to open a file, simply provide the full path from root, or a
relative path. The classpath has nothing to do with this.

File f = new File("/some/path/to/your/file");

- john
 
M

Michael Borgwardt

John said:
You don't load files into the classpath. That doesn't make sense. All
the classpath does (AFAIK) is supply the JVM with directories to search
through for Java .class files.

No, you can also use it to load all kinds of files via the "resource" methods
in the Class and ClassLoader classes. This is in fact the preferred method
because it allows the files to be packaged with the application and found
regardless of the install location (end even within a JAR file).

Of course, the advantages disappear when you first have to make a new
ClassLoader via an absolute path anyway.
 
J

John Davison

Michael said:
No, you can also use it to load all kinds of files via the "resource"
methods
in the Class and ClassLoader classes. This is in fact the preferred method
because it allows the files to be packaged with the application and found
regardless of the install location (end even within a JAR file).

Of course, the advantages disappear when you first have to make a new
ClassLoader via an absolute path anyway.

You're right. I stand corrected.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,048
Latest member
verona

Latest Threads

Top