1

Recently I was trying to run 2048 bot. I'm not a java programmer and installing IDE just for running one program would be overkill. So I tried compiling and running it from command line, but that was not a simple task for me, mainly because of the dependencies. Then I was told, that maven might come in handy. So I wonder if one can easily compile and run a program using maven or whatever tool they have without installing IDE?

Community
  • 1
  • 1
x-yuri
  • 16,722
  • 15
  • 114
  • 161

2 Answers2

4

The pom.xml file will have everything in it you need to compile it. In this particular case, it is only declaring a single dependency, the selenium-firefox-driver. With maven, all POM (Project Object Model) files inherit defaults from a "master" parent POM file. Maven uses a "convention over configuration" philosophy. Anything not explicitly configured, defaults to standard configurations from the parent pom that is part of maven. That is why you can build a project from such a seemingly simple POM file.

You will not be able to run the build from the IntelliJ IDEA module (.iml) file. In fact, IntelliJ IDEA generates that file from the POM.

First, make sure you have a Java JDK installed. Java 8 is the latest current. But a Java 7 JDK would be fine. After that, the Running Maven link @jeroen_de_schutter provided has all the information you need. Click on the top link in that document to Download and Install Maven. Once that is done, from a command line, navigate into the directory that contains the project (i.e. the directory with the pom.xml file in it) and issue the command: mvn compile to compile your code, mvn test to compile and test, or mvn package to compile, test and package the code. You can add clean to that any of those commands so maven will do a clean build. For example: mvn clean package. See the second Quick Start and third Use Maven links in the Running Maven document for more details and information.

Please note that the first time you run a build (any maven build) it will take quite a bit longer than normal. This is because maven has to download (and cache) a ton of plug-ins and dependencies. Once that is done, the builds go much much quicker. However, the first time you build a new project, the first build make take a little longer as it downloads and caches the dependencies and plug-ins needed by that project that have not already been retrieved.

Javaru
  • 30,412
  • 11
  • 93
  • 70
  • Thanks for the detailed answer. Things are getting clearer. I might haven't made myself clear enough though. I was able to compile the program, because I knew there were that `selenium` dependency. So I just downloaded the first thing on their download page that were labeled with a word `java`. Then `javac Program.java -cp selenium-java...jar` and it's there, waiting for me to run it. But then the real problems started. Suddenly, `selenium-java...jar` wasn't enough anymore to run the program. In the end, I run the program, but that was way too hard. – x-yuri Apr 05 '14 at 08:35
  • 1
    And now I realize, that `maven` has nothing to do with the second part, running the program that is. So I see the following ways to run the program: 1) run the program, see which classes are needed, download and add corresponding `jar`-file, and so on until it just runs (see last shell transcript in [my answer](http://stackoverflow.com/questions/22810359/how-do-i-know-which-jar-files-java-program-needs-to-run)), 2) take a look at `iml`-file, they all are supposedly there. The second way seems easier, if `iml`-file exists that is. Are there any better ways to run the program? – x-yuri Apr 05 '14 at 08:39
1

Yes you can, make sure you have a Java Development Kit and Maven installed. Then by following the Maven user guides you should be able to build it and run. But it might not be straightforward if you have never used maven, so I would recommend to ask the assitance of an experienced java developer, if you happen to know one.

jeroen_de_schutter
  • 1,843
  • 1
  • 18
  • 21
  • So here I am, asking an experienced java developer ;) As far as I can tell, [pom.xml](https://github.com/aaronshaver/2048_Bot_2.0/blob/master/pom.xml) doesn't contain enough information. [2048_Bot.iml](https://github.com/aaronshaver/2048_Bot_2.0/blob/master/2048_Bot.iml) seems more like it, but it's `IDEA`'s file which `maven` wouldn't read, I take it. By inspecting `2048_Bot.iml` I can run it manually, downloading and specifying all the dependencies. But I wonder if `maven` can simplify the task. – x-yuri Apr 04 '14 at 10:07
  • The pom should have everything you need. The iml file is for intellij's consumption, not for humans -- it contains things IntelliJ needs, not things that you need. The pom is everything, trust in it. – Software Engineer Apr 04 '14 at 16:22