run java file in linux command with maven dependencies - java

I have written a Java program with intellij and it's working, but now I want to run this file with a command, but when I try to compile it with javac I get errors because it doesn't recognise my imports, I'm new to programming so how can I run my program knowing that it has dependencies in maven?

If you have a choice, you should use maven command (mvn) rather than the low-level compiler javac, it makes compiling and packaging much easier.
But if you want to use javac, you must pass the path to the jar your code depends on, as explained in : How do I compile a java file that has jar dependencies?
If you have pulled your dependencies using maven and you are using centos, the jars should be stored in directory ~/.m2
But once again you should consider installing and using maven, if you want to manually compile your code in a similar way Intellij does

Maven was created to make it easier to handle builds with dependencies (jar files) which can be automatically downloaded from the internet. This includes invoking javac with all the dependencies on the build classpath using "mvn compile" - you do not have to invoke javac yourself! Look in target/classes for your byte code files compiled by Maven.
You can also ask Maven to run your Java class with the required dependencies available. Use the following command (adapted as necessary):
mvn exec:java -Dexec.mainClass="com.example.Main"
See http://www.mojohaus.org/exec-maven-plugin/usage.html for more details.

Related

How do I run a java application with maven from command line on ubuntu?

I'm trying to run this java application with maven from command line on ubuntu with OpenJDK 13
openjdk version "13.0.2" 2020-01-14
OpenJDK Runtime Environment AdoptOpenJDK (build 13.0.2+8)
OpenJDK 64-Bit Server VM AdoptOpenJDK (build 13.0.2+8, mixed mode, sharing)
The project is created with Intellij Idea.
I guess I've made the build successfully with these commands
git clone https://github.com/danvega/httpclient-tutorial.git
cd httpclient-tutorial
mvn package
However, I don't know how to run the application from command line.
I tried these commands
cd target/classes
java dev.danvega.Application
and got this error
Error: Unable to initialize main class dev.danvega.Application
Caused by: java.lang.NoClassDefFoundError: com/fasterxml/jackson/core/type/TypeReference
What am I missing?
You started Java without specifying where the Maven dependencies can be found, which is called CLASSPATH (and since Java 9 also MODULEPATH if you use the Java Module System), similar how *.dll files can be found in the PATH on windows or LD_LIBRARY_PATH can be used for *.so on Unix-systems
Please see the great answers and questions from others before you:
https://stackoverflow.com/a/20045263/6250649
Maven Run Project
Building a fat jar using maven
Is there a Maven plugin that runs a non-fat jar?
I personally prefer either:
The Maven Exec plugin https://www.mojohaus.org/exec-maven-plugin/examples/example-exec-for-java-programs.html
Create a fat jar (e.g. with the Maven Shade plugin) that is also practical for distributing your code and all required dependencies so anyone can easily start with java -jar yourfat.jar (or just click and it will start on Windows)
Here, you have maven project which has one dependency on jackson-databind which in turn will have some more dependencies i.e jackson-core and jackson-annotations.
Classes from these dependencies are not bundled in your application jar, so you cannot just run the Application main class from your project directly using java command, you need to specify the dependent classes on java classpath so that java can load these dependent classes of your program.
Since, it is a maven project, these dependent jars will be pulled into maven default directory (.m2) into your's home path and as you mentioned, you are using ubuntu that will be /home/<your username>/, For example your username which you are logged in with is singularli then your home path must be /home/singularli, you can also check it with echo $HOME command.
So, you would find the maven folder, which stores all the jar(s), into your home /home/singularli/.m2/repository, now here you would find jars like jackson-databind, jackson-core (these will be little inside subdirectories, as it keeps according to the package name, given below command example will give you more idea about it).
At last, once you find these jars, you would need to specify the classpath using -cp flag and include these jars with your application jar which would look like as given below:
java -cp "target/httpclient-tutorial-1.0-SNAPSHOT.jar:/home/singularli/.m2/repository/com/fasterxml/jackson/core/jackson-core/2.11.4/jackson-core-2.11.4.jar:/home/singularli/.m2/repository/com/fasterxml/jackson/core/jackson-databind/2.11.4/jackson-databind-2.11.4.jar:/home/singularli/.m2/repository/com/fasterxml/jackson/core/jackson-annotations/2.11.4/jackson-annotations-2.11.4.jar" dev.danvega.Application
It should work the same way as shown in that video, you referred in your question.
Please notice that you may have different versions i.e com/fasterxml/jackson/core/jackson-annotations/2.11.4, I included 2.11.4 as an example, you may check the version in this project and include that, if different versions are there and you included anyone of them, it may cause some issue as some feature used in this project might not be present in that version
The third-party dependency that contains com/fasterxml/jackson/core/type/TypeReference (which you can find the pom.xml com.fasterxml.jackson.core:jackson-databind is required at both compile time and runtime. If you run using the java command, you need to specify the dependency on the classpath. But since you are using Maven, there is an exec-maven-plugin that you can use for convenience which will handle the classpath at runtime:
mvn exec:java -Dexec.mainClass="dev.danvega.Application"
You can also compile then run in the same command:
mvn package exec:java -Dexec.mainClass="dev.danvega.Application"

how does a Java program find its Maven packages?

I have a Java program in IntelliJ which has a pom.xml and uses Maven. The packages were downloaded and currently they are found by IntelliJ.
I'm a little confused though because the Maven repository is not part of the CLASSPATH as far as I can tell. So does IntelliJ just do a bit of magic where it looks into its Maven repository to find the packages? (I think that IntelliJ has its own Maven repo. I separately have Maven 3 installed, but I think it isn't using it.)
But more generally: If you build a JAR using Maven then I guess it will put the dependencies in the JAR where the Java program can find them, so there won't be a problem. But if you just run a Java program directly, do you need to add the Maven repository to your classpath or does something else happen?
Thanks for any information you can provide to lessen my confusion :)
When you start the program from IntelliJ using a runtime configuration for your main() method IntelliJ constructs the classpath from all the project dependencies. You can see this in the Run window, the first log line is the java command used to start the main(). It's a long line but it usually looks similar to:
java -javaagent:/opt/idea/idea-IC-173.3727.127/lib/idea_rt.jar=40165:/opt/infra/idea/idea-IC-173.3727.127/bin -Dfile.encoding=UTF-8 -classpath /home/ [...]
IntelliJ constructs the -classpath argument adding both the module target directory and the Maven dependencies referenced from the local Maven repository.
When you package the project using Maven mvn clean package usually it becomes a standalone JAR with just your code (unless you changed the defaults). Now you have a few choices how to provide dependencies needed to start your main():
Provide them using -classpath parameter just like IntelliJ.
Add maven-shade-plugin and use shade goal to the build a runnable Uber JAR. This creates a fat JAR which doesn't require -classpath.
Use some other Maven plugin to perform point 2 e.g. Spring Boot spring-boot:repackage goal.
All the required dependencies, defined in the pom.xml file(s), are downloaded from Maven Central (or others if configured) to the local Maven repository. That repository is located at <user home>/.m2/repository.
Maven generates/calculates a dependency tree to know all the required dependencies for the project. (you can also dump that tree with the command mvn dependency:tree. I always pipe the result to a file, because the tree can be large mvn dependency:tree > deptree.txt). Maven put them all on the classpath when executing a maven command like mvn compile
IntelliJ also use/calculate the dependency tree and add all the jar files to the projects classpath (point to the files in the <user home>/.m2/repository folder). You can see them all in the list with External Libraries, and they will be used / on the classpath for compilation and running the application.
When building a JAR file the dependencies are NOT added to the JAR. Only the bytecode (java classes) and resources from your own project are packaged into the JAR file. (Source files can also be packaged if you configure that)
By adding a Maven plugin (maven-shade-plugin) you can configure your project to also pack dependencies into the JAR. SpringBoot projects also will do that.

How can I compile Scala code without using scalac

Let's suppose I have a file called Main.scala that I want to compile and run.
In a normal environment I can just do scala Main which automatically performs compilation and runs the file.
Now I am trying to deploy scala source code to another Server, on which I am not allowed to install scala. It is just possible to copy files (such as scala-library.jar and scala-compiler.jar) and Java 1.6 is pre-installed.
It would of course be possible to compile to bytecode locally using scalac Main.scala and execute the resulting .class-files using java -cp [...] Main.
For my workflow it would, however, be better if I could just checkout and compile the scala sources on the remote server directly.
Is there any possiblity to realize this compilation task with the tools available or by copying additional binary/jar files?
I would strongly suggest that you use a build tool. Many open source projects use sbt (http://www.scala-sbt.org/). You can use an sbt plugin (https://github.com/sbt/sbt-assembly) to create a fat jar with all your dependencies.
You can also use Maven and Gradle.

How do I compile and run Intelij IDEA java project without IDE?

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?
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.
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.

compile a java project with different packages

I have a java project in eclipse with different packages. How can I compile the project from command line in unix (bash) ? Using plain javac doesnt seem to work. For eg, in eclipse I have
src
...server
...client
...shared
And the main file that I want to run later is in the server package called server.java
You will have to add all the source paths and dependency .jar files into the compilation call. The easiest way to do this will be to get Eclipse to export an Ant build.xml for the final jar you are trying to compile. This autogenerated ant file is usually pretty messy but gives you an idea of how the project should be built.
javac -sourcepath /path/to/src -d /path/to/classes

Categories