0

I have a jar file which when run from command prompt works well, I mean it asks for user input,but when the same is run from psexec or powershell or jenkins doesn't accept values and starts processing further commands. In short: When jar is executed, there is a menu to make choices. If choice is within specific range, it works well else gives message entered value is not valid. Here I am getting the message in a loop. Can anyone suggest where I am going wrong or need to make any change in the settings.

P.S.~ I am trying to execute commands on remote machine and I am calling the jar using java -jar myFile.jar

Hunterr
  • 553
  • 1
  • 8
  • 28

1 Answers1

1

can you give a basic idea how I can achieve it in Jenkins.

So

  • if your jar accepts arguments you pass them in the jenkins job via Parameterized Builds
  • if it doesn't but instead prompts you, you are in problem: you need a way to automate console then.

Automate console

X-platform tool for the job is Expect. Bad thing about it is that it requires a lot of deps on Windows (if you are running this on Linux expect is probably already there). As alternative you could use Autohotkey altho it can't detect easily console text (unlike expect which reads console buffer). However if prompt appears in deterministic manner (i.e. just after start) you could do something like:

java -jar myfile.jar
sleep 5
autohotkey.exe SendKeys.ahk

SendKeys.ahk would be as simple as:

 WinActivate <pid / window name ...>
 Send answer1<ENTER>answer2<ENTER> ....

but it could be made more robust so that key sending will not depend on console window state.

In cmd you can set some specific window name with title command then you can activate that title with AHK.

majkinetor
  • 8,730
  • 9
  • 54
  • 72
  • I did as you said, I created a SendKeys.ahk and when called it from the batch file it didn't get invoked. When the jar is started in command prompt from the batch file, it prompts for user input. When SendKeys.ahk is directly started from cmd, it executes the statements. – Hunterr Apr 27 '16 at 12:26
  • 1
    add path to it, ahk will look it in the current directory. You need to set <...> part too. You can make it to run via hotkey for testing - just surround the code with `F1::` and `return` and start the script before the cmd. Then when prompt appears press F1 to see if it works. – majkinetor Apr 27 '16 at 12:55
  • I have set the location in the Path variable, and its works using hot keys. But I want to achieve this without pressing any key, I mean just when the prompt appears, I need to enter the values. Any suggestion for it? – Hunterr Apr 28 '16 at 04:35
  • 1
    It must work without a hotkey if you start it correctly with your original code (just as with the hotkey, there is really no difference). You can also try to do everything from within ahk (use `Run ` to start java for example and sleep from within ahk). – majkinetor Apr 28 '16 at 08:01
  • Cheers ! Thanks a lot. Works exactly as I wanted. Now need to invoke it in remote machine, else I am done. P.S.~ I had to use: `#SingleInstance, Force ;` as it was not updating even after saving the `.ahk` file. – Hunterr Apr 28 '16 at 09:37
  • But, just the overhead is to install Autohotkey in all remote machines if I need to execute in more than one machine. – Hunterr Apr 28 '16 at 09:40
  • Installing AHK is easy - you could execute `cinst autothotkey` to do it via chocolatey (in combination with GPO it will be on all servers), but easier, just keep `autohotkey.exe` on the share that is accessible from all servers (ahk is ~300KB, you can even embed it in the script). Yeah, I forgot about `#SingleInstance, Force;` which is something you should include in all ahk scripts while developing them. – majkinetor Apr 28 '16 at 10:05
  • Yes, first I had downloaded the installer so I thought it needs to be installed on each machine. But again google returned a portable exe, so not an issue now. Thanks again :) – Hunterr Apr 28 '16 at 11:28