JAVA_HOME is not defined correctly (only from jenkins) - java

Trying to setup jenkins but my builds fails with:
$ ant test
Error: JAVA_HOME is not defined correctly.
We cannot execute /usr/lib/jvm/java-7-openjdk-amd64/jre/bin/java
Build step 'Invoke Ant' marked build as failure
If I manually run "ant test" on the machine it works just fine and the JAVA_HOME is set to the exact same value. Any idea why it fails when jenkins try to run it ? Are there any more environment variabled involved ( I could not see any though ) ?

For others, I had to add the PATH /usr/bin/ to my PATH variable within Jenkins. (Find your correct path using which java).
Jenkins > Manage Jenkins > Configure System
Add an Environment Variable >>
e.g:
Name: PATH
Value: /usr/local/bin/:/usr/bin/
Screenshot

The problem was this, I had forgot to check the box "Restrict where this project can be run" in the project configuration. Thus the testing tried to execute on "master" where JAVA_HOME was not the same as expected on the build executor I intended to run it on. Thus where I tested and where it actually ran was different machines.

Set JAVA_HOME in your Jenkins system config in Jenkins 2.107.1.
open your jenkins, e.g. http://192.168.1.12:9090, then add /configure to the url,
that is http://192.168.1.12:9090/configure, then you can find like next:

For me the options above did not help, solved by creating a link to what's asked:
sudo ln -s ${actual_java_location} /usr/lib/jvm/java-7-openjdk-amd64/jre/bin/java
actual_java_location can be read by this:
readlink -f $(which java)

Related

Intellij Idea not able to execute git pre-push hook

I would like to execute a git pre-push hook from the IntelliJ IDEA.
Git pre-push hook can able to execute via a Terminal but not from the IntelliJ IDEA.
I have posted the Version Control console:
And Its saying JAVA_HOME is not set.
Used Versions
Java : 1.8.0_211
IntelliJ IDEA : IntelliJ IDEA 2019.2 EAP (Ultimate Edition), Build #IU-192.4787.16
Ubuntu : 18.04
Git : 2.17.1
Git pre-push hook script
source ~/.bash_profile
remote="$1"
url="$2"
echo “Running Tests”
pwd
./gradlew test
exit $?
I am not getting the where is a problem exactly and How can I solve this issue ?
The issue is that there is no JAVA_HOME variable defined in the environment the hook is run in. The environment is inherited from the parent process, so depending on how and when IntelliJ is started, its environemnt
The easiest way to fix the issue is to define JAVA_HOME in the hook itself (or maybe in .bash_profile), e.g. by adding export JAVA_HOME=<PATH to your Java Home> in it. This way variable will be set during hook execution and gradlew will not fail.

VS Code Showing 'mvn' is not recognized as an internal or external command

I'm trying to create a maven project with VS Code but when I run the command it says :
'mvn' is not recognized as an internal or external command,operable program or batch file.
but mvn -version is running on command prompt
Environment varaible for User :
MAVEN_JOME : C:\apache-maven-3.6.1,
M2_HOME : C:\apache-maven-3.6.1,
JAVA_HOME : C:\Program Files\Java\jdk1.8.0_212\jre and
System Variables
path is set to C:\apache-maven-3.6.1\bin, C:\Program Files\Java\jdk1.8.0_212\bin for maven and java respectively.
The command I'm running to create the project is:
mvn archetype:generate -DgroupId=com.cs.test-project -DartifactId=test-project -DarchetypeArtifactId=maven-archetype-quickstart -DinteractiveMode=false The Reference to Command
And the VS Code Reference to Command
The same command is running in Command prompt and it created the project successfully on Desktop.
I fixed this problem by simply restarting VSCode as I had set my PATH variable while my VSCode was running so the changes were not reflected to the VSCode integrated terminal until I restarted it.
It's kindy counterintuitive as path has to be pointed to the actual mvn command, instead to a M2_PATH folder. Also once properly pointed, a new error will be shown if your environment is missing proper jdk within JAVA_HOME. The error is trown by maven. Maven plugin is kinda stupid as it will ignore your default JDK configured within settings.json (java.home or/and java.configuration.runtimes properties) and will happly NOT set java for maven. You have to configure it specificly for maven plugin as this:
"maven.executable.path": "c:\\apache-maven-3.8.1\\bin\\mvn",
"maven.terminal.customEnv": [{
"environmentVariable": "JAVA_HOME",
"value": "c:\\openjdk-1.8.0_232-redhat",
}]
Of course, both paths should be pointed to proper folders/files in your environment.
The way I fixed was by changing the exec path in Vs Code settings from CMD to Powershell.
Open
settings -> features -> terminal
Change the windows exec path from cmd to where you have PowerShell.
For example: C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe
Windows exec setting
On Tuesday Oct. 25, 2022 while trying to solve the same problem, I went through all of these previous solutions for Windows and finally had to resort to the official installation docs:
Installing Apache Maven
Unzipped the download and located the bin directory and manually added that directory location to my: System Properties>Advanced>Environment Variables>'Path'
For good measure, restart the computer.

Bazel and System Envirentmental Variable

I am building my java project with bazel. I want to use environmental variable at build/run time. According to the documentation --action_env=APP_ENV=Development should work.
So i do bazel run myproject:app --action_env=APP_ENV=Development
But System.getenv("APP_ENV"); is null
In my intellij IDE I have the following. Neither --action_env=APP_ENV=Development nor --action_env APP_ENV=Development work
Did i get something wrong here?
You can use --run_under to add a prefix to the Bazel run command. This can be used in the IntelliJ Run/Debug configuration to set environment variables.
--run_under='export VAR1=FOO VAR2=BAR && '
Launch Intellij from a terminal so it has the environment variables that it needs during bazel sync (build) and bazel run.
On MacOS, use the open command to launch it.
$ export POSTGRES_USER=dev_user
$ open -a "IntelliJ IDEA CE"
$
Bazel will only pass variables to the build processes if you provide them on the command line:
$ bazel build --action_env=POSTGRES_USER //...
Or specify them in .bazelrc:
build --action_env=POSTGRES_USER
The current version of the Intellij bazel plugin allows you to specify a bazel binary. You can take advantage of this to add specific environment variables per run configuration.
Let's say you want to add the following to the runtime environment:
VAR1=VAL1
VAR2=VAL2
Then create a file called say bazel_custom with the following contents (replace /path/to/bazel with the correct path to bazel):
#!/usr/bin/env bash
export VAR1=VAL1
export VAR2=VAL2
/path/to/bazel "$#"
Save the file and mark it as executable:
chmod +x bazel_custom
Then edit your Intellij bazel run configuration and set the path to the task-specific bazel binary to /path/to/bazel_custom.

package org.junit does not exist

I am using Jenkins, and using a Github repo as Source Code.
In the Build section, I am executing this as a Windows Batch command:
set path=%path%;C:\Program Files\Java\jdk1.8.0_144\bin
cd \Users\harr\JenkinsServer\JenkinsTest\src
javac SimpleTest.java //Error is after this executes
java SimpleTest
I know it has something to do with classpath, but I am unsure how to solve this problem in jenkins.
Let me know if more information would be helpful.
Suppose you deploy the jekins server on linux platform, so you have to install the jdk, tomcat and so on, set the env path as well. Then you don't have to execute set path before every build.
you can create a script and copy the command into it, then when jenkins performs the build task, it can execute the script. Refer to the jenkins tutorial to learn about it.

Ant unable to launch, giving a main class error

I'm running Elementary OS (Ubuntu 12 based), and I'm having issues running apache ant. It was working earlier before a restart, so I'm not sure what would've changed.
I've defined environment variables in /etc/environment as follows:
PATH="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:$ANT_HOME/bin"
JAVA_HOME="/usr/lib/jvm/java-1.7.0-openjdk-i386"
ANT_HOME="/opt/ant"
ANT_OPTS="-Xms256M -Xmx512M"
So my Java and Ant environment variables should be set. I'm trying to deploy with ant, with 'ant clean deploy', but I get an error in my terminal:
Error: Could not find or load main class org.apache.tools.ant.launch.Launcher
I've tried "source /etc/environment". Running 'echo $ANT_HOME' shows the correct path. I've tried moving ant to a different location and resetting the variables. Nothing. I'm kind of lost. Please help!
you should define $ANT_HOME before using it in your $PATH
JAVA_HOME="/usr/lib/jvm/java-1.7.0-openjdk-i386"
ANT_HOME="/opt/ant"
PATH="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:$ANT_HOME/bin"
double check that you have ant-launcher.jar under $ANT_HOME/lib
As others have stated, it looks like ANT_HOME isn't being set before you declare it in your path.
I take a slightly different approach to these things. Instead of constantly updating my PATH, I usually set my path to something fairly simple:
PATH="/usr/local/bin:/bin:/usr/bin/:$HOME/bin"
Then, I make hard links to various binaries into my /usr/local/bin directory.
$ cd $ANT_HOME/bin # All the Ant binaries
> for binary in *
> do
> sudo ln -s $PWD/$binary /usr/local/bin
> done
Now, I don't have to add $ANT_HOME/bin to my $PATH. Even better, if I include /usr/local/bin before /usr/bin and /bin, I am picking the binary in /usr/local/bin first. This way, I can ensure I run the version I installed over the default. For example, my machine comes with Ant 1.7 in /usr/bin/ant but I want to use Ant 1.9.1. I can install Ant 1.9.1, and that will be my default version.
I had same error when install ant with npm install. When I tried install from official repository throw pacman -S apache-ant(apt-get install apache-ant -- for Debian/Ubuntu) it's start working proper for me.
I'm experiencing this bug with jdk 1.8. But, I came across this RHEL 6.5 bug for OpenJDK 1.8 which may be related:
https://bugzilla.redhat.com/show_bug.cgi?id=1149605
Essentially, it's fixed in jpackage-utils-1.7.5-3.13 which is included with RHEL 5.7.
The problem, as Javier Ramirez said in the bug comments:
Your script /usr/share/java-utils/java-functions has problems with
"openjdk version" because it expects "java version" as Java 7 does.
$ mkdir /usr/share/java-1.8.0
$ mkdir /usr/lib/java-1.8.0
$ diff /usr/share/java-utils/java-functions.orig /usr/share/java-utils/java-functions
149,150c149,150
< -e '/java \(full \)*version "/s/'$re'/<<<\1>>>/' \
< -e '/java \(full \)*version "/s/.*<<<\([^>]\{1,\}\)>>>.*/\1/p')
---
> -e '/[java|openjdk] \(full \)*version "/s/'$re'/<<<\1>>>/' \
> -e '/[java|openjdk] \(full \)*version "/s/.*<<<\([^>]\{1,\}\)>>>.*/\1/p')
------
I am using RHEL which comes with ant (in /usr/bin/ant) and the ant libraries in
/usr/share/ant. As suggest above, ant -version gave the "could not find" error.
I installed my own version of ant (in /home/Ant since I have a lot of space
in /home) and put ANT_HOME in my .bash_profile and $ANT_HOME/bin in my PATH
and $ANT_HOME and $ANT_HOME/bin (for good measure) in my CLASSPATH. Then ant
worked (when used as myself, not as root).

Categories

Resources