2

I am using Jenkins to schedule my selenium/java automated tests (test scripts packaged into a .jar file).

I have created a .bat file to trigger the .jar file and i am calling the .bat file from Jenkins.

When I manually trigger the tests, by double clicking on .bat file, everything runs smoothly. But when I try to run the same through Jenkins, I am getting the following error for each test case:

"The system cannot find the file specified"

Jenkins is calling the .bat file successfully, because I can see the test case name (which is present within .jar file) displayed on the jenkins console.

In my java code, I have used .\ to represent the "current directory" where my jar file is present along with an excel file which data drives the automation framework. Even the .bat file is present in the same location, and I have given fully qualified path on both .bat file and on jenkins.

I can't hardcode and will have to use ./ in my java code. And I am running this on Windows Server (not my local machine).

mrfreester
  • 1,981
  • 2
  • 17
  • 36
Shreyas SG
  • 368
  • 3
  • 6
  • 21
  • Maybe [this](http://stackoverflow.com/a/25440709/2861476) could help. – MC ND Apr 04 '17 at 15:29
  • Which directory do you run it from when you run it manually? Have you tried to have Jenkins change to that same directory before running the `.bat` file? My guess is this is a working directory issue with some other file than your `.bat` or `.jar` file. – mrfreester Apr 04 '17 at 16:11
  • @mrfreester - I run it from C:/Desktop/QA directory, manually.. in jenkins, under configure build action, i have selected 'windows bat file' and have given full path - C:/Desktop/QA/trigger.bat Also, in the bat file, i am setting PATH and CLASSPATH values to C:/ProgramFiles/java/jdk1.7 and C:/Desktop/QA directories, respectively. – Shreyas SG Apr 05 '17 at 04:56
  • 1
    @ShreyasSG try `Execute Windows Batch Command` instead for your build step. On the first line, put `cd C:/Desktop/QA/`. On your second line try `trigger.bat` or `C:\Desktop\QA\trigger.bat`. Otherwise, you would probably have to figure out what in your script is trying to use the working directory. If that works I'll put it as an answer, let me know :) – mrfreester Apr 05 '17 at 14:32
  • Something to also consider, are all your dependencies on the Jenkins box? All the web drivers or other files you reference? Is there any more detail in the `cannot find the file` error you're seeing? – mrfreester Apr 05 '17 at 14:39
  • Another way to get around this probably would be [to look at this question/answer](http://stackoverflow.com/questions/320542/how-to-get-the-path-of-a-running-jar-file) instead of `.\ ` in your java code to reference the `.jar` directory instead of the current working directory. This is because `.\ ` won't necessarily be the directory your `.jar` file is in:) – mrfreester Apr 05 '17 at 14:46
  • @mrfreester, Tried your 1st option above, was able to progress to an extent. I have page URLs on an excel sheet and that excel is referred by .jar file to launch browser and perform tests. Jenkins is just printing the excel URLs and not launching the browser. That means, Jenkins executed my .jar file ---- 2nd option - What do you mean by "dependencies on Jenkin box"? can you please elaborate? I'm new to this :( ----- 3rd option - I am storing .jar file and excel file and everything in the same location. – Shreyas SG Apr 10 '17 at 07:41
  • @ShreyasSG Basically everything on your local computer that your tests require to run also need to be on the machine Jenkins is running on, preferably in the same location. I'm not sure what WebDrivers you're using, but you'll need those on the Jenkins machine as well if you're using them locally. Are you sure Jenkins is not launching the browser? Do you get an exception? If you run tests through Jenkins, you might not see the browser launch because it's as a different user on a different desktop, but that doesn't mean it's not running the test. – mrfreester Apr 10 '17 at 14:32
  • To test, you could add a `println` after you navigate to the url in your code. If you see that output you know the browser launched successfully, because it'll throw an exception if it fails. – mrfreester Apr 10 '17 at 14:33
  • The problem was with the installation of Jenkins. A devops engineer solved this for me. Had to install Tomcat and jenkins within that. He said "If Jenkins is installed as UI, it can't open another UI. Hence we should install .war file on a separate server which creates separate UI for jenkins, and my application UI (firefox) can be launched on the local". – Shreyas SG Apr 10 '17 at 17:15
  • Sounds like this was 2 problems, the first was the issue with the working directory, and the 2nd, not related to the question, was with the Jenkins installation not setup to be able to open a browser. Glad you got it worked out :) – mrfreester Apr 10 '17 at 17:18
  • You can also try following method mentioned at https://stackoverflow.com/questions/24733917/jenkins-cant-see-mapped-drive?utm_medium=organic&utm_source=google_rich_qa&utm_campaign=google_rich_qa – helloworld9 May 16 '18 at 19:46

5 Answers5

0

identify the current dir path where .bat file is placed and append / add to system path variable. Once done then allow jenkin to execute .bat file.

  • Where to find path variable? In jenkins? In bat file, i am setting PATH and CLASSPATH values to C:/ProgramFiles/java/jdk1.7 and C:/Desktop/QA directories, respectively. C:/Desktop/QA has all my files, bat, jar, excel... – Shreyas SG Apr 05 '17 at 05:00
0

This is an issue with your working directory being different when you're running on Jenkins.

Easiest fix: Execute Windows Batch Command instead for your build step. On the first line, put cd C:/Desktop/QA/ to change your working directory to match what you're doing locally. On your second line use trigger.bat or C:\Desktop\QA\trigger.bat.

More robust fix: In your java code, you're using .\ when referencing other files. This basically means current directory, which won't be what you want if you're in a different working directory. Instead, you can look at this question/answer which shows how to reference the directory where your .jar file is instead of the working directory. In summary do this:

return new File(MyClass.class.getProtectionDomain().getCodeSource()
    .getLocation().toURI().getPath());
Community
  • 1
  • 1
mrfreester
  • 1,981
  • 2
  • 17
  • 36
0

The problem was with the installation of Jenkins. A devops engineer solved this for me. Had to install Tomcat and jenkins within that. He said "If Jenkins is installed as UI, it can't open another UI. Hence we should install .war file on a separate server which creates separate UI for jenkins, and my application UI (firefox) can be launched on the local".

Shreyas SG
  • 368
  • 3
  • 6
  • 21
0

It seems while removing the files/folder or copy the files to remote server it may give below error message:

Caused: java.io.IOException: Cannot run program "nohup" (in directory "C:\Windows\system32\config\systemprofile\AppData\Local\Jenkins\.jenkins\workspace\Test_ssh_Remove"): CreateProcess error=2, The system cannot find the file specified
java.io.IOException: CreateProcess error=2, The system cannot find the file specified

To resolve this issue you must give relative path in source and target instead of absolute path as below. e.g. Here "Program Files" folder inside the C: drive on windows machine (Remote server).

Sample :

dir('VueAPP') { // Go to Your Current Directory where build code present to copy at Remote server 

   sshRemove remote: remote, path: "/Program Files/Apache24/htdocs/dist"

   sshPut remote: remote, from: 'dist', into: '/Program Files/Apache24/htdocs/'

   sshPut remote: remote, from: 'index.html', into: '/Program Files/Apache24/htdocs/'

}
itronic1990
  • 1,231
  • 2
  • 4
  • 18
0

For me, there was an extra space in the path of java "C:\Program Files\Java\jdk1.8.0_301/bin/java" I updated and it worked. Manage Jenkins -> GLobal Tool Configuration -> JDK

badar
  • 81
  • 1
  • 3