r/javahelp • • Jun 27 '21

What is the Java equivalent of Javascript’s (node’s) “npm install”?

When you git clone a node project, you usually run “npm install” to download all the project’s dependencies so you can run the project locally. What is the java equivalent for this? I git cloned a java project from github and apparently I need MySQL, maven, and JDK 11 to run the project. I made sure to have all that. I started the MySQL server. I then ran the project locally from visual studio code. I keep getting errors when trying to run the project locally and I’m worried it might be because I don’t have the dependencies installed correctly.

17 Upvotes

12 comments sorted by

•

u/AutoModerator Jun 27 '21

Please ensure that:

  • Your code is properly formatted as code block - see the sidebar (About on mobile) for instructions

  • You include any and all error messages in full

  • You ask clear questions

  • You demonstrate effort in solving your question/problem - plain posting your assignments is forbidden (and such posts will be removed) as is asking for or giving solutions.

    Trying to solve problems on your own is a very important skill. Also, see Learn to help yourself in the sidebar

If any of the above points is not met, your post can and will be removed without further warning.

Code is to be formatted as code block (old reddit: empty line before the code, each code line indented by 4 spaces, new reddit: https://imgur.com/a/fgoFFis) or linked via an external code hoster, like pastebin.com, github gist, github, bitbucket, gitlab, etc.

Please, do not use triple backticks (```) as they will only render properly on new reddit, not on old reddit.

Code blocks look like this:

public class HelloWorld {

    public static void main(String[] args) {
        System.out.println("Hello World!");
    }
}

You do not need to repost unless your post has been removed by a moderator. Just use the edit function of reddit to make sure your post complies with the above.

If your post has remained in violation of these rules for a prolonged period of time (at least an hour), a moderator may remove it at their discretion. In this case, they will comment with an explanation on why it has been removed, and you will be required to resubmit the entire post following the proper procedures.

To potential helpers

Please, do not help if any of the above points are not met, rather report the post. We are trying to improve the quality of posts here. In helping people who can't be bothered to comply with the above points, you are doing the community a disservice.

I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.

28

u/edubkn Jun 27 '21

mvn compile or gradle build

15

u/feral_claire Software Dev Jun 27 '21

Maven is basically the Java equivalent of NPM. Gradle is another popular option. However, with Java, you don't "install" dependencies like with NPM. Dependencies are declared in the pom.xml (basically the package.json for maven) and maven will automatically download the dependencies as needed such as when running mvn compile to compile, mvn test to compile and run tests, or mvn package to compile, run tests, and package the compiled code into a jar file.

Your ide should be able to handle this for you, reading the pom file and the run button should work as ex. If you are going to be using much Java I would recommend using a Java ide such as intellij or eclipse. VS Code with its extentions can work, but it's more to set up and will not be as good as a fully integrated Java ide.

As to why you are getting errors you need to show the errors you get or else we can only guess what is going wrong.

11

u/snot3353 COMPUTERS!!! Jun 27 '21

Depends what build tool you’re using. For example, with Maven you’d say “mvn clean install”.

9

u/feral_claire Software Dev Jun 27 '21

Usually you would just mvn compile or mvn package. There usually no need to clean. And most of the time you don't want to install, just build the project.

1

u/ZukoBestGirl Jun 27 '21

IMHO, never. Too much room for stupid stuff to happen. always clean whatever. Except for, you know, calling tools like mvn checkstyle:check

0

u/nutrecht Lead Software Engineer / EU / 20+ YXP Jun 28 '21

mvn verify master race!!! ;)

1

u/Orffyreus Jun 27 '21

The command mvn install is for installing your own maven project though. The dependecies a Maven project has are installed/downloaded automatically "globally" into the local Maven repository. And Maven deals with different (side-by-side) installed versions of dependencies. Gradle usually also relies on Maven dependencies, but it creates an own weird non portable cache, if there are no local Maven dependencies (or if mavenLocal() is not defined in the repositories block).

1

u/BigGuyWhoKills Jun 30 '21

I always run mvn clean all by itself first. IIRC, on the first run of a project, that will download all dependencies. It's nice to have that done at the simplest level, so I can better see what is going on (Maven can have a lot of output).

After that, I run package or test or whatever.

13

u/Camel-Kid 18 year old gamer Jun 27 '21

if you're using a build tool like maven/gradle you should have a pom file that you add the dependencies too, then you'll run a maven update/install

2

u/nleachdev Jun 27 '21

Not meant to replace other answers here, this is more of an add on:

While most IDEs will have support for maven/gradle, i recommend actually using them barbones through command line so that you can get a good grip of what's actually happening behind the scenes (when the IDE's do everything for you)

For ex, intellijs gradle plug-in is incredibly convenient, but you don't want these tools to be a black box. Ofc, regardless you will still need to manage the build script yourself, but I just think it's a good idea to start off using CL for these because its never enough to be able to use something. You really want to understand how they work. Whether that's because of possible/future interview questions, or for when you need to do something that isn't supported through an IDE plug-in

2

u/kiwidog8 Jun 27 '21

Other people already responded the correct answer, I'll just add that make sure you have VS Code Java Extension Pack (the one with Red Hat's extension), if your project structure is correct it will automatically build your project using Maven or Gradle in the background like Eclipse does.

To add on the answer, while not a perfect 1-to-1 comparison, Maven or Gradle (both do pretty much the same thing, Gradle is newer and modern while Maven is older and used in way more places) are effectively the NPM or Yarn of Java projects (except actually Java is much older so it's the other way around). But fair warning Maven will be much less intuitive to use than NPM and you will really need to take the time digging through the docs and reading Stack Overflow to understand it. That being said it is incredibly robust and is basically the gold standard for managing all aspects of Java projects (from what I've seen)