1

I have had some trouble with a small program I am working on. All it does is change the desktop picture to something else. But I cant do something like this:

   static File aFile = new file.getClass().getResource(name)

Instead I use this:

   static File aFile = new File("/Library/Application Support/Adobe/Adobe PCD/cache/SOURCE CODES/troller/Troller/src/1.jpg");

I would like the image for the program to put the desktop, available in the jar file, but I can only say where the image is with a full file path. (A think its called Absolute Path) So how would I make the file find the image wherever the compiled jar would be. (assuming that the image is in the jar with the program.) Might be something i'm doing wrong with making everything static.Here is the entire program:

import java.io.File;

public class Main {
static File aFile = new File("/Library/Application Support/Adobe/Adobe PCD/cache/SOURCE CODES/troller/Troller/src/1.jpg");
static Main troll = new Main();
public static void main(String[] args) {
    try {
        setWallpaper(aFile);
    } catch (Exception e) {
        System.out.println("Execption lel");
        e.printStackTrace();
    }
}
 public static void setWallpaper(File file) throws Exception
    {
        String as[] = new String[]
        {
            "osascript",
                "-e", "tell application \"Finder\"",
                "-e", "set desktop picture to POSIX file \"" + file.getAbsolutePath() + "\"",           
                "-e", "end tell"
        };
        Runtime runtime = Runtime.getRuntime();
        Process process;
        process = runtime.exec(as);
        System.out.println("Test");
    }
}

Thanks in advance.

Ky6000
  • 103
  • 1
  • 9
  • 1
    This isn't clear; are you saying that the image is *in* the JAR? – Oliver Charlesworth May 12 '14 at 23:03
  • Take a look at this: http://stackoverflow.com/questions/6351441/referencing-an-image-in-a-jar-file – merlin2011 May 12 '14 at 23:05
  • Note that is is unlikely that you will be able to reference an image *inside* a jar as a wallpaper without explicitly copying it out to the filesystem first. – merlin2011 May 12 '14 at 23:05
  • getResource will take a string argument (path + resource name) and return the URL of the resource location relative to the class that called the method. If there's a leading "/" it will begin the search from the root of the classpath. Where in the JAR file is this file saved? All paths in a JAR file are relative to the root of the JAR itself... Second, merlin20111 is right... you will want getResourceAsStream instead, and will either need to save an Image object, or copy the byte stream out to a file on the filesystem. – Ryan J May 12 '14 at 23:10
  • The image is in the source folder. When I say "in the jar", I mean when the program is compiled, the image is going to go in the jar. Does this mean I have to copy the image to another spot on the computer from the jar, or from the source folder when i'm using eclipse, and then set the file path for "aFile" to wherever I copied the image? – Ky6000 May 12 '14 at 23:46

0 Answers0