简体   繁体   中英

Using Java to change Windows 7 desktop background from inside a runnable jar

I'm using code from here to change my desktop background to a set picture. This code works perfectly in Eclipse, but when I package the application to a runnable jar, it doesn't do anything. Running

java -jar DesktopChanger.jar

from the command line doesn't result in any errors. It just prints out a job completed message (sysout) and ends, but the wallpaper didn't change.

I imported the jna libraries jna-platform-4.1.0.jar and jna-4.1.0.jar from here . The only difference between my code and the other question is a sysout, a messageDialog to say the job finished, and the location of the picture:

String path = "src/background/changer/picture.jpg";

Finally, in Eclipse I right click on the project

  • hit "Export"
  • select "Runnable JAR File"
  • select "Package required libraries into generated JAR"
  • and finish.

I've even messed with the Manifest.mf file since Eclipse sets "Main-Class:" to it's own main, using "Rsrc-Main-Class" for the actual one. So I deleted "Rsrc-Main-Class" and put my main into "Main-Class" but again nothing works.

Do I also need to package the native code or dll files?

Your issue is the path of the image. The path you used is inside of your jar file, and as such windows can't locate it. Thus, it just ignores the request.

Change it to something like this:

String path = "C:\\picture.jpg";

and put the picture in that location.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM