I would like to know if it is possible to pass JAVA_HOME as parameter to mvn command line. I have searched almost everywhere, but couldn't find an answer to that.
I know we can set JAVA_HOME using export, but I need to pass it as parameter if that is possible. Something like:
mvn install -DJava_Home=/usr/java/jdk-1.7.0
Another hacky way I did this: ( I have most of my projects on Java 7, but a handful on java 8 )
1) Add a new env variable JAVA8_HOME to your .zshrc ( or similar )
2) copy the 'mvn' executable and call it 'mvn8'
3) Replace 'JAVA_HOME' with 'JAVA8_HOME' in 'mvn8'
Now, mvn8 clean install should just work.
no, not directly, but looking at mvn.bat on my machine i see this promising snippet:
#REM Execute a user defined script before this one
if not "%MAVEN_SKIP_RC%" == "" goto skipRcPre
if exist "%HOME%\mavenrc_pre.bat" call "%HOME%\mavenrc_pre.bat"
so you could override any variable you like in a mavenrc_pre script file, although i realize this wont let you override java home from the command line.
worst case, mvn is a simple script file and you could add the option to it. also note that simply overriding JAVA_HOME may not always produce the expected results as on many systems JAVA_HOME\bin is on the path. this means that even if you override it the previous jvm will still be on the path, which might lead to unexpected results.
Related
I want to execute some old code in java, but it's becoming complicated with old versions of it. Therefore, I wanted to create my "Java aliases" so the command "java11" would execute the java 11 binary command. Unfortunately, on mac, I was not successful.
I tried to follow tutorials wanting to add an alias part in your ~/.zshrc. I did that, and it works perfectly, but it doesn't work for bash scripts, which is really annoying.
Here is the line for my java11 alias in the .zshrc:
alias java11="/Library/Java/JavaVirtualMachines/jdk-11.0.11.jdk/Contents/Home/bin/java $#"
(I don't know if it would do anything, but I also added the alias lines in my .bashrc)
Aliases have limited usefulness for things like this. They need to be configured in a shell initialization file (and which file is used depends on the shell, what mode it's started in, etc), they're disabled by default in scripts, and they aren't available at all when the command is executed by something other than a shell (e.g. in sudo java11 ..., it's sudo that does the execution, and it doesn't know anything about aliases).
I'd recommend creating an actual command instead. Since this is just another name for an existing binary (/Library/Java/JavaVirtualMachines/jdk-11.0.11.jdk/Contents/Home/bin/java), you can just use a symbolic link to the original binary (for something more complex, it might make sense to create a wrapper script instead). /usr/local/bin doesn't exist by default in macOS, but it is in the default PATH (in most contexts except cron jobs, grrr), so if you create it, it'll be searched automatically. So something like this should work:
sudo mkdir -p /usr/local/bin
sudo ln -s /Library/Java/JavaVirtualMachines/jdk-11.0.11.jdk/Contents/Home/bin/java /usr/local/bin/java11
In your running zsh sessions, you'll probably have to run rehash to get them to notice the new binary. And remove the alias, to keep it from overriding the new command.
i'm very new to this world.. so forgive me for silly explanations.
I've installed maven and all the setting is done. Also i created a maven.sh
Content:
# Apache Maven Environment Variables
# MAVEN_HOME for Maven 1 - M2_HOME for Maven 2
export JAVA_HOME=/usr/lib/jvm/java-11
export M2_HOME=/opt/apache-maven
export MAVEN_HOME=/opt/apache-maven
export PATH=${M2_HOME}/bin:${PATH}
At this point everything is working as expected, but every time i close the terminal it seems to forget some settings..
I have to open the terminal and execute the following commands to make it work again:
~$ cd /etc/profile.d
~$ source maven.sh
I don't know if it is just the way to use maven or i'm missing something, but it's kind of annoying writting the same commands every single time i want to execute maven.. so i would appreciate if you can explain me a way to automate it.
Some extra information:
OS : Ubuntu 20.0.4
JDK : 11
Not first time creating JAVA_HOME variable
After executing those commands above, it shows the correct JAVA_HOME, but it's forgotten after closing the terminal and shows the wrong JAVA_HOME again
Let me tell you guys, this is my first question on stackoverflow, so i will understand if you want to correct me about my manners and my bad english (it's not my mother tongue, i have to get used to it)
Thank you in advance!
I can think of a couple of ways you could resolve this problem given your situation, which is not maven-specific but rather terminal-specific --
Add a reference to your maven.sh script to your profile so that it runs when you start a terminal session. This can typically be accomplished by creating (or adding to) the .bash_profile file in your home directory. You could either add the contents of your maven.sh script to that file, or add source /etc/profile.d/maven.sh to it. From that point forward when you start a terminal session, the script gets run automatically and your variables are set correctly. See https://joshstaiger.org/archives/2005/07/bash_profile_vs.html or the bash man page for some more details about the login shell
If you've already done the first step and it didn't work, consider checking to see if those environment variables are being set in another place (check ~/.bashrc, ~/.bash_profile, and anywhere your particular shell distribution / OS checks for startup scripts
Lastly, I recommend using SDKMan! to manage maven installs without these headaches -- but your mileage may vary. It handles keeping track of the environment variables, java version, &c and allows multiple Java/Maven versions to be installed and managed. https://sdkman.io
Sources:
https://joshstaiger.org/archives/2005/07/bash_profile_vs.html
https://linux.die.net/man/1/bash
https://sdkman.io
You can add these properties to individual user settings file. This file will be available in home directory
vi $HOME/.bash_profile
So everytime, you login to terminal, all these settings will be executed.
I solved the problem by doing a logout and login in Ubuntu
Here is a line in file ~/.bash_profile
export MESSAGE="Hello World"
I want to access system variable MESSAGE in java.
System.getenv("MESSAGE"); doesn't work.
The .bash_profile file is only sourced for login shells. If your java process is spawned from a shell that is not a login shell (for example a script with a #!/bin/sh at the top) then it will not read it (though it may still inherit MESSAGE from the environment depending on how you run it).
Note also that .bash_profile is also not run for interactive shells that are not "login" shells, so you can't rely on it being run even when you have a shell prompt. People usually use .bashrc, which is sourced for all interactive shells, for that purpose.
If you want a variable to be set in all Bourne shell derivatives regardless of whether they are interactive or not, put it in both .profile and .bashrc.
I would recommend you to test if your environment variable is indeed "defined" by using echo $MESSAGE as suggested above.
In addition, changing the bash_profile file will not effect your current shell, you must use "source" in order for it to effect your current shell.
I would also recommend reading this article about differences between bashrc and bash_profile.
Maybe you should define the EXPORT at bashrc?
This actually gets even more interesting for users who have a .profile (for the old Bourne shell), that gets read in by .bash_profile automatically (providing compatability). In either case, the environment variables are read in once only at login shell startup, and all subshells inherit those variables for free. The .bashrc is for tty-dependent things and unheritables like functions (the old sh used $ENV, I think, if it were set, for something similar).
Your use of ~/.bash_profile looks fine (although single quotes are more reliable than double quotes, which allow some substitutions). Make sure you've logged out and back in between editing that file and trying your test, or use ". ~/.bash_profile" (no quotes and note the leading dot and space, since the dot is the command here).
The article at http://www.joshstaiger.org/archives/2005/07/bash_profile_vs.html covers some good things, like using ". ~/.bashrc" at then end of your ~/.bash_profile (excepth that you should use -r, not -f). The comment about using export in your .bashrc is wrong, you should not do this, for two reasons (1) an pretty insignificant performance penalty, (2) a pretty high chance that some command you execute won't get the environment variable - particularly things spawned from your window manager menus and other places where an actual command prompt didn't appear in any of their parents.
Lastly, make sure the $MESSAGE actually exists in your environment - look at the output of the "env" command - if it isn't there, the Java process won't see it unless it's internally creating it and storing it in its own internal copy of the environment variables.
Another note, if you set env variables in the .profile, use this syntax:
VAR=VALUE ; export VAR
...since the old sh shell doesn't support "export VAR=VALUE". Using set -e before a bunch of these and set +e after can remove the need to use "export" at all, if I remember correctly.
Another place to look at: /etc/environment (this may overrule/replace your .bashrc or .bash_profile in shells opened via your IDE)
Variables in your bash profile only get used in the terminal session you are in, so IntelliJ won't pick them up.
Instead, go to Edit Configurations in your Run Configurations box, and add your environment variables in a semi-colon separated list in the environment variables box!
I do most of my work against JDK 1.5 - but occasionally I have to change to 1.6. it is a bit painful to have to manually go and change my 'JAVA_HOME' system variable whenever I need to work on a project specific to one or the other (and no, Eclipse doesn't play well in these scenarios - trust me...I've tried.)
I'm looking for a registry script or windows shell script or for that matter any means by which I can "toggle" this system variable with something that is easy to run.
I've messed with the 'set' command, but that only sets the variable for that particular command instance - not globally.
Thanks in advance.
EDIT #1:
Points of advise:
Use the JAVA_HOME variable in your path variable as well, that way you only have to change the JAVA_HOME (which is used in many projects anyways [maven, ant, etc])
Write the command into a couple batch scripts for easy use
When you make the change the windows command session will not reflect it right away. You must close and reopen it.
You could use setx for that purpose
Like so:
setx /M JAVA_HOME "C:\Program Files (x86)\Java\jdk1.6.0_17"
I'm trying to get my Apache Lucene demo to work and I'm down to setting the classpath in this tutorial http://lucene.apache.org/java/2_3_2/demo.html
I've hunted the web and these wer the 2 solutions I found to set CLASSPATH:
CLASSPATH=${CLASSPATH}:/Users/philhunter/Desktop/COM562\ Project/lucene-3.0.3/lucene-core-3.0.3.jar
and
setenv CLASSPATH ${CLASSPATH}:/Users/philhunter/Desktop/COM562\ Project/lucene-3.0.3/lucene-core-3.0.3.jar
The second one brings up a error
-bash: setenv: command not found
The first one seemed to accept ok but wen i tried the next step in the tutorial i got an error. The next step was to run the following:
Phil-hunters-MacBook:webapps philhunter$ java org.apache.lucene.demo.IndexFiles /Users/philhunter/Desktop/COM562\ Project/lucene-3.0.3/src
which gave me the error:
Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/lucene/demo/IndexFiles
This leads me to believe my CLASSPATHS didnt set correctly. Would I be right in assuming this? I have tried other tutorials and demos and see to get this same error quite a bit. Im new to Lucene and relatively new to mac and Unix shell scripting. Anyone know if I am setting the CLASSPATH correctly and if thats the cause of the errors?
in the terminal type
$ vim ~/.bash_profile
edit the file and add one line:
export CLASSPATH=${CLASSPATH}:/usr/local/lucene-3.6.2/lucene-core-3.6.2.jar:/usr/local/lucene-3.6.2/contrib/demo/lucene-demo-3.6.2.jar;
make sure to change the path of yours.
In your way you lose to add lucene-demo-3.0.3.jar in your classpath.
When you set an environment variable like CLASSPATH then by default it only applies to the current process (i.e. the shell process itself) - it isn't available to the java process you launch in the next line. In order to make it available to other processes you need to "export" the variable. In this case you can use something like:
export CLASSPATH=${CLASSPATH}:/Users/philhunter/Desktop/COM562\ Project/lucene-3.0.3/lucene-core-3.0.3.jar
This basically says "set the CLASSPATH variable to its current value plus the location of the lucene jar, and make the new variable available to any processes launched from this shell".
However, with java the usual way of setting the classpath is to do it as part of the java command itself, using the -classpath or -cp options. In your case it would look something like:
Phil-hunters-MacBook:webapps philhunter$ java -cp /Users/philhunter/Desktop/COM562\ Project/lucene-3.0.3/lucene-core-3.0.3.jar org.apache.lucene.demo.IndexFiles /Users/philhunter/Desktop/COM562\ Project/lucene-3.0.3/src
As an aside, the error you see when using the setenv line is because setenv is the command used in the C shell to set environment variables, but the default Mac shell (and the shell you're using) is bash which doesn't recognise setenv and lets you know it doesn't recognise it with the error message: -bash: setenv: command not found.
i create a .bash_profile file in my home directory and do things like
export GRAILS_HOME=/usr/share/grails
...
export PATH=${GRAILS_HOME}/bin:${GROOVY_HOME}/bin:/usr/local/mysql-5.1.45-osx10.6-x86_64/bin:${PATH}
you can work of that to set the classpath -- these examples show how to declare an environment variable and how to use the variable in other variables.