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?
Related
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
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.
Not sure if this is best for Stackoverflow or Superuser since it pretty much applies to both...
I'm running Lubuntu and I want to create a Windows-like 'start menu' search for the LXDE desktop menu. As with Windows, after an item has been found, I want to be able to launch it.
The LXDE menu system works with .desktop files, so my code performs the following command using Runtime.exec() to start up the programs:
gtk-launch <.desktop filename without extension>
This runs great for 99% of the time. Unfortunately I cannot figure out why that other 1% refuses to launch. One of these applications is TeamViewer. Now here comes the strange part: When I run the command gtk-launch teamviewer-teamviewer10 in a terminal, it works great, but if I run the same command through Runtime.exec(), it does not start, but it does not give me any error messages either.
Here is my code:
Process p = Runtime.getRuntime().exec(new String[] { "gtk-launch", "teamviewer-teamviewer10" });
p.waitFor();
Do I have to modify the gtk-launch command or is there something wrong with my code? (Note: Almost all other programs launch just fine.)
As a workaround, I decided to extract the Exec command from the .desktop file and run this through bash.
String command = getExecCommandFromDesktopFile().replaceAll("\\\\ ", " ");
Runtime.getRuntime().exec(new String[] { "/bin/bash", "-c", command });
This seems to work under all conditions. I still haven't figured out why gtk-launch does not work for all cases, but for me, this workaround will do just fine.
I'm trying to write a small Java command-line application that will create a new file, and then open it with the systems default editor stored in $EDITOR, and then exit after the editor is closed.
So far, without luck, I've tried the following:
Desktop dt = Desktop.getDesktop();
dt.edit(file);
This method resulted in an UnsupportedOperationException, which sort of makes sense as I'm running my program from the terminal, not as a Java appliacation from the desktop.
Right now, I have this:
Runtime rt = Runtime.getRuntime();
Process proc = rt.exec(commandString); // "vim newfile"
proc.waitFor();
This is working, but not how I need it to. When I run
ps a | grep vim
I can see that it is indeed running in the background, with the filename I've given it:
1000 pts/1 S+ 0:00 vim 2014-07-16.23-02
Any ideas on how to make this run in the foreground?
vim, like many interactive programs, expects its stdin to be a real terminal that it can send ioctl calls to. But when executing through Runtime.exec() stdin will be redirected to the parent process (see the Javadoc on Process for more information).
In Java 7, you should be able to use ProcessBuilder.inheritIO() to pass along the file handles. (Disclaimer: I haven't tried it, YMMV.)
I'm trying to create a new process in a new window. I'm following this example for the command Running a command in a new Mac OS X Terminal window (using Java Process builder). I printed out the resulting command and it works if I type it in manually, but it doesn't start the new window when I run it.
Something as simple as osascript -e 'tell application "Terminal" to do script "java -jar test.jar"' doesn't bring anything up
Sample code:
try {
ProcessBuilder pb = new ProcessBuilder("osascript", "-e",
"'tell application \"Terminal\" to do script \"java -jar test.jar\"'");
Process p = pb.start();
} catch (Exception ex) {
ex.printStackTrace();
}
It doesn't even bring up an error (as it should if it were starting the java process in a new terminal window)
And as usual although I spend hours looking into my problem I find the silly solution after I ask stack overflow... don't need the single quotes around tell application.... Process builder does that. Er, not sure if question needs to be closed. Quite localized, but certainly confusing IMO