Problem:
In short, I can run my uber jar (generated by the maven shade plugin) OUTSIDE of eclipse, by using java -jar myJar.jar (the main class is specified in the maven jar plugin), BUT I can't run the Main class inside the eclipse.
when running in eclipse, it runs from the target\classes folder, and I check all the dependencies are correctly listed in the classpath.
I also checked java -version, both eclipse and cmd got the same version
I wander what difference would it be?
add
-vm
C:program\javaxxxx\jre\bin\javaw.exe fix this
not java version for me has to be 1.7+
the eclipse.ini
its mainly because java.home is used in one of the pom.
eclipse failed to pass the java home
then such pom fails to pass the transitive dependencies
therefore libraries will go missing
Take look of this.
Maven not picking JAVA_HOME correctly
Related
I am new to programming, there is a maven project with java code that does certain testing and it runs regularly through Jenkins pipeline, it runs the tests on an environment (linux machine).
Now what I am struggling and not knowing where to start is how can I test this code manually knowing that it has different Mains (i.e Main1 , Main2 , Main3) and I want to run a certain Main not all, for example only Main1 which tests specific thing.
ps: I have the code in Intellij, but I want to run it outside intellij, I want to run it on the environment I'm testing (linux maachine).
first you have to build the maven project using this command in your project directory:
mvn package
After a successful build, you will see a .jar file has created in target folder same as your package name and version.
finally you have to RUN the project.
To run the project use this command:
java -cp target/jarfileName.jar path_of_the_project_startup
Done.
First, you need to build your maven project. Navigate to the project folder (you must have the project root pom.xml there) open a terminal and to build it type:
mvn clean package
Depending on your project structure, a successful run of this command will result in several target folders at different levels for your modules and possibly .jar files inside them. Navigate into the target folder of the module in which your entry point (main) is and check jar file name. Let's call it jarname.jar for simplicity.
In case you have several entry points in the same jarname.jar, you can run them like this:
java -cp jarname.jar Main1
java -cp jarname.jar Main2
java -cp jarname.jar Main3
If you want to run from terminal and still debug from IntelliJ, run app with:
-agentlib:jdwp=transport=dt_socket,server=y,suspend=n,address=5005
and then create a 'Remote JVM Debug' config in IntelliJ on same port (5005), adding some break points and 'Debug'.
As an alternative, you can create Run Config 'Java Scratch' from IntelliJ to run/debug your app.
I created a JavaFX project in IntelliJ.
I can run project in IntelliJ. I added below code in Configurations):
--module-path ${PATH_TO_FX} --add-modules=javafx.controls,javafx.fxml
But the output .jar file of project (made with Artifects) doesn't run. I tested these commands, but didn't get any chance:
java --module-path %PATH_TO_FX% --add-modules javafx.controls,javafx.fxml -jar Timer.jar
java --module-path %PATH_TO_FX% --add-modules javafx.controls Timer.jar
Last error log of command line:
Error: Could not find or load main class Files\Java\javafx-sdk-11.0.1\lib
Caused by: java.lang.ClassNotFoundException: Files\Java\javafx-sdk-11.0.1\lib
p.s: I could run .jar file of this project when build on JDK-10
EDIT:
I downloaded JavaFX and added it's lib folder to System Environments.
for adding JavaFX to project I did this process:
Project Structure > Libraries > add > Java > JavaFxPath/lib
Then I created Artifect for output jar file in this process:
Project Structure > Artifects > Add > JAR > From Modules with dependencies > main Class : main.Main.
Providing you have a simple (non-modular) JavaFX 11 project (without Maven/Gradle build tools), and you are using IntelliJ, like the HelloFX sample from here,
this is how you can create a jar from IntelliJ that can be run from the console
A full tutorial on how to run the project can be found here, and instructions on how to create a jar are here (see section Non-modular project), but these doesn't cover Artifacts from IntelliJ.
Check that the HelloFX project runs from IntelliJ with these VM options:
--module-path ${PATH_TO_FX} --add-modules javafx.controls,javafx.fxml
where PATH_TO_FX has been set in File -> Settings -> Appearance & Behavior -> Path Variables, pointing to the JavaFX SDK lib.
Semi fat Jar
We can create a Jar that only contains the classes from the project, and third party dependencies, but not JavaFX ones.
Go to File -> Project Structure -> Artifacts -> Add -> JAR -> From modules with dependencies, add your main class, accept.
Then remove the JavaFX jars from the list, and accept.
Build the project, it will create a quite small jar (3 KB in this case).
Now you should be able to run it like:
java --module-path %PATH_TO_FX% --add-modules javafx.controls,javafx.fxml -jar out\artifacts\HelloFX_jar\HelloFX.jar
(make sure that %PATH_TO_FX% points to a valid folder and use quotes if it contains spaces.
You can distribute this jar, and run it in other platforms, providing those also have the JavaFX SDK.
Fat Jar
If you want a full fat jar that includes JavaFX dependencies, you can still use Artifacts.
Go to File -> Project Structure -> Artifacts -> Add -> JAR -> From modules with dependencies, add your main class, accept.
Then keep the JavaFX jars from the list, and accept. Build the project.
In theory, you should be able to run it like:
java -jar out\artifacts\HelloFX_jar\HelloFX.jar
But this won't work.
Reason 1: You need a launcher class, as explained here.
So create a launcher class:
public class Launcher {
public static void main(String[] args) {
Main.main(args);
}
}
Reason 2: If you only add your SDK jars to the fat jar, you will be missing the native libraries, as explained here.
So edit the artifact, select the Launcher class as main class, and add the native libraries (Directory Content -> path-to/JavaFX SDK/bin on Windows):
Now build the project (now the jar is about 33 MB, and contains unnecessary native libraries) and run:
java -jar out\artifacts\HelloFX_jar\HelloFX.jar
You can distribute this jar, but only to Windows platforms.
You can create similar jars for other platforms, if you download their JavaFX SDKs, and you can also build cross-platform jars if you add them all together, as explained in the linked answers above.
Anyway, you should consider using jlink instead.
Note
About this error:
Caused by: java.lang.ClassNotFoundException: Files\Java\javafx-sdk-11.0.1\lib
it looks like the library path was set without quotes and it is missing the first part of the path C:\Program Files\.... Just make sure you use quotes:
set PATH_TO_FX="C:\Program Files\Java\javafx-sdk-11.0.1\lib"
I had the similar issue exporting/generating an Jar using JavaFX and IntelliJ Non-modular with Gradle (https://openjfx.io/openjfx-docs/)
The jar I was generating using Gradle jar command does not run and throws error saying it can not find my Main Class. When I opened my jar I was able to locate my main class. So I realized the error has to do with Jar Packaging.
I fixed the problem by adding JavaFX SDK to my Java SDk in IntelliJ as shown below.
After this I use the regular Gradle build Jar command to generate my Jar file (as shown below) and it runs Normally.
The easiest way to do this is to use an OpenJDK build that include JavaFX. Both Bellsoft and Azul produce such builds.
For Azul's Zulu builds of OpenJDK:
https://www.azul.com/downloads/zulu-community/?version=java-11-lts&package=jdk-fx
For Bellsoft Liberica JDK:
https://bell-sw.com/pages/downloads/#/java-11-lts
and choose "Full JDK"
These builds are basically OpenJDK with the OpenJFX JavaFX modules added. Though be careful as some aspects of JavaFX may not be supported on LibericaFX. See https://bell-sw.com/pages/liberica-release-notes-11.0.9.1/
The above answers apply to non-modular JavaFX projects. To get a modular JavaFX project (with modular dependencies) running I used jlink.
Take the JavaFX SDK from https://gluonhq.com/products/javafx/; unzip the files to a directory, and point a shell variable JFX_LIB to the unzipped lib/ directory.
Take the JavaFX jmods (not SDK) file from https://gluonhq.com/products/javafx/; unzip the jmods, they are in a folder similar to javafx-jmods-11.0.2/. Point a shell variable JMOD_PATH to the unzipped directory javafx-jmods-11.0.2/.
Compile, sending your compiled classes to the directory mods/:
javac -d mods --module-source-path src --module-path $JFX_LIB/javafx.graphics.jar:$JFX_LIB/javafx.base.jar:$JFX_LIB/javafx.controls.jar --module com.mymodule
Create a custom JRE with jlink, referring to the module path containing the JavaFX mods and your own compiled mod:
jlink --module-path $JMOD_PATH:mods --add-modules com.mymodule --output customjre
Run:
customjre/bin/java --module com.mymodule/com.mymodule.Main
(I tried for a LONG TIME to get a java -jar running with --module-path and --add-modules. I always got the following error:)
Error: Could not find or load main class com.mymodule.Main
Caused by: java.lang.NoClassDefFoundError: javafx/application/Application
I have a very large maven project, and when I'm debugging, it sucks to have to rebuild the entire project just to see the changes. My current method is to compile the java file in IntelliJ, then copy the class file in my target directory, to my tomcat/webapps/project/ directory, and replace the existing class file with the intellij compiled class file. Then rebuild the war file, and replace it with the existing war file in the tomcat/webapps directory.
I want to be able to automate this process. I looked into doing it with the javac command, but I'm having problems with packages be imported from jar files in the .m2 directory, and can't figure out how to automate the classpath, depending on the specific file. I'm sure IntelliJ does this automatically, and was wondering if there's a way to run IntelliJ's compiler from the command line, or if anyone has any insight into how I can get javac to work for just the one file in my whole project scope.
Any help is appreciate, thanks!
IntelliJ delegates (by default, there are options to delegate to the Eclipse compiler or other ones) to the javac compiler.
If you do not want to import your project into IntelliJ, then you can use maven from the command line to build the project.
If you do not want to build the whole project, then you can use javac from the command line.
To generate your classpath, use :
mvn dependency:build-classpath -Dmdep.outputFile=classpath.txt
classpath.txt will contain your classpath.
From then you can do : javac -cp (contents of classpath.txt) your java file.
IntelliJ is fast even on large maven projects and it can easily compile into an exploded war your tomcat would point to. It is unclear to me on why you would not want to benefit from IntelliJ.
I'm trying to get a maven managed project to run on the command line.
I have a set of dependencies in the pom.xml which are subsequently downloaded and installed in the ~/.m2/repository/. I've included the necessary config in my pom to add the classpath to the jar manifest.
Now the problem is i'm attempting to run the jar thus: java -jar project-SNAPSHOT.jar.
Java can't find the downloaded dependencies (i'm assuming because they are listed without paths in the manifest?) , but i'm not sure how best to get this running.
Options 1:
The jar created does not have the dependent jar files. So, you need to tell java the class-path where all the dependent jars are
java -cp /lcoation/of/dependency1.jar:/location/of/dependency2.jar:/location/of/dependency3.jar -jar project-SNAPSHOT.jar
Option 2:
The easier and much better solution is to use AppAssembler plugin. What it does it packages your jar in a directory structure that contains
dependent jars
the created jar
shell/windows scripts to execute it
have a look here http://www.mojohaus.org/appassembler/appassembler-maven-plugin/
Option 3:
If you do not want all the baggage and just wanted to have one jar-with-dependency
You may want to refer here How can I create an executable JAR with dependencies using Maven?
This will contain all the dependent jars within it.
Edit 1: For Option 1, Brad M mentioned that you can get a list of all your project's deps using the dependency plugin. dependency:build-classpath
mvn exec:java -Dexec.mainClass="com.vineetmanohar.module.Main" -Dexec.classpathScope=runtime
You can find more examples here: 3 ways to run Java main from Maven.
I have a NetBeans project I would like to compile from the command line. There are many other questions on StackOverflow about how to do so, but they explain how to compile the project using commands like javac src/*.java.
I haven't changed my NetBeans project's build settings. By default, how can I compile my project from the command line using Ant? Once I've built my project, where is the compiled file located, and what format is it in (i.e., .class files, one .jar file, etc.)?
(I understand that asking how to use Ant to compile my project in general is too broad of a question. That's why I'm specifically asking about how to compile using NetBean's default configuration for a project.)
I'm using NetBeans 8.0.2.
ant compile Compiles the project (.class files are placed in the build/classes folder)
ant jar Compiles the project (see above) and builds a JAR ( located in dist/ )
If that doesn't work for you, check ant's output for errors. (Is the JAVA_HOME Variable set properly?)
I'm totally agnostic IDE developer. After several frustrating years trying to emerge "netbeans ant config" to something usable from command line I became to create a wrapper for netbeans ant.
https://github.com/albfan/ant-netbeans
By now you can:
detect defined targets with standard
$ ant tabtab
Rely on project will honor JDK_HOME
and most important
Expect all ant target to complete smoothly, passing test and whatever stuff implied.