java - JRE not using classpath specified by manifest file of runnable jar -
first post, sorry poor formatting. have java program developed in eclipse. exported program jar (myjar.jar), , put of external jars program depends on folder called lib lives in same location myjar.jar. in order set classpath have manifest file following format:
main-class: exe.mymain class-path: lib/jar_1.jar lib/jar_2.jar ... lib/jar_n.jar manifest-version: 1.0
however, when attempt run program using "java -jar myjar.jar" classes jars live in lib not being loaded (i'm getting classnotfoundexception) . used following code in program print classpath:
classloader cl = classloader.getsystemclassloader(); url[] urls = ((urlclassloader)cl).geturls(); for(url url:urls){ system.out.println(url.getfile()); }
and when run code classpath "myjar.jar".
i have 2 questions:
1.) above code give me classpath jre @ run time, or being given address of main class?
2.) given above code indeed give me classpath jre @ run time, doing wrong?
please feel free ask more information, , happily provide need.
what doing sounds correct.
for reason class-path
entry in manifest not show when inspecting classpath (e.g. here , here; examples use property "java.class.path"
testing shows classloader.geturls()
behaves same). should still searched classes though. don't know how obtain true classpath includes class-path
entry manifest.
the first thing i'd check file meta-inf/manifest.mf
inside myjar.jar
matches manifest created. can open myjar.jar
renaming have .zip
file extension.
i tried replicate problem classes in lib/jar_1.jar
loaded me, if meta-inf/manifest.mf
correct i'll describe did in detail can find doing differently.
edit:
here steps used:
create new directory called "experiment". following steps done in directory.
create new directories called "jar_1", "lib", , "exe"
create file called "classinjar1.java" in directory "jar_1" following content:
package jar_1; public class classinjar1 { public static void method() { system.out.println("hello classinjar1"); } }
run
javac jar_1/classinjar1.java
run
jar cf lib/jar_1.jar jar_1/classinjar1.class
create file called "mymain.java" in directory "exe" following content:
package exe; import java.net.*; import jar_1.classinjar1; public class mymain { public static void main(string[] args) { classloader cl = classloader.getsystemclassloader(); url[] urls = ((urlclassloader) cl).geturls(); (url url : urls) { system.out.println(url.getfile()); } classinjar1.method(); } }
run
javac exe/mymain.java
create file called "manifest" in "experiment" directory following content:
main-class: exe.mymain class-path: lib/jar_1.jar lib/jar_2.jar manifest-version: 1.0
run
jar cfm myjar.jar manifest exe/mymain.class
run
java -jar myjar.jar
output:
/.../experiment/myjar.jar hello classinjar1
Comments
Post a Comment