1

Is there a way in Java to add a file to the classpath without overwriting the existing one?

So that the application would take what is provided to it (whatever other means are used for that) but adding just this one more file I provide.

I don't want to do it on the fly by the application - but on starting it. -cp overwrites the classpath - That's what I don't like. Is there another option?

pouyan021
  • 177
  • 1
  • 4
  • 19
PaulEdison
  • 897
  • 1
  • 15
  • 36
  • Changing the class path needs to be done externally to the application. I don't think it can be done on the fly (though you can use a custom classloader). – markspace Apr 12 '19 at 17:05
  • Possible duplicate of: https://stackoverflow.com/questions/252893/how-do-you-change-the-classpath-within-java – Harish Apr 12 '19 at 21:47
  • @RedHunt: No - I don't want to do it within the application. Calling the application Iwant to add just one file to what ever is alereay on the class path. – PaulEdison Apr 13 '19 at 10:01
  • @markspace: I don'T want to do it by the application. I do want to do it on appliaction start like providing one file more to the class path without changing it in it self (pointing to a different folder. – PaulEdison Apr 13 '19 at 10:05
  • So, edit the java command used to start your program, and add the file at the end of the `-cp` option. Not sure what you're asking. – JB Nizet Apr 13 '19 at 10:10
  • @JBNizet: There is currently no -cp that would overwrite the existing one. I would need a argument that is NOT changing the existing one but add just one new jar to the classpath. – PaulEdison Apr 13 '19 at 11:31
  • So you're relying on the global CLASSPATH environment variable, do I understand correctly? This is a bad idea. But anyway, if you really want to continue doing that, and still add something to the classpath, then use `-cp $CLASSPATH:someOtherFile.jar` (on Unix), or `-cp %CLASSPATH%;someOtherFile.jar` (on Windows). – JB Nizet Apr 13 '19 at 12:03

0 Answers0