hklel
hklel

Reputation: 1634

.jar program works on OS X but doesn't work on Windows

I developed a Java program on Mac and exported it as runable .jar file. It works perfectly on OS X without error or warning. But When I tried to run it in Windows, I got an error.

Here are two functions in my main class:

 private static void getImageUrl(ArrayList<String> imagePageUrl) throws IOException{
    for (String pageUrl : imagePageUrl){
        String url = pageUrl;
        Document doc = Jsoup.connect(url).get();
        Elements media = doc.select("[src]");

        for (Element src : media) {
            if ((src.tagName().equals("img")) & (src.attr("abs:src").endsWith("jpg"))){
                System.out.println("Downloading: " + src.attr("abs:src"));
                String destination = LocalDateTime.now() + ".jpg";
                saveImage(src.attr("abs:src"), destination);
            }
        }
    }
}

private static void saveImage(String imageUrl, String destinationFile) throws IOException {
    URL url = new URL(imageUrl);
    InputStream is = url.openStream();
    OutputStream os = new FileOutputStream(destinationFile);

    byte[] b = new byte[2048];
    int length;

    while ((length = is.read(b)) != -1) {
        os.write(b, 0, length);
    }

    is.close();
    os.close();
}

In Windows cmd the line System.out.println("Downloading: " + src.attr("abs:src")); was executed successfully, but right after this line was printed, I got an error:

Exception in thread "main" java.lang.reflect.InvocationTargetException

How can I fix this? Thanks.

Upvotes: 0

Views: 113

Answers (1)

Tea Curran
Tea Curran

Reputation: 2983

LocalDateTime.now() + ".jpg" is going to produce a string like:

2015-06-08T17:05:16.532.jpg

This isn't a valid file name in windows. Have you tried doing something like:

String destination = LocalDateTime.now().toString().replaceAll(":", "-") + ".jpg";

Upvotes: 1

Related Questions