I am facing a problem where I need to run a bat file from java program.
This Batch file contains certain SET ms-dos command which i need to access further down in my application.
I used ProcessBuilder Class to invoke the batch file but processBuilder.environment() is not returning me the variables set by the batch file.
DOS SET command sets the variables on that prompt only.
I am unable to find the solution for this problem.
Is there any ulternate approach for such issue. I cant change the Batch file.
Regards
Abhay
System.getenv lets you get an environmental variable. It should work in this context too. There is another version which returns a Map of environmental variables.
Your ProcessBuilder instance holds the environment variables used by the child process. You should reference processBuilder.environment() rather than referencing the environment variables for the parent process found in System.getenv().
However, you may find that it's still a one-way street. You can set environment variables there to pass to your process shell, but the changes made by the SET commands will not survive the return trip.
So, here's an alternate approach. Write a batch file as a wrapper script and have that output your desired variables to somewhere more easily accessed, like stdout, stderr, or a file.
wrapper.bat
===========
#echo off
call "D:\build\XL_7_12\XL_7_12\build\xl_env.cmd"
echo ROOT=%ROOT%,FOLDER=%FOLDER%
Then you call your wrapper.bat from Java and collect and parse the ROOT and FOLDER values from the last line of your process.getInputStream().
Another approach would be to redirect the output of the ECHO command to a file, then read this later from Java. eg.
echo ROOT=%ROOT%,FOLDER=%FOLDER% > \temp\vars.txt
Then read \temp\vars.txt from java. This might be a little easier than reading the input stream. Finally, be aware that instead of a wrapper.bat file, you can issue two commands to a single copy of the command.exe interperter,, thus sharing environments, so you could:
D:\build\XL_7_12\XL_7_12\build\xl_env.cmd & ECHO %ROOT% > \temp\vars.txt
Related
I am setting environment variable in my machine using export MY_KEY=foo. And I am trying to fetch it in JVM using System.getenv("MY_KEY"). This returns null. But running echo $MY_KEY shows foo on the terminal.
I have tried restarting the IDE. Doesn't work, still.
The environment variable is only available to sub processes of the shell that exported it. Did you start your IDE from that shell?
If you want the variable to be available all the time, you need to add it
to the /etc/profile file or create a extra file in /etc/profile.d. It depends on your operating system.
I added some exportable environment variables in my bash_profile and my profile files in the following format export "X=y". This worked as it should, now I want to remove them permanently . I've tried the following methods:
Deleting the exported variables from the bash_profile and the profile files, and saving the files, and calling source ~/.bash_profile and source ~/.profile.
Called "unset X" in terminal window. Before I could call "echo $X" which would display y, now it displays nothing.
Rebooting the Mac Computer.
Still however, whenever I call Java's System.getenv(X), env variable X's value (y) is still returned. What else do I need to do to completely eliminate an env variable from my system in MacOS Mojave?
It appears you believe environment variables are global. That is, modifying the value in one program, such as a running terminal, will affect the value in a different program. That is not how env vars work in a UNIX like OS. Each process is provided a copy of the env vars provided by the parent process. That is, they are inherited from the parent process.
The fact that you have to unset X in a terminal to remove it means that either it is being inherited by the shell from the terminal process or your shell is setting it. In the latter case the specific files read when a shell starts depends on the shell. But /etc/profile is read by most interactive shells so you might want to look there.
You say your Java app is run by Tomcat but failed to mention how Tomcat is started. That is important for the reasons I mention above.
Note that macOS uses a daemon named launchd to manage running most services. Those services are configured via "plist" files. See man launchd.plist. Those launchd config files support defining custom env vars. That is slightly unusual but worth looking at. See if any of the files in ~/Library/LaunchAgents, /Library/LaunchAgents, or /Library/LaunchDaemons mention the env var.
I would like to run a Java program, which is a standalone application, from Ruby. I am using the following commands in Ruby:
system("cd /home/webserver/testproject");
system("sh testsh.sh")
My Java project is available in "/home/webserver/testproject", so I am changing the directory using the first command. The sh file writes a file which contains info to run my main class and also sets the CLASSPATH environment variable to run program. When doing this outside of Ruby it's working properly but not inside Ruby.
system makes a new subshell every time you run it, so you always start in the current directory. What you need is to change the directory inside Ruby with Dir.chdir:
Dir.chdir("/home/webserver/testproject") do
system "sh testsh.sh"
end
The change in the working directory will only be applied to the code inside the do … end block. If you want to make the change permanent for the whole script, you can do it this way:
Dir.chdir "/home/webserver/testproject"
system "sh testsh.sh"
Calling system("cd ... changes the current working directory only inside the scope of the command. To run the second command in the correct directory, you need to chain them:
system("cd /home/webserver/testproject && sh testsh.sh")
I have a Java program utility that I want to execute as a command in cmd. I added the location to the PATH variable, but java programs needs to be executed using java - jar "...". How do I shorten that to just the program name, like mysql or netstat?
Update:
I neglected to mention that this java program takes arguments of its own to handle its tasks, so the batch program would need to pass the arguments passed to it over the the java program. I'm not skilled enough in batch to know how to do this.
~Jacob
You could create a batch file or bash script (depending upon your OS) that calls the program with the proper java -jar commands, and simply name the batch (or bash) script whatever you would like to enter as the command. Place this in a directory that is in your PATH variable, and have at it.
Edit: Read this for info on how to parse command line parameters in batch scripts. Just take the parameters passed to the batch file, parse them, and pass them to your jar file with:
java -jar jarfile.jar param1 param2 ...
So for example, lets's assume that your program takes two arguments. Your script could then be as follows:
java -jar jarfile.jar %1 %2
I am not an expert in batch files by any means, so there is probably a more proper way to do this. That being said, why over complicate things?
With Launch4J you can wrap a Java program in a standalone executable file. I'm not going to copy their (long) feature list here, but definite highlights are the numerous ways presented to customize the resulting exe, its small size, the fact that it's open source and its permissive license that allows commercial usage.
I've been using ProcessBuilder to successfully invoke a process with various environment variables using env.put("VAR","value").
Now I'd like to source some bash scripts to set a whole bunch of environment variables that are not predetermined within java.
Anyone know of an easy way to do this?
bash supports the environment variable BASH_ENV on startup. Set the variable to your script and its contents will be sourced before execution. See bash(1) for details.
If your "batch scripts" to be sourced are in properties format, you can always load them using Properties and merge into the env.