The error you got only happens when Jenkins does not have permission as a sudo user on the Linux machine.
This works perfectly fine on windows but in Linux, you need to provide sudo permissions.
To get over the error follow these steps:
Step 1: open terminal
Step 2: $sudo nano /etc/sudousers
Step 3: at the end of the file add the following statements:
- Jenkins ALL=(ALL) NOPASSWD: ALL
- Save and exit
Step 4: Next is you have to go to your job->configure->build triggers->build periodically on your Jenkins page and add sudo in front of your compile statement
It’ll look like this:
Cd /file_path/
Sudo javac filename.java
Java filename
You can learn more about Jenkins by enrolling for Jenkins Training.