Batch file wrappers on Windows

646 views
Skip to first unread message

Paul Moore

unread,
Jun 21, 2010, 1:42:47 PM6/21/10
to Clojure
The common way of running Java applications (and hence Clojure code)
on Windows seems to be to use a batch file wrapper (for example,
lein.bat for Leiningen). It seems to me that there are a number of
issues with this:

1. Batch files don't nest well - if I want to call lein.bat from
within another batch file, I have to remember to say "call lein arg
arg..." rather than simply "lein arg arg..." as the latter doesn't
return to the calling script.
2. I use JP Software's Take Command, which implements a command
language almost, but not precisely, like that of cmd.exe. As a result,
lein.bat (in particular, possibly others) fails with syntax errors. I
appreciate that this is a Take Command problem (and it's not too hard
to work around, in fact) but nevertheless it does routinely trip me
up. And presumably other Take Command users have the same problem.
3. Using a bat file to start a GUI application from Explorer causes an
unnecessary and ugly console window to appear.

Before I spend time trying to solve a problem that the Java community
turns out to have solved years ago, is there another solution that
people use? Or is it just one of those things that Windows people put
up with (I'm not a typical Windows user, I guess :-))?

Thanks,
Paul

Kevin Kelley

unread,
Jun 21, 2010, 4:27:11 PM6/21/10
to clo...@googlegroups.com
On Mon, 21 Jun 2010 12:42:47 -0500, Paul Moore <p.f....@gmail.com> wrote:

> The common way of running Java applications (and hence Clojure code)
> on Windows seems to be to use a batch file wrapper (for example,
> lein.bat for Leiningen). It seems to me that there are a number of
> issues with this:

> [...]


> Before I spend time trying to solve a problem that the Java community

> turns out to have solved years ago ...

For simple-and-quick, make an "executable jar" by adding a
META_INF/Manifest.mf
file which calls out the main entry point to the app; then it runs with a
double-click ('lein uberjar' might already do this?)

For slightly prettier and distributable, make a .jnlp file that lists
jar dependencies, icon, etc.; then download/running the .jnlp installs the
app
to your machine. You don't see this too much, but it works okay for the
basic "install a java app" use case... I think by the time it was finally
possible, a lot of people didn't really care any more. Still, it's better
than nothing.


--
Kevin Kelley

Russ Olsen

unread,
Jun 21, 2010, 5:12:32 PM6/21/10
to Clojure
Paul,

One way would be to use the cljw.exe that comes with
dejour. This is a windows executable that runs clojure
without creating that annoying extra window.

I haven't really looked into making dejour work with
Leiningen yet, so that may be a problem for you.

You can find the dejour project on github at:

http://github.com/russolsen/dejour

If you are just interesting in getting Clojure running, download the
latest zip or
tar file at:

http://github.com/russolsen/dejour/downloads

Good Luck!

Russ

Rick Moynihan

unread,
Jun 21, 2010, 7:11:47 PM6/21/10
to clo...@googlegroups.com
On 21 June 2010 18:42, Paul Moore <p.f....@gmail.com> wrote:
> 3. Using a bat file to start a GUI application from Explorer causes an
> unnecessary and ugly console window to appear.

You should use "javaw" instead of "java" to start the JVM without the
console window appearing. This should ship with both the JRE and JDK
on Windows.

R.

Paul Moore

unread,
Jun 21, 2010, 5:44:12 PM6/21/10
to clo...@googlegroups.com
On 21 June 2010 22:12, Russ Olsen <russ...@gmail.com> wrote:
> Paul,
>
> One way would be to use the cljw.exe that comes with
> dejour. This is a windows executable that runs clojure
> without creating that annoying extra window.

Thanks. That's certainly one option - I could probably without too
much difficulty put together something similar for Leiningen.

It sounds like there isn't a generic answer, so going with a custom
launcher sounds viable. Hmm, I wonder whether a stub executable plus a
suitable Ant task / Leiningen plugin (see how I use these terms like I
know what they mean? :-)) would be an option - add some static data
onto the stub, which the stub reads and sets the appropriate variables
and command line args, then launches Java.

Something to think about...
Paul.

Paul Moore

unread,
Jun 22, 2010, 5:08:41 AM6/22/10
to clo...@googlegroups.com

Sorry for not being clear - I know about javaw, but even with that,
double clicking on a .bat file flashes up a console window (briefly).
I can also set the console to start minimised and it's then a very
minor inconvenience. As I said, there are workarounds (and this is the
least problematic of the three issues I noted) but I still find it an
issue.

Clearly others don't get so up tight about these inconveniences as me,
which is fine. At least that means I haven't missed a solution that
already exists.

Thanks for the feedback,
Paul.

Lars Nilsson

unread,
Jun 22, 2010, 10:15:30 AM6/22/10
to clo...@googlegroups.com

Maybe Windows Script Host is an option, if you haven't looked at that one yet.

Lars Nilsson

Lars Nilsson

unread,
Jun 22, 2010, 10:37:19 AM6/22/10
to clo...@googlegroups.com

Dim WshShell, oExec
Set WshShell = CreateObject("WScript.Shell")
Set oEnv = WshShell.Environment("Process")
oEnv("CLOJURE_DIR") = "C:\\Devtools\\clojure"
oEnv("CLOJURE_JAR") = "C:\\Devtools\\clojure\\clojure.jar"
Set oExec = WshShell.Exec("javaw -cp %CLOJURE_JAR%;.\\classes;.\\src;.
clojure.main %1 -- %*")

Saving this as a .vbs file, and replacing %1 with the name of a gui
.clj file seems to work for me. I don't happen to have any Clojure gui
code laying around, so I just picked the temperature converter program
from the Clojure website (http://clojure.org/jvm_hosted). Closing the
window left the javaw process running, maybe because it doesn't listen
for the closing of the window.

Lars Nilsson

Paul Moore

unread,
Jun 22, 2010, 11:46:47 AM6/22/10
to clo...@googlegroups.com
On 22 June 2010 15:15, Lars Nilsson <chama...@gmail.com> wrote:

> Maybe Windows Script Host is an option, if you haven't looked at that one yet.

Yes, WSH/VBS is a reasonable option. There are some gotchas which you
need to be careful of though - there's *still* only one filetype
(.vbs) which is executed by cscript or wscript depending on
user-settable defaults (cscript //H:wscript) which makes it
unreliable.

For a really robust solution, I think a custom EXE might be the best
(for the user) answer.

But this is probably far too Windows-specific for the list now. I'll
go off and try to implement something and report back.

Thanks for all the feedback,
Paul.

Reply all
Reply to author
Forward
0 new messages