0

I've just started studying Java and wanted to test something. I have Window 10 and using PowerShell I found a script for changing the wallpaper:

$setwallpapersource = @"
using System.Runtime.InteropServices;
public class wallpaper
{
    public const int SetDesktopWallpaper = 20;
    public const int UpdateIniFile = 0x01;
    public const int SendWinIniChange = 0x02;
    [DllImport("user32.dll", SetLastError = true, CharSet = CharSet.Auto)]
    private static extern int SystemParametersInfo (int uAction, int uParam, string lpvParam, int fuWinIni);
    public static void SetWallpaper ( string path )
    {
        SystemParametersInfo( SetDesktopWallpaper, 0, path, UpdateIniFile | SendWinIniChange );
    }
}
"@
Add-Type -TypeDefinition $setwallpapersource
[wallpaper]::SetWallpaper("C:\Users\Saab\Desktop\Image.jpg")

I saved this .ps1 script and converted it to .exe ("change_wp.exe").

If I double click on the exe file, it will change wallpaper as required.

Then I wanted to see if I could execute the exe file on Java:

import java.io.IOException;

class Change {
    public static void main(String args[])
        throws IOException
    {
        Runtime.getRuntime().exec("C:\\Users\\Saab\\Desktop\\change_wp.exe");
    }
}

Saved the file as Change.java and put it on my desktop.

Opened cmd, reached the path were the file java is (C:\Users\Saab\Desktop) and executed the 2 below commands:

  • javac Change.java (no error displayed)
  • java -cp . Change (no error displayed)

It seems all good no? Then I had a look at the wallpaper, which didn't change. :(

Where am I wrong?

Nisse Engström
  • 4,738
  • 23
  • 27
  • 42
Sa Ab
  • 1
  • I suggest you look at the process' output. See here: https://stackoverflow.com/questions/5711084/java-runtime-getruntime-getting-output-from-executing-a-command-line-program – Andy Turner Oct 22 '17 at 20:38
  • Implemented the procedure for displaying the process' output, which is made up by the following 2 lines: **Here is the standard output of the command:** **Here is the standard error of the command (if any):** – Sa Ab Oct 22 '17 at 21:13
  • Don't try to post the output in comments: [edit] the question. – Andy Turner Oct 22 '17 at 21:16
  • Not enough reputation to edit yet...just started _noob_ :) – Sa Ab Oct 22 '17 at 21:21
  • Nobody has an idea please? – Sa Ab Oct 24 '17 at 19:35

0 Answers0