# How can I run Alloy in command line?

**URL:** <https://alloytools.discourse.group/t/how-can-i-run-alloy-in-command-line/275>\
**Category:** Questions\
**Created:** [July 23, 2022, 9:53am UTC](https://alloytools.discourse.group/t/how-can-i-run-alloy-in-command-line/275 "2022-07-23T09:53:43Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![kindaro](https://yyz2.discourse-cdn.com/free1/user_avatar/alloytools.discourse.group/kindaro/32/144_2.png) [@kindaro](https://alloytools.discourse.group/u/kindaro)\
**Post date:** [July 23, 2022, 9:53am UTC](https://alloytools.discourse.group/t/how-can-i-run-alloy-in-command-line/275/1 "2022-07-23T09:53:43Z")

</div>

I have version `6.1.0.202111031525` which, as I understand, is fairly recent.

Unfortunately I must say that the GUI is not suitable for me. I have a finely tuned emacs installation that I should rather edit my Alloy code with. I can then open a file in Alloy’s GUI and press some buttons to make it check my models — but this is far from an ideal working environment.

With other languages, I can write a one line shell script that watches for changes in my source code and runs the compiler automatically every time I save my project. I should like to have a similar arrangement with Alloy. How can I achieve it? Ideally I should like for Alloy to check the model, print the instances and also save their graph representations to files that I can view with a viewer of my choice.

I see that there are some command line possibilities:

```nohighlight
% alloy --help --quit
Usage: alloy [options] file ...
  // -d/--debug set debug mode
  -h/--help show this help
  -q/--quit do not continue with GUI
  -v/--version show version

```

So, I can do like this:

```nohighlight
% alloy --quit example.als

```

— But this does not seem to do anything. Truly it will exit successfully even if I give it a random file. It does not even check the syntax!

---

<div class="post-metadata">

**Author:** ![amsmith](https://yyz2.discourse-cdn.com/free1/user_avatar/alloytools.discourse.group/amsmith/32/152_2.png) [@amsmith](https://alloytools.discourse.group/u/amsmith)\
**Post date:** [August 17, 2022, 7:22pm UTC](https://alloytools.discourse.group/t/how-can-i-run-alloy-in-command-line/275/2 "2022-08-17T19:22:31Z")

</div>

Here are two ways of integrating scripts with Alloy without writing any new Java code.

#1 There are some other classes in the Alloy jar that expose a main method that might be useful. Here’s an example of abusing the _ExampleCompilingFromSource_ class to evaluate a model that has been packed into a single command line argument:

`$ java -cp org.alloytools.alloy.dist.jar edu.mit.csail.sdg.alloy4whole.ExampleCompilingFromSource "sig Room {link: set Room } {#link < 4} fact { some home:Room | home.*link = Room } run {} for exactly 5 Room"`

#2 You can use Alloy’s Java APIs directly from scripting languages using a foreign function interface library. Here’s an example of calling into the Alloy jar from Python using JPype:

> **[Google Colaboratory](https://colab.research.google.com/drive/1Bck7xNgMrVm8wkM1ZBeRWEBng4g8nWln?usp=sharing)**

---

<div class="post-metadata">

**Author:** ![evelynm](https://yyz2.discourse-cdn.com/free1/user_avatar/alloytools.discourse.group/evelynm/32/195_2.png) [@evelynm](https://alloytools.discourse.group/u/evelynm)\
**Post date:** [August 17, 2022, 8:28pm UTC](https://alloytools.discourse.group/t/how-can-i-run-alloy-in-command-line/275/3 "2022-08-17T20:28:08Z")

</div>

Alloy from Python is just what I needed.

Thank You!

Evelyn Mitchell

---

<div class="post-metadata">

**Author:** ![Alejandro](https://avatars.discourse-cdn.com/v4/letter/a/ee7513/32.png) [@Alejandro](https://alloytools.discourse.group/u/Alejandro)\
**Post date:** [May 4, 2024, 3:13pm UTC](https://alloytools.discourse.group/t/how-can-i-run-alloy-in-command-line/275/5 "2024-05-04T15:13:29Z")

</div>

Hello everyone. Could you share your emacs setup for alloy?

I’m new to alloy and I’m probably missing something, but only I see here a way to generate an instance just like I pressed the execute button. How to emulate the new button?

And how to get png or dot from generated data?

---

<div class="post-metadata">

**Author:** ![Alejandro](https://avatars.discourse-cdn.com/v4/letter/a/ee7513/32.png) [@Alejandro](https://alloytools.discourse.group/u/Alejandro)\
**Post date:** [May 4, 2024, 8:39pm UTC](https://alloytools.discourse.group/t/how-can-i-run-alloy-in-command-line/275/6 "2024-05-04T20:39:37Z")

</div>

As far as I understand, the solution in the linked example is of type A4Solution, and it has a `next()` method.

[https://alloytools.org/documentation/alloy-api/edu/mit/csail/sdg/alloy4compiler/translator/A4Solution.html#next()](https://alloytools.org/documentation/alloy-api/edu/mit/csail/sdg/alloy4compiler/translator/A4Solution.html#next())

And it seems there’s no `prev()` method. But the good news is we can just log all the solutions we’ve seen so far.

Now, how to convert an instance to png or dot?
