Calling JDK terminal utilities in an OS agnostic manner? - java

Does anyone have an idea on how to call something from JDK_INSTALL_FIR/bin using:
Runtime.getRuntime().exec(cmd)
Without having to care about OS terminal specifics like escaping or quoting spaces in paths on Windows or concatenating .exe at the end of the command.
In other words I want to make this work on Windows:
Runtime.getRuntime().exec("C:\Program Files (x86)\Java\jdk1.7.0_45\bin\java -version")
I'm open to any solutions like bundling my own JDK, or generally anything that will save me from checking what OS I'm currently running on.
Thanks.

Java path is saved in system variables (mostly)
try something like:
System.out.println(System.getenv("JAVA_HOME"));
Which should return path to java (not sure about "JAVA-HOME" )
It is from:
How can I get System variable value in Java?
Generally I advice using:
System.getenv(String);
method:
getenv documentation

Related

How to use Java to run a command through Mac OS Terminal

I am a high school student working on a project that will convert the video from a YouTube link into an MP3 and download it. However, the way that the video form YouTube is downloaded and converted is through the Mac OS terminal by using YouTube-dl.
This is what I put into the terminal:
youtube-dl -f bestvideo+bestaudio \"ytsearch:{https://www.youtube.com/watch?v=5X-Mrc2l1d0}\"
This works in terminal, but when I try to use:
Runtime.getRuntime().exec("cd /Users/poppa/Desktop/IA Vids");
and there's an error saying "No such file or directory"
Another Problem that I am having is running the code that is inputted into the Terminal from Java. I'm using IntelliJ IDEA if that helps. Thank You!
You have a space in the directory path. Try putting double quotes around like this:
Runtime.getRuntime().exec("cd \"/Users/zeidakel/Desktop/IA Vids\"");
Also note that executing cd command from JVM may have no effect on current user dir when (for example) creating files with new File("path")
If cd means change directory (and isn't the name of an executable), then it almost certainly won't take effect, even if it is executed correctly. The process spawned by exec() will have a working directory, and it can be changed -- but that change will only affect the spawned process.
In addition, having spaces in arguments to exec() is inherently problematic. exec() is not a shell, and you won't be able to protect the string from being split at the spaces by using shell mechanisms like quotes. Instead, you need to split the command into arguments yourself, knowing where the splits should be, and then use the form of exec() that takes a String[] as input. That is, split the arguments into an array of strings yourself, rather than relying on exec() to do it for you (wrongly).
Runtime.exec() is fraught with difficulties, and needs very careful handling. I've written extensively about this subject here:
http://kevinboone.me/exec.html

Why does CMD not recognize the javadoc command?

I installed JDK on windows 10, 64bit, followed the documentation instructions.. CMD does not recognize javadoc command. What I tried:
-copied the path of the "bin" folder from Java and in cmd I wrote the command:
set path = "full_path_to_java_sdk_bin_folder"
I saw this on youtube and it worked for the guy, my cmd still did not recognize javadoc command -I set the PATH in system variables from control panel->system->advanced->environment variables and made sure that there are is no other bin folder...
Didn't find any other tips online...
The usual approach for Java development is to set a JAVA_HOME environment variable, and use that to update the PATH (for one thing, it makes it much easier to support multiple versions of Java). Also of note is that Windows puts the quotes oddly on the command line (oddly compared to every other platform that is) and if your path contains spaces you need to quote it correctly. Like,
set "JAVA_HOME=<full_path_to_jdk>"
set "PATH=%JAVA_HOME%\bin;%PATH%"

OS-independent way of specifying the class path

I have a bash script that bootstraps a Java process. I want it to be able to run on *nix and Cygwin. The problem is the separator in the -classpath parameter is different under the two platforms (: under *nix and ; under Windows).
I can't find an environment variable that specifies this separator (same as $PATH separator), so is there a better way to solve this than detecting the OS and hard-coding?
No. If you write your bootstrap code in Java rather than Bash, you can use the path.separator system property.
Otherwise, it's quite normal to see separate launcher scripts for both Unix and Windows.

Where does Java's ProcessBuilder look to execute commands?

When I execute a command using ProcessBuilder, how does it know where to look for that command? Using this hack/trick I've modified my PATH variable (verified by inspecting processBuilder.environment()) to be bad (empty, working dir, etc) but ProcessBuilder can still execute sort, echo, bash, etc. just fine. How is it doing this?!
Note: My particular development environment is OSX but this code will also run on Red Hat Enterprise Linux.
The documentation says
[...] a command, a list of strings which signifies the external program file to be invoked and its arguments, if any. Which string lists represent a valid operating system command is system-dependent. [...]
Which in essence mean that where it looks for programs to execute depends on the particular system and JVM you're running on.
I can't find a complete matrix of JVM / System behaviors, but supposedly it behaves similar to the popular shells of the system (bash for *nix and cmd for windows) i.e. it searches the directories in the PATH environment variable from left to right and executes the first executable file it finds.
If you want to take control of finding commands, then, well, take control of finding commands. Don't let ProcessBuilder search. Use your own code to find what you want to run, and then put an absolute pathname into the parameter to ProcessBuilder.

How to use mkdir and rmdir commands in a java program

I want to use system commands like mkdir and rmdir while running a java program.
How can I do that?
Why do you want to use the command line? FYI, there are built-in platform-independent File classes.
http://www.exampledepot.com/egs/java.io/deletefile.html
http://www.roseindia.net/java/beginners/java-create-directory.shtml
Make directory:
new File("dir path").mkdir();
Remove directory:
new File("dir path").delete();
'new File' here is a bit of a misnomer, it isn't actually creating the directory or a file. It's creating a Java resource hook which you can use to query or operate upon an existing filesystem resource, or create a new one at your request. Otherwise, use Runtime.getRuntime().exec("command line here") for using command line operations (not advised!!).
Edit: sorted out the problem the question poster was having:
String envp[] = new String[1];
envp[0] = "PATH=" + System.getProperty("java.library.path");
Runtime.getRuntime().exec("command line here", envp);
Note the insertion of envp into the exec(..) method call, which is basically the PATH variable from the environment.
As the other mentioned, you shouldn't do this for simple file management. But to have it mentioned: The Java API has a class called Runtime, that allows system calls... for example:
Runtime.getRuntime().exec("some_command");
The best is not to, but rather find the pure Java API function that does it. It is cleaner, easier to understand and much less error prone. It is also the only way to do Java that is write once run everywhere. Once you are calling shell commands, you are tied to that shell.
In your case you are looking for the java.io.File class, and specifically the mkdir and delete methods.
For reference of people stumbling onto this question and wondering why Runtime.getRuntime().exec("mkdir foo") doesn't work even when incorporating the environment as per Chris Dennett's answer, the most probable reason is that you don't have a program called "mkdir" on your system. While most Unix-like systems have a program of this name, it isn't absolutely necessary for them to have one, and Windows doesn't have one, because in both cases the shell interprets this command itself, rather than passing it to an external program.
To make it work, try ...exec ("cmd /c mkdir foo") for NT-family Windows (or "command /c mkdir foo" for Windows 95 family), or exec ("sh -c \"mkdir foo\"") for Unix.
The fact that there isn't a platform-independent way to do this is yet another reason to prefer the Java APIs for performing the task.
Hi Agree to the fact of not been platform independent but just for testing an app I had to use it.
The solution in my case for the
Runtime.getRuntime().exec("my_command_name") ;
for not working was i had to give the full path to where the batch/sh/executable file was
ie:
Runtime.getRuntime().exec("/d/temp/bin/mybatfile");

Categories

Resources