42

I want to run a Java jar file like this:

java -jar spider.jar

How to run it on the background on Windows?

Like this on Linux:

nohup java -jar spider.jar > /var/tmp/spider.log 2>&1 &
Peter O.
  • 32,158
  • 14
  • 82
  • 96
www
  • 4,065
  • 7
  • 30
  • 27
  • I'm still looking for an answer. Related question: http://stackoverflow.com/questions/1536205/running-another-program-in-windows-bat-file-and-not-create-child-process – Basilevs May 08 '14 at 12:41

5 Answers5

31

You could use the Windows start command:

start /min java -jar spider.jar

This command is not really the same as nohup; but it might be suitable if you're happy with the Java process running in a separate minimised window. See http://www.microsoft.com/resources/documentation/windows/xp/all/proddocs/en-us/ntcmds.mspx

gutch
  • 6,959
  • 3
  • 36
  • 53
30

On Windows it's not normal that a process terminates once its parent was killed (like Unix-likes do it normally). Therefore there is no direct necessity for something like nohup. If you want to avoid the console window associated with it, you can use javaw but redirection won't work, then.

Joey
  • 344,408
  • 85
  • 689
  • 683
  • 1
    See http://stackoverflow.com/questions/1997718/difference-between-java-exe-and-javaw-exe – skaffman Aug 01 '10 at 13:03
  • 6
    There is a case where you need a windows equivalent of nohup: things launched with the windows "start" command are killed when you log out. – Anthony May 15 '15 at 10:37
  • 1
    @Anthony: All processes belonging to your user are killed when you log out. Note also that `start` is a command of `cmd.exe`, not of Windows. – Joey May 15 '15 at 11:59
  • @Joey **but redirection won't work** what do you mean by this? – Vikash Sep 25 '18 at 09:06
  • 1
    @Vikash: `javaw` is a GUI program, it doesn't automatically have an attached console. Hence you cannot redirect the standard streams like stdout. – Joey Sep 25 '18 at 09:07
  • On some Windows 10 version: c:\>which start C:/PROGRA~2/INTEGR~1/Toolkit/mksnt/start.exe – Alexander Stohr May 07 '21 at 09:25
7

The only way to get the nohup behavior, where the process still runs after logging off (like for micro-services, analytic tools, server batch jobs etc.), is to run the .bat file with the start javaw -jar ... contents as either a service or a scheduled task.

Lodewyk
  • 396
  • 4
  • 6
1

save the following code to nohup.vbs, and add the directory to PATH.

Set args=Wscript.Arguments
Set ws=CreateObject("wscript.shell")
ws.Run args(0),0,true

then, run:

nohup "java -jar spider.jar > /var/tmp/spider.log"

Note that you should quote the full commands and the redirects.

lyh543
  • 847
  • 6
  • 8
-2

For windows run following mentioned command in Command Prompt or in Terminal

nohup (file need to run) > (File you need to save the log) 2>&1

Ex: nohup ./bin/windows/kafka-server-start.bat config/server.properties > ./MyKafka.log 2>&1