executing java -jar via classpath vs in the jar fi

2020-05-06 12:00发布

After having used NetBeans to create a Java program call it Addition and then having successfully cleaned and built an Executable Jar File in a folder c:\Users\Ben\Doc\NetBeansProjects\Addition\dist

WHY is it that when executing, from command prompt, c:\Users\Ben Java -Jar -cp "c:\Users\Ben\Doc\NetBeansProjects\Addition\dist" Addition.jar it does NOT work (i get 'unable to access jarfile Addition.jar) BUT if i use cd to change my current dir to c:\Users\Ben\Doc\NetBeansProjects\Addition\dist and THEN run 'java -jar Addition.jar' from there, the Addition program runs fine

2条回答
迷人小祖宗
2楼-- · 2020-05-06 12:23

because java doesn't look in classpath to launch jar file for this command it needs file as input

so if you set the directory where your jar file is placed and try to execute java -jar command and expect it to pick up jar from that directory because it is in classpath it is not valid

you can give full path to jar like from any directory

java -jar c:\Users\Ben\Doc\NetBeansProjects\Addition\dist\Addition.jar
查看更多
兄弟一词,经得起流年.
3楼-- · 2020-05-06 12:34

The -classpath argument is ignored when you use the -jar option. See the documentation.

查看更多
登录 后发表回答