0

I am running a few powershell scripts from within java, i have completed my program but now i am unsure how to package it up into an executable .jar, while still being able to use the scripts. i have placed the scripts inside the project file (at the same level as the src folder)

String[] commands1 = {"powershell.exe", "Set-Variable", "-Name \"address\" -Value \""+javaAddress+"\";", "C:\\Users\\--\\Documents\\ieAutomate.ps1\" "};

&

    String runScan = "powershell.exe  \"C:\\Users\\--\\Documents\\ipScan.ps1\" " ;
    Process scanProcess = Runtime.getRuntime().exec(runScan);

I am aware that .class.getResourceAsStream("ipScan.ps1") could potentially work - but how can i use this as a string for the second part of the runScan string?

I also am unsure how to specify filepath for a FileWriter too.

    writer2or32Port = new FileWriter("C:\\Users\\--\\Documents\\serials\\2or32Port.csv");
    buffer2or32Port = new BufferedWriter(writer2or32Port);`

ideally i can just use something like %USERNAME% or %USERPROFILE% to any users desktop

writer2or32Port = new FileWriter("%USERPROFILE%\\Desktop\\2or32Port.csv");

However im getting file not found exceptions. Any help would be appreciated.

Thanks

Joshfromnz
  • 45
  • 4
  • 1
    One of the problems you will have is the concept of the "working directory". This is the location from which the program was launched, which may not be the location that the program resides. In this case, there's not a lot you can do, as it's generally not possible to find the installation location. Instead, you could package the scripts into the Jar and, at runtime, extract them to a well known location (like temp or the user's home location) – MadProgrammer Jan 31 '20 at 01:03
  • @MadProgrammer cheers for the response, i plan to have the scripts definitely in the jar file - although i have never done this before, im not sure how to adjust my code from within java to call my scripts if its packaged together (i only know how to specify an exact location, i havent been able to figure out how to use the .getResourceAsStream) – Joshfromnz Jan 31 '20 at 01:09

1 Answers1

1

I had a similar problem and found this useful:

E.g. you put your script in your sources and load it via getResourceAsStream() and put the content anywhere on your disk. From there you then can launch the script.

public static void main(String[] args) throws IOException {

    Files.copy(Test.class.getResourceAsStream("test.ps1"), Paths.get("/tmp/sometempfile.ps1"));
    // now you know the location and can do something like: cat /tmp/sometempfile.ps1

}
ave4496
  • 2,950
  • 3
  • 21
  • 48
  • Hey, thanks heaps for the response, so i have been searching around in the meantime and have found https://stackoverflow.com/a/320595/12502255 this answer - so i placed my scripts at this location and have just added the "\\ipScan.ps1" onto the end of the string, seems to work so far? i will package this up and give it a test - will use your method if i need to. Thank you – Joshfromnz Jan 31 '20 at 01:45
  • Nit pick, but I'd be tempted to use [`File.createTempFile`](https://docs.oracle.com/javase/10/docs/api/java/io/File.html#createTempFile(java.lang.String,java.lang.String)) instead – MadProgrammer Jan 31 '20 at 03:08
  • 1
    @Joshfromnz No, as a general concept, it won't work for you - assuming you want the scripts contained in the Jar file as a starting point. You will need to use `getResourceAsStream` (or similar) and, using the stream, copy the script to a "external" location - remember, a Jar is just a zip file – MadProgrammer Jan 31 '20 at 03:09
  • @MadProgrammer yeah ive realised this :( will have to try the `getResourceAsStream` when im back in the office - currently as you stated it wont work as i want, as i need to have the scripts outside of the jar for it to work cheers though for the help! – Joshfromnz Jan 31 '20 at 03:29