Установка clojure на windows

Clojure on Windows
#

This is a guide to installing Clojure on Windows from scratch.

Install Scoop
#

Step one: Open Powershell & install Scoop using commands below.

# First try to run this command
irm get.scoop.sh | iex

If it fails, then try other commands below where we will have
to change the security settings.

Run command get-executionpolicy -List and take note of
what your setting are before we change them.

Step two: Run the following commands. You may need to update them
to restore it to your other settings, but that should be
somewhat obvious.

When it prompts you respond with Y and hit enter:

Set-ExecutionPolicy RemoteSigned -Scope CurrentUser
irm get.scoop.sh | iex
Set-ExecutionPolicy -ExecutionPolicy Undefined -Scope CurrentUser

Step three: Use Get-ExecutionPolicy -List again to check if your
before and after is the same. Assuming you care of course.

Install Java
#

Open a new cmd window and run below commands:

call scoop install 7zip
call scoop install git
call scoop bucket add java
call scoop install openjdk
call scoop update
call scoop update --all

Note for my users because I control the environment, steps 1 and
2 is what I use to install my programs as well since it is enough
for the clojure jar files to run. We do not need to go any
further if users are not doing development. The reason I do this
is that I like having scoop around to update stuff when it
becomes a problem

Install Clojure
#

Taken from here: https://github.com/littleli/scoop-clojure…
we’ve already installed Java so there are a few steps we can
skip, but listing it here for reference to source material
Note do not use temurin-lts-jdk, I’ve had problems with
it and clojure for various dev tools. Use openjdk as we
already installed above.

Run the following commands:

scoop bucket add java
scoop bucket add extras
scoop bucket add scoop-clojure https://github.com/littleli/scoop-clojure
scoop install clj-deps
scoop update clj-deps

If you want leiningen, then also run

scoop install leiningen

A reasonable expectation with a modern platform is the
ability to generate a new template project of various types.
Clojure used to have a nice system for this called leiningen.
Then they upgraded to deps.edn because it is better in many
ways, but it is a step backward…
In leiningen, you could now call lein new projectname
and a new project would be generated in your active
directory. To do the same in deps.edn, we need to install
another tool.

The install instructions should be enough to install this tool…
https://github.com/seancorfield/deps-new

For convenience, the command to run is:

clojure -Ttools install-latest :lib io.github.seancorfield/deps-new :as new

Note that if you use any other features of the install then you will need to escape the
strings differently as Windows decided to do something much worse with their cmd line
and powershell tools. For reference here is an install for the precursor tool where the
version is specified in triple quotes due to how the cmd line escapes the strings.

clojure -Ttools install com.github.seancorfield/clj-new '{:git/tag """v1.2.404"""}' :as clj-new

Create a new project in your workspace folder using the command below:

clojure -Tnew app :name myusername/mynewapp

Setup Dev Environment
#

VS Code
#

Install VS code and use Calva extension. You can figure
out how to do this on your own.

To start a project and test the integration, the best
thing to do is to create a new project using the deps-new tool from
above and then open it with vs code. The Calva extension
once installed should configure the files and setup the
syntax highlighting etc.

The VS Code command to test the integrated REPL is:

Calva: Start a Project REPL and Connect (aka Jack-in).
    Project Root: Workspace Root
    Project Type: deps.edn
    Alias: :run-x

Note: use cntrl-shift-p to bring up the command search in VS Code
You can also click the REPL text at the bottom left hand side
of the screen to run the command above however you will still have to pick the options

You may see a couple of options come up, don’t worry if
you don’t see a particular option, it may already be
configured.

EMACS
#

For emacs, you can refer to this page in brave clojure book.

_Note that I actually prefer the prelude configuration for
emacs instead
of the one in brave clojure, but the brave clojure one
will get you started and you can configure it from there.

The prelude extension is maintained by the guy who
developed/maintains many of the clojure tools for emacs
such as CIDER which is the backbone of many other editor
clojure integrations as including Calva (the extension we use).

Let’s get Clojure installed on your system. This guide will help you through the
following steps to get Clojure installed.

The instructions depend on what operating system you are running. Please click your operating system below.

Click your operating system

Start a terminal

For these instructions, you will need to have a terminal, or command line, open.
This is a text-based interface to issue commands to your computer, and you can
open it by running Terminal.app, which is found under /Applications/Utilities.
If you have never used the terminal before, you may want to spend some time
reading up on command-line
basics.

The prompt (where you will type your commands) usually shows the computer name
and user name, as well as the folder or directory you are currently in.

I will tell you to run commands in your terminal. When I say that, I mean «type
the command into the terminal and press the Return key.»

1. Verify if Java is installed

In your terminal, run the following command:

java -version

It may say something like «command not found«. In that case, Java is not
installed on your system, and we will need to install it. If it prompts you to
install Java, ignore it and cancel anything it opens.

It may output a few lines saying what Java version is installed, like this:

$ java -version
java version "1.10.1"
Java(TM) SE Runtime Environment 18.3 (build 10.0.1+10)
Java HotSpot(TM) 64-Bit Server VM 18.3 (build 10.0.1+10, mixed mode)
$

This output indicates java version "1.10.1". In the Java world, we ignore the
«1.» at the beginning. So that’s called Java 10. If you have Java less than 11,
you need to upgrade. If you have Java 11 or higher, you are good for the courses
here, but you may want to upgrade to the latest LTS version (version 21).

2. Install or upgrade Java

We need to download the latest JDK.

Visit adoptium.net.

Click the big «Latest LTS Release» button. Make a note of the version number.

This will download a pkg file. Open that file to begin installing it.

Proceed through the steps of the installer. You may «trash» the pkg when you’re
done.

3. Verify Java again

Open a new Terminal window.

Run the following command:

java -version

It should indicate you have the latest release of OpenJDK.

You’re done with Java!

Leiningen is a project tool for Clojure. Many Clojure projects are still built
using Leiningen. Some projects here use Leiningen.

Leiningen is a command-line tool you run using the lein command. We will need
to install the lein command to your path, which means it will be available to
you to run whatever directory you are in.

To install lein, execute the following commands in your terminal. You will be
prompted to enter your computer’s login password. If you are having trouble
calling sudo with the message «User is not in sudoers file, incident
reported», follow these
instructions to give
your account sudo permissions.

1. Download the lein script

curl https://raw.githubusercontent.com/technomancy/leiningen/stable/bin/lein > lein

2. Move the lein script to the user programs directory

sudo mv lein /usr/local/bin/lein

3. Add execute permissions to the lein script

sudo chmod a+x /usr/local/bin/lein

4. Verify your installation

You should have Leiningen installed now. Run the following command to test it
out.

lein version

It should take a while to run, as it will download some resources it needs the
first time. If it completes successfully, you are golden!

The Clojure Command-Line Interface (CLI) is a relatively new (2018) addition to
the Clojure tooling world. It is the official way to run Clojure applications.
Many people are using it now instead of Leiningen. Some of the projects here use
the Clojure CLI.

1. Verify/install Homebrew

Homebrew is a package manager for macOS. It is a common tool
for developers to use on Macs.

The Clojure CLI is installed using Homebrew. Type the following command to
update the Homebrew package repository and verify it is installed.

brew update

If it looks like it ran, you’re good to move on to the next step. If the system
complains that the brew command is not found, then you will need to install
Homebrew. To do that, execute the following command:

/usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"

This command will run a script that explains what it will do and wait for you to
confirm it.

2. Installing Clojure CLI

Now that you have Homebrew ready, execute the following:

brew install clojure/tools/clojure

This will install the Clojure CLI.

To upgrade from a previous install:

brew upgrade clojure/tools/clojure

3. Verifying the installation

Now that it’s all installed, run the following to test it out:

clj

It should run a new prompt (called a REPL), like this:

$ clj
Clojure 1.11.1
user=>

If you’ve got that, then you’re ready to roll! Hit Ctrl-c to exit the Clojure
REPL.

Start a terminal

You will need to have a terminal, or command line, open. This is a text-based
interface to talk to your computer. In Ubuntu, you can open it by clicking «Dash
Home» and typing Terminal. You can also open a terminal anytime by
pressing CTRL-ALT-T. If you have never used the terminal before, you may want
to spend some time reading up on command-line
basics.

The prompt (where you will type your commands) may look different: it usually
shows the computer name and user name, as well as the folder or directory you
are currently in.

I will tell you to run commands in your terminal. When I say that, I mean «type
the command into the terminal and press the Return key.»

If you’re using Ubuntu or another version of Linux, open your terminal. The
commands we type will be the same.

1. Verify if Java is installed

In your terminal, run the following command:

java -version

It may say something like «command not found«, or it may suggest a package to
install. Ignore it. In either case, Java is not installed on your system, and we
will need to install it.

It may output a few lines saying what Java version is installed, like this:

$ java -version
java version "1.10.1"
Java(TM) SE Runtime Environment 18.3 (build 10.0.1+10)
Java HotSpot(TM) 64-Bit Server VM 18.3 (build 10.0.1+10, mixed mode)
$

This output indicates java version "1.10.1". In the Java world, we ignore the
«1.» at the beginning. So that’s called Java 10. If you have Java less than
11, you need to upgrade. If you have Java
11
or higher, you are good for the courses here, but you may want to
upgrade to the latest LTS version (version 21).

2. Install or upgrade Java

We have some commands to run in the terminal. These instructions are for Ubuntu
and Debian. If you run another distribution of Linux, follow the instructions
here.

1. Allow extra repositories and install packages you need

sudo apt-get install -y software-properties-common wget apt-transport-https

2. Get the Eclipse Adoptium GPG Key (for encryption)

sudo mkdir -p /etc/apt/keyrings
wget -O - https://packages.adoptium.net/artifactory/api/gpg/key/public | sudo tee /etc/apt/keyrings/adoptium.asc

3. Add the AdoptOpenJDK repository

echo "deb [signed-by=/etc/apt/keyrings/adoptium.asc] https://packages.adoptium.net/artifactory/deb $(awk -F= '/^VERSION_CODENAME/{print$2}' /etc/os-release) main" | sudo tee /etc/apt/sources.list.d/adoptium.list

4. Update the repository index

sudo apt-get update

5. Install Adoptium Temurin

sudo apt-get install -y temurin-17-jdk

3. Verify Java again

Run the following command in the terminal.

java -version

It should indicate you have the latest version of OpenJDK.

You’re done with Java!

Leiningen is a project tool for Clojure. Many Clojure projects are built using
Leiningen. Some Clojure projects here use Leiningen.

Leiningen is a command-line tool that you run with the lein command. We will
need to install the lein command to your path, which means it will be
available to you to run whatever directory you are in.

To install lein, execute the following commands in your terminal. You will be
prompted to enter your computer account’s password.

1. Install curl command

sudo apt-get install -y curl

2. Download the lein script

curl https://raw.githubusercontent.com/technomancy/leiningen/stable/bin/lein > lein

3. Move the lein script to the user programs directory

sudo mv lein /usr/local/bin/lein

4. Add execute permissions to the lein script

sudo chmod a+x /usr/local/bin/lein

5. Verify your installation

You should be installed now. Run the following command to test it out.

lein version

It should take a while to run, as it will download some resources it needs the
first time. If it completes successfully, you are golden!

The Clojure Command-Line Interface (CLI) is a relatively new (2018) addition to
the Clojure tooling world. It is the official way to run Clojure applications.
Many people are using it now instead of Leiningen. Some of the projects here
still use Leiningen, but some use the CLI.

1. Install dependencies

sudo apt-get install -y bash curl rlwrap

2. Download the install script

curl -L -O https://github.com/clojure/brew-install/releases/latest/download/linux-install.sh

3. Add execute permissions to install script

chmod +x linux-install.sh

4. Execute the installer

sudo ./linux-install.sh

5. Verifying the installation

Now that it’s all installed run the following to test it out:

clj

It should run a new prompt (called a REPL), like this:

$ clj
Clojure 1.11.1
user=>

If you’ve got that, then you’re ready to roll! Hit Ctrl-c to exit the Clojure
REPL.

Start a terminal

You need a terminal, or command line, open for these instructions. A terminal is
a text-based interface to talk to your computer. You can open it by searching
for «command prompt» in the search bar and then clicking on the Command Prompt
app.

If you have never used the terminal before, you may want to spend some time
reading up on command-line basics.

The prompt (where you will type your commands) may look different. It usually
shows the folder you are currently in.

I will tell you to run commands in your terminal. When I say that, I mean «type
the command into the terminal and press the Enter key.»

I recommend installing Java Development Kit (JDK) version 21 or newer for new installs.

1. Verify if Java is installed

In your terminal, run the following command:

java -version

It may say something like «'java' is not a recognized command'«. If it does,
that means Java is not installed and we will need to install it.

It may output a few lines saying what Java version is installed, like this:

> java -version
java version "1.10.1"
Java(TM) SE Runtime Environment 18.3 (build 10.0.1+10)
Java HotSpot(TM) 64-Bit Server VM 18.3 (build 10.0.1+10, mixed mode)
>

This output indicates java version "1.10.1". In the Java world, we ignore the
«1.» at the beginning. So that’s called Java 10. If you have Java less than 11,
you need to upgrade. If you have Java 11 or higher, you are good for the courses
here, but you may want to upgrade to the latest LTS version (version 21).

2. Install or upgrade Java

We need to download the latest version of the JDK.

Visit adoptium.net.

Click the «Latest LTS Release» button. Make a note of the version.

This will download a .msi file (installer). Open that file to begin installing
it. A window should open. Proceed through the installer with the defaults.

3. Verify Java again

Open a new command prompt, then run the following command:

java -version

It should indicate you have OpenJDK version you noted above.

You’re done with Java!

Leiningen is a project tool for Clojure. Many Clojure projects are built
using Leiningen. Some projects here use Leiningen and some use the CLI.

Leiningen is a command-line tool that you run with the lein command. We need
to install the lein command to your path so that you can run it in whatever
directory you are in.

To install lein, execute the following commands in your command prompt. You
will be prompted to enter your password.

1. Download the lein.bat script

curl -O https://raw.githubusercontent.com/technomancy/leiningen/stable/bin/lein.bat

2. Create a bin directory for scripts

md bin

3. Move the lein.bat script to that directory

move lein.bat bin

4. Add bin to your path

setx path "%path%;%USERPROFILE%\bin"

5. Complete your installation

Close the command prompt and open a new one. Then run the following command to
finish the installation.

lein self-install

It should take a while to run, as it will download some resources it needs the
first time. If it completes successfully, you are golden!

The Clojure Command-Line Interface (CLI) is a relatively new (2018) addition to
the Clojure tooling world. It is the official way to run Clojure applications.
Many people are using it now instead of Leiningen. However, Leiningen is still
very popular, and many of the projects here still use Leiningen. It is a good
idea to install it.

1. Verify/install Scroop

Scoop is an installer for the Windows command line. Many
programmers use it to install development tools.

We can install the Clojure CLI using Scoop. Open a PowerShell terminal and type
the following command to verify it is installed.

scoop search clojure

If the command is not found, you need to install Scoop. Continue with this
installation step. If it does work, skip to the next step.

To install Scoop, run the following:

Set-ExecutionPolicy RemoteSigned -Scope CurrentUser # Optional: Needed to run a remote script the first time
irm get.scoop.sh | iex

This command will run a script and will guide you through the process if it
needs any input.

2. Installing Clojure CLI and its dependencies

Now that you have Scoop ready execute the following at your command prompt:

Install git

If you don’t have git installed, you will need it to add the Clojure package.

scoop install git

You need some things from the «extras» package.

scoop bucket add extras

We need to add the Clojure package from a GitHub repo.

scoop bucket add scoop-clojure https://github.com/littleli/scoop-clojure

Finally install the Clojure CLI.

scoop install clj-deps

3. Verifying the installation

Now that it’s all installed, run the following to test it out:

clj

It should run a new prompt (called a REPL), like this:

> clj
Clojure 1.11.1
user=>

If you’ve got that, then you’re ready to roll! Hit Ctrl-c to exit the Clojure
REPL.

Learn how to install Clojure locally to solve Exercism’s exercises on your own machine

Installing the Clojure CLI

Clojure provides command line tools that can be used to start a Clojure repl, use Clojure and Java libraries, and start Clojure programs.

If you need to install Java, we recommend Adoptium Temurin 17.

Linux

Ensure that the following dependencies are installed: bash, curl, rlwrap, and Java.
You can install Clojure either of these 2 ways:

  1. LinuxBrew — requires Homebrew
brew install clojure/tools/clojure
  1. Script installer
curl -O https://download.clojure.org/install/linux-install-1.11.1.1347.sh
chmod +x linux-install-1.11.1.1347.sh
sudo ./linux-install-1.11.1.1347.sh

Homebrew for Mac OS X

Install the command line tools with brew from the clojure/tools tap:

brew install clojure/tools/clojure

Java

Any distribution of Java will work. If needed, you can install Temurin using brew:

brew tap homebrew/cask-versions
brew install --cask temurin17

Windows

Clojure on Windows has improved significantly recently. We used to recommended using WSL2, which works great,
but there is now a PowerShell installer. Clojure on Windows is currently in an alpha state, see clj-on-windows for known issues.

Make sure Java is installed and the JAVA_HOME environment variable is set, and run:

iwr -useb download.clojure.org/install/win-install-1.11.1.1165.ps1 | iex

Another way to install Clojure on Windows is with the Scoop package manager. To install scoop, open PowerShell and run these 2 commands, one after the other:

iwr -useb get.scoop.sh | iex
Set-ExecutionPolicy RemoteSigned -Scope CurrentUser -Force

To install Clojure with Scoop you have to add two important buckets first:

# if you don't have git version control system installed
# it is required for adding new external buckets to your scoop installer
# you can skip this step otherwise
scoop install git

# add scoop bucket for Java 
scoop bucket add java

# add scoop bucket for clojure build
scoop bucket add scoop-clojure https://github.com/littleli/scoop-clojure

Now we are ready to install Java and Clojure by issuing following commands:

# install Java runtime and compiler
scoop install adoptopenjdk-lts-hotspot

# install official clojure tools
scoop install clojure

# update to the newest version
scoop update clojure

After successfully running steps above, you should be able to run Clojure with the following:

clj

Install Leiningen (optional)

While no longer necessary, we still support the full featured project management tool Leiningen.
It offers a wide variety of helpful tasks like convenient testing, builds, and an improved REPL.

  1. Download the lein script or lein.bat (Windows)
  2. Place it on your $PATH where your shell can find it (eg. /usr/local/bin/)
  3. Set it to be executable (sudo chmod a+x /usr/local/bin/lein)
  4. Run lein.

Editor integration

The best way to develop Clojure is with an editor-connected REPL. Plugins are available for the most popular editors/IDEs:

  • Emacs: CIDER
  • IntelliJ: Cursive
  • VSCode: Calva
  • Vim: Conjure

See the official docs for more info.

When you find yourself looking to flex your language learning skills, functional languages often end up on your short list. After reading this past year’s «State of the Clojure Community» survey results, I couldn’t help but notice how few developers who run Windows are choosing to learn Clojure. For sure, it could be the shining allure of F#. But maybe it’s also that many tutorials focus on Linux or MacOS and often skip over the installation of the Java development kit. In any case, this article is going to cover getting up-and-running with Clojure, specifically when using Windows and we won’t be using the Windows subsystem for Linux (WSL).

What is your primary development OS? Answer: Not Windows

In my opinion, Clojure has a lot to offer anyone working on software development regardless of the operating system we have on our primary machine. 😉

Aside from being a functional language, Clojure has some other interesting features: there is a very real stress on immutability and that makes some other things, like concurrent or parallel code, easier to reason about. The language continues to evolve and improve and the community is reasonably friendly and accessible.

Tools

This article assumes that you have little or no experience with Clojure and that you haven’t really done much in the way of setup ahead of time. To that end we’ve chosen some tools for you.

  • Visual Studio Code for editing source code
  • Calva will be the VS Code plugin we use to make working with Clojure straightforward
  • Clojure runs on the Java virtual machine, we’ll get our JVM from AdoptOpenJDK
  • There are a couple different tools for managing Clojure projects, we’ll use Leiningen

All of these are quick and painless to install, even under Windows.

Install Your Tools

We’re going to start at the bottom of our list and work our way up, the first thing we will install is…

Java

Open up your preferred web browser and navigate to the AdoptOpenJDK website. Whatever they reccomend is fine, leave the radio buttons as-is and click on the «Latest Release» button to download the Java Development Kit installer. Double-click the installer to run, accept the license agreement, and install the software.

Leiningen

Clojure projects are typically managed by a tool, we’ll be using Leiningen. It will be responsible for downloading and managing libraries that we use in our project and building our code during development and deployment.

Where Do You Keep Your Scripts?

Leiningen is distributed as a batch file that you place in your path. This is pretty common and if you have a location for storing files like this, you can skip this part. If you don’t have a location, read on!

I recommend that you store your loose executables and scripts in a directory named «bin» in your home directory. We will now create that folder and then add it to your path, making these scripts available to you from wherever you happen to be on your machine.

Open up a File Explorer and then select the directory with your name from the right-hand side to view the contents of your «home» directory («C:\Users\YOUR_NAME»). Create a new directory for your loose scripts and call it «bin» (C:\Users\YOUR_NAME\bin»), you can double-click it to view it’s contents and then click on the path at the top of the window and copy it to the clipboard.

Next, right click on the icon for «This PC» and choose the «Properties» option; the details for your computer will appear. Click on the «Advanced System Settings» item in the right-hand menu, then click on the «Environment Variables» button in the lower right-hand corner. The «Environment Variables» window will appear with the entries specific to your account at the top. Highlight the entry with the value «Path» in the «Variable» column and then press the «Edit…» button. You will be present with a list of all the directories added to your path, click the «New» button to add a new entry and then paste in (or type) the path to your new «bin» directory («C:\Users\YOUR_NAME\bin»). Press the «OK» button to make the change permanent and then again to dismiss the «Environment Variables» window and then again to close the «System Properties» window.

Install Leiningen

Installing Leiningen is straightforward: we download the script and then place it in the same directory where we keep all of our loose tools and scripts (i.e. «C:\Users\YOUR_NAME\bin» or any location on your executable path). Open your web browser and navigate to the Leiningen website and scroll down to the «Install» section, you will see the instructions and links to the two scripts (one for Linux and one for Windows). Right click on the script for Windows («lein.bat») and choose the «Save Link As…» item to download a copy to your machine. The last step is to place the file in your «bin» directory («C:\Users\YOUR_NAME\bin»).

Now the Leiningen is available, we can tell it to download its own dependencies so that it’s ready to run. Open up a Powershell window and then run the following:

PS > lein self-install

Enter fullscreen mode

Exit fullscreen mode

The self-install should go pretty quickly, typically the only output you’ll see is…

Downloading Leiningen now...

Enter fullscreen mode

Exit fullscreen mode

Just to test it out, you can ask Leiningen what version it is.

PS> lein -version
Leiningen 2.9.6 on Java 11.0.11 OpenJDK 64-Bit Client VM

Enter fullscreen mode

Exit fullscreen mode

Visual Studio Code

You probably have Visual Studio Code installed and running already, but for the sake of completion we’ll provide a brief overview of that here. If you’re all set with Code, you can definitely skip this step.

When you visit the Visual Studio Code download page, you can click on the big blue «Windows» download box. That will get you the «User Installer» for your machine. This version will install everything in your current account profile and, in general, is much easier to keep up-to-date.

Once downloaded, you can double-click to start the installation. After you accept the license and choose the install location, you will be presented with a couple other options. All things being equal, I recommend that you check the boxes to add an «Open with Code» action to Windows Explorer files and directories.

Install the Calva Clojure Plugin

Calva is a plugin for Visual Studio Code that works with Leiningen (and other tools) to provide an integrated development environment where you can work on your Clojure code and get real work done. Since we already have our Java development environment and Leiningen installed, this will be very easy!

Open up Visual Studio Code and then click on the «View» menu and choose the «Extensions» item. The «Extensions» panel will load in the left hand side of the editor window and a list of popular extensions will be displayed. Click in the search box at the top of the panel and type in «Clojure». You will see that there are a variety of plugins for Code that support Clojure, «Calva» should be listed towards the top. Go ahead and click on «Calva» to select it, information about the plugin should appear in the main body of the editor window. Click on the «Install» button at the bottom of the Calva header panel to install it.

Create Your Project

Now that we have all of the tools installed, we are ready to create our project! Close the editor panel for Calva and then click on the «View» menu and choose the «Explorer» option. Next, click on the «File» menu and choose «Open Folder…» A new open dialog will appear, navigate to where you would like to store your code and then click the «New Folder» button towards the top of the window to create a new folder, call it something like «tutorial». Next, click on the new folder to select it and click on the «Select Folder» button at the bottom of the window.

Create the Project File

Every project needs a project file where we can store information about our project, like it’s name, version and any libraries that it depends on. Click on the «File» menu and choose «New File» to create a new file. Before we start adding content to the file, choose «Save As…» from under the «File» menu and name your file «project.clj», you want to save it right into the root of your project directory.

We’ll fill this file in with our project definition. You can model on the very simple one listed below (or copy it directly).

(defproject tutorial "0.1"
  :dependencies [[org.clojure/clojure "1.10.3"]]
  :main tutorial)

Enter fullscreen mode

Exit fullscreen mode

Here we set the name of our project, it’s version and a description. Next we add the version of Clojure we’re working with as a dependency (1.10.3 is the current version at the time of this writing). On the last line we point out which file should be the entry point to the project. You can name it anything, in this case we called it «tutorial» meaning that Leiningen should look for a file called «tutorial.clj» in our source code directory.

Add a Source File

Now we can add a file of source code. Leiningen will look for a source code in the «src» directory; in the «Explorer» panel on the left-hand side of your Code window, you will see some icons appear at the top when the mouse enters the panel or it gains focus. Click on the icon that looks like a folder to create a new directory and call it «src». Click on that new directory and then click on the icon that looks like a file to create a new file inside that «src» directory and call it «tutorial.clj». Add the text below to your «tutorial.clj» file and then save it.

(ns tutorial)

(defn -main
  [& args]
  (println "Hello!"))

Enter fullscreen mode

Exit fullscreen mode

The «-main» function represents the entry point to our application, if we were to compile our project and run it on it’s own, the «-main» function is called with any parameters we pass in on the command line.

Start An Interactive Session (REPL)

Select the «Command Palette» item from under the «View» menu, the command window will appear at the center top of your Code window, it will have a text area at the top where you can type in text and a list of matching commands in the body of the window. Type in «jack» and you will see the that the list narrows by a lot, you will see the «Calva: Start a Project REPL and Connect (aka Jack-in)» item in that list, to the right of it will be the command shortcut to invoke the task (Control-Alt-C and then Control-Alt-J). Note the shortcut as it’s easier to type then navigating the command palette and then select the «Calva: Start a Project REPL…» item.

Calva will now ask you what kind of REPL session you’d like to start. Go ahead and pick «Leiningen» (the first item) to start a new REPL with the Leingingen tool. The panel at the bottom of your window will show you details as Calva sets up and connects to your REPL and then a new editor pane will appear with the contents of your session (typically called «output.calva-repl»).

Go ahead and click into the REPL pane to make it active. You’ll see some introductory text from Calva and then the prompt (clj:tutorial:>). Type the following after the prompt and press return.

(-main)

Enter fullscreen mode

Exit fullscreen mode

This will call the main function and you should see output similar to what is pictured below.

; Jack-in done.
cljtutorial> 
(-main)
Hello!
nil
cljtutorial>

Enter fullscreen mode

Exit fullscreen mode

Congratulations! You have installed all of the tools and started a new Clojure project. 🏆

Working with Calva

There’s a good amount of documentation on the Calva website, you should definitely check it out. There is a «try first» tutorial that goes over compiling your whole file or just the form you are working on. From there you can explore the rest of the documentation to get productive in no time.

Build a Self Contained File

Now that we have some code that knows how to say «hi», the last thing we’ll do is build one Java archive (JAR) file that contains our application. We can then distribute that archive to anyone else who would like to run our code.

First we need to compile a class with our entry point so that a regular Java runtime knows how to start it up. Open up the «src\tutorial.clj» file and add (:gen-class) to the namespace declaration at the top, like so:

(ns tutorial
  (:gen-class))

(defn -main
  [& args]
  (println "Hello!"))

Enter fullscreen mode

Exit fullscreen mode

Next we’ll ask Leiningen to compile one archive for our project. If we had included any libraries in our project, Leiningen would add those to the archive as well. Click on the «View» menu and choose the «Terminal» item, a new terminal pane will open at the bottom of your code window. Type in the following and press return to build a self contained JAR file for your project.

C:\> lein uberjar

Enter fullscreen mode

Exit fullscreen mode

Leiningen will display some output and build your archive. You may run it like so…

C:\> java -jar target\tutorial-0.1-standalone.jar

Enter fullscreen mode

Exit fullscreen mode

You should see the output with the text «Hello!» At this point you should know enough to be dangerous. Good luck! 😏

A.1.2 Installing on Windows

As a Java developer venturing into the world of Clojure, setting up your development environment on Windows is a crucial first step. This guide will walk you through the installation process using two methods: the recommended Scoop package manager and a manual installation approach. By the end of this guide, you’ll have a fully functional Clojure environment ready for developing scalable data solutions with NoSQL databases.

Why Clojure on Windows?

Windows remains a popular operating system for many developers, and setting up Clojure on Windows allows you to leverage your existing tools and workflows. Clojure’s seamless integration with Java makes it an attractive choice for Java developers looking to explore functional programming and NoSQL data solutions.

Prerequisites

Before diving into the installation process, ensure that you have the following:

  • Windows 10 or later: This guide assumes you are using a modern version of Windows.
  • Administrator Access: Some installation steps require administrative privileges.
  • Internet Connection: Required for downloading installation packages and dependencies.

Installing Clojure Using Scoop (Recommended)

Scoop is a command-line installer for Windows that simplifies the installation of development tools. It handles dependencies and ensures that your software is up to date.

Step 1: Install Scoop

  1. Open PowerShell as Administrator: Right-click on the Start menu, select “Windows PowerShell (Admin)” to open PowerShell with administrative privileges.

  2. Set Execution Policy: Run the following command to allow script execution, which is necessary for Scoop to function:

    Set-ExecutionPolicy RemoteSigned -scope CurrentUser
    

    This command changes the execution policy for the current user, allowing you to run scripts that are signed by a trusted publisher.

  3. Install Scoop: Execute the following command to install Scoop:

    iwr -useb get.scoop.sh | iex
    

    This command downloads and executes the Scoop installation script. Once completed, Scoop will be installed and ready to use.

Step 2: Install Clojure

With Scoop installed, you can now install Clojure:

  1. Install Clojure: Run the following command in PowerShell:

    Scoop will download and install the latest version of Clojure, along with any necessary dependencies.

Step 3: Install Leiningen

Leiningen is a build automation tool for Clojure, similar to Maven for Java. It simplifies project management and dependency handling.

  1. Install Leiningen: Execute the following command:

    This command installs Leiningen, enabling you to create and manage Clojure projects efficiently.

Step 4: Verify Installations

To ensure that Clojure and Leiningen are installed correctly, verify their versions:

  1. Check Clojure Version: Run the following command:

    You should see the installed version of Clojure displayed in the terminal.

  2. Check Leiningen Version: Execute:

    This command displays the installed version of Leiningen, confirming that the installation was successful.

Manual Installation of Clojure

If you prefer not to use Scoop, you can manually install Clojure and Leiningen. This method involves downloading and setting up the necessary files yourself.

Step 1: Download the Windows Installer

  1. Visit the Clojure Website: Go to the Clojure Tools for Windows page.

  2. Download the Installer: Click on the link to download the Clojure installer for Windows. This installer includes the Clojure CLI tools.

  3. Run the Installer: Double-click the downloaded file to start the installation process. Follow the on-screen instructions to complete the installation.

Step 2: Install Leiningen

Leiningen is not included in the Clojure installer, so you need to set it up separately.

  1. Download leiningen.bat: Visit the Leiningen GitHub repository and download the leiningen.bat file.

  2. Add to PATH: Place the leiningen.bat file in a directory that is included in your system’s PATH environment variable. This allows you to run Leiningen from any command prompt.

  3. Run Self-Install: Open a command prompt and run the following command:

    This command downloads the necessary Leiningen files and sets up your environment.

Step 3: Verify Installations

To confirm that everything is set up correctly, check the versions of Clojure and Leiningen:

  1. Check Clojure Version: Open a command prompt and run:

  2. Check Leiningen Version: Execute:

    Both commands should display the respective version numbers, indicating successful installation.

Best Practices for Clojure Development on Windows

  • Use a Modern IDE: Consider using an IDE with Clojure support, such as IntelliJ IDEA with the Cursive plugin, to enhance your development experience.
  • Leverage REPL: Take advantage of Clojure’s interactive REPL for rapid prototyping and testing.
  • Version Control: Use Git for version control to manage your Clojure projects effectively.
  • Regular Updates: Keep your tools and libraries updated to benefit from the latest features and security patches.

Common Pitfalls and Troubleshooting

  • Execution Policy Errors: If you encounter issues with script execution, ensure that the execution policy is set correctly using Set-ExecutionPolicy.
  • PATH Configuration: Double-check that leiningen.bat is in a directory included in your PATH. Incorrect PATH settings can lead to command not found errors.
  • Network Issues: If downloads fail, verify your internet connection and try again.

Conclusion

Setting up Clojure on Windows is a straightforward process, whether you choose the convenience of Scoop or prefer manual installation. With your environment ready, you’re equipped to explore the powerful combination of Clojure and NoSQL databases, leveraging your Java expertise to build scalable data solutions.

Quiz Time!

### What is the recommended tool for installing Clojure on Windows?

— [x] Scoop
— [ ] Chocolatey
— [ ] Homebrew
— [ ] npm

> **Explanation:** Scoop is recommended for installing Clojure on Windows due to its simplicity and ability to manage dependencies.

### Which command is used to set the execution policy in PowerShell?

— [x] Set-ExecutionPolicy RemoteSigned -scope CurrentUser
— [ ] Set-Policy RemoteSigned
— [ ] Set-Execution RemoteSigned
— [ ] Set-PolicyExecution RemoteSigned -scope CurrentUser

> **Explanation:** The `Set-ExecutionPolicy RemoteSigned -scope CurrentUser` command allows the execution of scripts signed by a trusted publisher for the current user.

### How do you verify the installation of Clojure using Scoop?

— [x] clojure -M —version
— [ ] clojure —check
— [ ] clojure -v
— [ ] clojure —verify

> **Explanation:** Running `clojure -M —version` displays the installed version of Clojure, confirming the installation.

### What is the purpose of the `lein self-install` command?

— [x] To download and set up Leiningen
— [ ] To update Leiningen to the latest version
— [ ] To uninstall Leiningen
— [ ] To verify Leiningen installation

> **Explanation:** The `lein self-install` command downloads the necessary Leiningen files and completes the setup process.

### Which file must be downloaded for manual installation of Leiningen?

— [x] leiningen.bat
— [ ] lein.bat
— [ ] lein.exe
— [ ] lein.sh

> **Explanation:** The `leiningen.bat` file is required for setting up Leiningen manually on Windows.

### What should you do if you encounter script execution errors in PowerShell?

— [x] Check and set the execution policy
— [ ] Reinstall PowerShell
— [ ] Use Command Prompt instead
— [ ] Disable script execution

> **Explanation:** Setting the execution policy correctly resolves script execution errors in PowerShell.

### Where should `leiningen.bat` be placed for manual installation?

— [x] In a directory included in the PATH
— [ ] In the Windows System32 folder
— [ ] In the Clojure installation directory
— [ ] In the user’s Documents folder

> **Explanation:** Placing `leiningen.bat` in a directory included in the PATH allows it to be executed from any command prompt.

### What is the primary advantage of using Scoop for installation?

— [x] Simplifies dependency management
— [ ] Provides a graphical user interface
— [ ] Offers more installation options
— [ ] Integrates with Visual Studio

> **Explanation:** Scoop simplifies dependency management and ensures that software is up to date, making it ideal for command-line installations.

### True or False: Clojure can only be installed on Windows using Scoop.

— [ ] True
— [x] False

> **Explanation:** Clojure can be installed manually on Windows without using Scoop, although Scoop is recommended for its simplicity.

### Which IDE is recommended for Clojure development on Windows?

— [x] IntelliJ IDEA with Cursive
— [ ] Visual Studio Code
— [ ] Eclipse
— [ ] NetBeans

> **Explanation:** IntelliJ IDEA with the Cursive plugin is recommended for Clojure development due to its robust support for Clojure features.

Понравилась статья? Поделить с друзьями:
0 0 голоса
Рейтинг статьи
Подписаться
Уведомить о
guest

0 комментариев
Старые
Новые Популярные
Межтекстовые Отзывы
Посмотреть все комментарии
  • Служба центр обновления windows сама включается
  • Ошибка проверки контрольной суммы файл c windows system32 wininet dll
  • Образ windows для raspberry pi
  • Windows 10 education что это за версия
  • Как посмотреть на сколько гигабайт видеокарта на windows 10