Welcome! Please see the About page for a little more info on how this works.

0 votes
in Clojure by

I'm trying to follow the guide here: https://www.clojure.org/guides/deps_and_cli to run a simple clojure program.

My directory structure looks like this:

└── test-project
	├── deps.edn
	└── src
		└── bar.clj

and bar.clj looks like this:

(ns bar)
(defn main []
	(printl "hello world"))

However, when I run clj -X bar/main I get the following error:

Exception in thread "main" java.io.FileNotFoundException: -X (No such file or directory)
	at java.io.FileInputStream.open0(Native Method)
	at java.io.FileInputStream.open(FileInputStream.java:195)
	at java.io.FileInputStream.<init>(FileInputStream.java:138)
	at java.io.FileInputStream.<init>(FileInputStream.java:93)
	at clojure.lang.Compiler.loadFile(Compiler.java:7570)
	at clojure.main$load_script.invokeStatic(main.clj:452)
	at clojure.main$script_opt.invokeStatic(main.clj:512)
	at clojure.main$script_opt.invoke(main.clj:507)
	at clojure.main$main.invokeStatic(main.clj:598)
	at clojure.main$main.doInvoke(main.clj:561)
	at clojure.lang.RestFn.applyTo(RestFn.java:137)
	at clojure.lang.Var.applyTo(Var.java:705)
	at clojure.main.main(main.java:37)

What am I doing wrong?

2 Answers

+1 vote
by
selected by
 
Best answer

You need to update to a newer version of clj that supports -X.

by
And also change main to accept a single argument, which is a hash map of the "exec args" passed in from the -X invocation:

clojure -X bar/main :val 123

will invoke your main function with {:val 123} as the single argument.
by
-X is for invoking a function that a single map argument. In your example code you have a main method. If you want to use a main method, it should have a signature like

    (ns hello)
    (defn -main [& args] ...)

and you should invoke it like

    clj -M -m hello
by
It is main not -main so he _could_ invoke it with -X if it accepted a single argument.

Or he could invoke it with -M -m if he changed main to -main (and maybe added & args depending on whether he needs it to accept arguments).

I've seen quite a few people using -X with main functions (not -main functions) which could certainly be confusing.
0 votes
by

That is surprising. I get a different, more expected error (Wrong number of args (1) passed to: bar/main).

What's in your deps.edn? If you put {} in it, and change the signature of your main function to accept an argument, like

(defn main [opts]
  (println "Hello, world"))

(and also update printl in your current function to println), any luck?

...