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.
Related
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%"
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
I am running Cygwin on a Windows 7 machine, and using script files to execute Java programs in batch. My problem is this: I try to pass in a Cygwin / Linux path to a file, via the command line, and Java converts all of the forward slashes to backslashes.
For instance:
java program $scratchname/path_to_folder/ filename_$i.txt
Within Java, I take the directory and add the file name to open the file, which usually works with no issues as long as I'm using a Windows command line. However, in Cygwin Java converts this to:
home\scratch\path_to_folder
which Cygwin doesn't like.
I don't think this is a simple matter of replacing the backslashes with forward slashes, because Java seems to default to the Windows path conventions when I try to open the file. I'm guessing this is because Cygwin is pointed to the Windows installation of the JVM.
How can I force Java to use Cygwin / Linux path name conventions on a Windows system?
Java is a Windows program, and as such, only understands Windows paths; launching it from a Cygwin shell can't change that. You can use cygpath to convert paths back and forth.
Reference link: https://cygwin.com/cygwin-ug-net/using-effectively.html
Example case:
java -jar path/to/your-1.0.jar "$(cygpath -aw /home/YOUR_USER/path/to/file.txt)"
Options:
a provides the absolute path
w uses the Windows format
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.
I'm using a bat file to run my jar. The code in my bat file is this :
#echo off
java -cp analyser.jar be.model.Start
pause
This works fine for windows.
But it doesn't do anything at linux. I also need to be sure it will run on Mac
Bat files are specific to Windows. You would need to execute the command in Linux and Mac in a manner that is specific to those platforms. The actual java call should work the same, I believe. The one change to the java line would be if you had multiple items in the classpath. In that case, you would need to use a colon as a separator instead of a semicolon (which is what Windows uses). (Thanks to khachik for that tip)
For Linux, you would use Shell programming using a BASH script. Here is a link that will describe what you need to do:
http://www.tldp.org/HOWTO/Bash-Prog-Intro-HOWTO.html
For Mac, you would probably use an AppleScript. Here is an article on how to get started with AppleScripts:
http://www.macosxautomation.com/applescript/firsttutorial/index.html
For Linux, why not use a .sh (shell) file?
As Biggs~ alreay said, the actual Java call should remain the same.
Update:
You will also have to make it executable by changing it's user permissions. To do this, use: chmod +x thescript.sh