How to run a .ps1 file in PowerShell_ISE.exe through Java - java

So I am creating a GUI in java that launches a variety of different scripts through powershell. I have been able to write a command that opens the .ps1 file in powershellISE, but the script doesn't actually run. My code is as follows:
String [] str = {"cmd", "/c" "start", "powershell_ise.exe", "-file", "myPath"};
try{
Runtime.getRuntime().exec(str);
} catch (IOException e) {
e.printStackTrace();
}
I found this question to be helpful:
Powershell open window (from Java.Runtime.exec)
But it did not solve my issue of actually running the script
Thanks so much!

Windows PowerShell Integrated Scripting Environment (ISE) is a graphical host application that enables you to read, write, run, debug, and test scripts.
If you want to run scripts you know work, you might try replacing powershell_ise.exe with powershell.exe. How to run powershell in cmd should help.

Related

Running a shell script which runs a Java program

I have a java application and from that application I want to be able to run new applications. While the concept is the same, I'm using this in a Minecraft environment where I want to have one application be able to run other Minecraft servers whenever I specify so in the first application.
I'm trying to run the startup shell script but whenever I do I simply get the error Error: Unable to access jarfile server.jar.
Note that I am perfectly able to run the script on my computer by "double clicking it", the shell script works, Java application starts up, and I can join the game server and play without any problems.
I've also tried running the jar application directly from the code, but then I get either CreatProcess error=5 or CreatProcess error=193 (using ProcessBuilder), even though I've set all the computer to have all permissions regarding reading and writing the file.
This is my code for running the shell script:
// Starts the server by running the script
File startScript = new File("C:\\Users\\{MYUSER}\\Desktop\\{PROJECTNAME_FOLDER}\\Network\\prison-spawn-2\\start.bat");
// Run the process
try {
Process process = Runtime.getRuntime().exec("cmd /c start " + startScript.getCanonicalPath());
int exitVal = process.waitFor();
if (exitVal == 0) {
System.out.println("Success!");
}
} catch (Exception exception) {
exception.printStackTrace();
}
Batch Script Content
echo "Starting server..."
java -jar C:\Users\{MYUSER}\Desktop\{PROJECTNAME_FOLDER}\Network\prison-spawn-2\server.jar nogui

Run python file in java

I am developing a programm in java, using Eclipse IDE, and I'm trying to run a python script using a java method.
This python script will simply write "Test2" in a txt file, so it really won't interfere with anything within my java programm.
The image bellow shows how my packages are organized:
The python script I wish to run is Communication.py, and I used the following method, (called by Systemm.java on the method main) to do so:
private void runServer() {
try {
System.out.println("Im running your py");
Process p = Runtime.getRuntime().exec("cmd /c python pythonClient\\Communication.py");
} catch (IOException e) {e.printStackTrace();}
}
However this method does literally nothing (apart from printing "Im running your py").
I have never really worked with java Runtime's class, and I just copied this command from the internet, so I'd really aprecciate any help, in solving this issue.
P.S: my other classes both py and java are all working smoothly. This is literally the only issue so far

How to execute a Python tool installed in Python virtual environment from Java

I want to run a Python tool installed in a python virtual environment from Java source code. What are the possible Java libraries that I can use for this purpose?
I have already tried below code:
Runtime.getRuntime().exec("/Users/xxx/Documents/venv/bin/python3.7 yyy);
But this code doesn't work. It is to run a Python script (eg., yyy= script.py) from the virtual environment (venv). Therefore, it gives me an error saying there is no file called yyy. But my requirement is to run a Python tool installed in the virtual environment venv.
Your requirement might need a little clarification, but I suspect you can make it work with a ProcessBuilder. Use directory(File) to control the working directory for the command. And inheritIO() to make stdio work "automatically". Never hardcode a user's home folder. You can use System.getProperty(String) to retrieve the home folder.
ProcessBuilder pb = new ProcessBuilder();
pb.directory(new File(System.getProperty("user.home"), "Documents/venv/"));
pb.inheritIO();
try {
Process p = pb.command("bin/python3.7",
"lib/python3.7/site-packages/yyy").start();
p.waitFor();
} catch (Exception e) {
e.printStackTrace();
}
It might be better to use System.getenv(String) instead of relying on "Documents/venv" to contain the pyvenv root.

Intellij Terminal behaving differently than Ubuntu Terminal for pulseaudio/pacmd/pactrl commands

I'm trying to run some pulseaudio operations with ProcessBuilder in Java, e.g. pacmd list-source-outputs on Ubuntu 18.04. When I run the code directly from Intellij it says No PulseAudio daemon running, or not running as session daemon.
However, if I go to build/classes/java/main and execute java MyMainClass it works as expected.
I assume that it has something to with how the Intellij terminal is integrated. It doesn't seem to behave the same as the OS terminal (see image). Does anyone have more insights about the Intellij Terminal?
Process p = null;
try {
p = new ProcessBuilder("pacmd", "list-source-outputs").start();
printStream(p.getInputStream());
printStream(p.getErrorStream());
p.waitFor();
} catch (IOException | InterruptedException e) {
e.printStackTrace();
}
Edit: My Terminal settings:
The problem was with pulseaudio. What worked for me was to call
export PULSE_RUNTIME_PATH=/run/user/1000/pulse
before running any pulseaudio/pacmd/pacntl command. The export command doesn't seem to work from runtime. However, you can create a shell file and then execute your commands:
test.sh:
#!/bin/bash
export PULSE_RUNTIME_PATH=/run/user/1000/pulse
pacmd list-source-outputs
In Java:
Runtime.getRuntime().exec("sh test.sh");
Easier handling for dynamic calling:
test.sh:
#!/bin/bash
export PULSE_RUNTIME_PATH=/run/user/1000/pulse
$1
In Java:
Runtime.getRuntime().exec("sh test.sh \"pacmd list-source-outputs\"");
Here is the official answer from Jetbrains:
How do you launch IDE: from terminal via .sh script or from desktop
launcher? make sure to try to launch it from terminal via .sh script.
Also try restarting the IDE after starting the pulseaudio or restart
the pulseaudio daemon in IDE terminal. Try also these suggestions:
https://bbs.archlinux.org/viewtopic.php?pid=1214072#p1214072
https://bbs.archlinux.org/viewtopic.php?pid=1214157#p1214157
Note that after changing the environment it might be needed to restart
the IDE.

Is it possible to automate ffmpeg-normalize in java?

I need to use ffmpeg-normalize (https://github.com/slhck/ffmpeg-normalize) to properly normalize audio files in my java program but ffmpeg-normalize requires manual installation and command usage.
I thought I could automate it in java using ProcessBuilder but it's been proving somewhat impossible.
ffmpeg-normalize is written in python, so I tried every stackoverflow question that relates to running python commands in java. None of them worked, it seems windows just refuses to allow java to run commands in general.
I added python to my environment variables as well. No dice. Windows keeps telling me it doesn't recognize python nor py nor pip as valid internal or external commands.
This is my final code, I tried all other solutions before this including Runtime.exe, using "cmd /c start", and all other derivatives of that.
My last attempt was running a .bat file from the java cmd processor and have the .bat do the rest, but not only is that windows only, but also fails.
Running pip in a cmd from cmd.exe directly (outside the java program I mean), it runs properly and shows me the help menu for pip. When the cmd opens from the java app, windows refuses to acknowledge the existence of python all together like in the image below.
Process process = Runtime.getRuntime().exec("cmd /c start " + installerBat.getAbsolutePath(), null, ffNormExtractDir);
StreamGobbler errorGobbler = new StreamGobbler(process.getErrorStream());
StreamGobbler outputGobbler = new StreamGobbler(process.getInputStream());
errorGobbler.start();
outputGobbler.start();
try {
process.waitFor();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
I expected the java-born cmd to run the commands instantly and perfectly, but it simply refuses to run them.
So what is my best approach to make installing ffmpeg-normalize a breeze to the user?

Categories

Resources