Export JAR with Netbeans - java

How to export java project to JAR with Netbeans ? I cannot find options like in Eclipse.

You need to enable the option
Project Properties -> Build -> Packaging -> Build JAR after compiling
(but this is enabled by default)

Do you mean compile it to JAR? NetBeans does that automatically, just do "clean and build" and look in the "dist" subdirectory of your project. There will be the JAR with "lib" folder containing the required libraries. These JAR + lib are enough to run the application.
If you disable "Compile on save" in the project properties, then it is no longer necessary to do "clean and build", simply "build" will suffice in most cases. This will save time if you want to change just a bit of the code and rebuild the JAR. However, note that NetBeans sometimes fails to handle dependencies and binary compatibility properly, which will lead to a faulty JAR throwing "no such method" or other obscure exceptions. Therefore, if you made a lot of changes since the last full rebuild and even remotely unsure that it will still work even if some classes aren't recompiled, then you must still do a full "clean and build" in order to get a perfectly working JAR.

It does this by default, you just need to look into the project's /dist folder.

Right click your project folder.
Select Properties.
Expand Build option.
Select Packaging.
Now Clean and Build your project (Shift +F11).
jar file will be created at your_project_folder\dist folder.

Related

Generating JAR on IntelliJ

I have some problems while creating a jar of my intelliJ project. First of all I just do the usual procedure:
Go to project structure
Select artifacts
Create a new one with modules and dependecies
Go to build artifact, build it
Execute it at console with java -jar "ddddd.jar"
So when I execute the project (not the jar) all runs ok, libreries and all works, but not in the JAR.
Terminal of IntelliJ:
My libraries are located in the project like: project>lib>"some jars here">otherjars>"here some jars and respective dll
This is because the "other jars" need the dll to work. I already tried to run them without the .dll but doesn't work without it.
Also I have them in the project on Modules>Dependencies I have the folder selected as a library.
Any help would be appreciated.
Edit 1:
Here I add some pictures about how I have my libraries structured.

Project is missing required library - but correct JAR exists?

One of my eclipse projects gives me the Project X is missing required library: 'somelib.jar' error, but when I go to Configure Build Path > Libraries the library is present, and no error is indicated there.
I tried the usual things:
Clean build (all projects in the workspace)
Exited eclipse multiple times
My coworkers, using the same git checkout with the same status as mine, and also using eclipse Neon2, have no build errors.
Just to be sure, I navigated into the corresponding directory and removed a different jar file, and when I look at my build path configuration that jar shows up with a red "x" indicating that the library is missing.
I checked that somelib.jar sits in the expected place and is a valid JAR file.
How do I fix this?
These steps did resolve the problem:
closing / re-opening the corresponding project
another "full" clean of all projects in the workspace
In other words: it seems that a simple F5 "refresh" and a even a "clean project" isn't sufficient sometimes. So closing the project is the key element here!
Some times eclipse becomes naughty following steps should be taken to nicely add the jar library in project
First remove the library from libs folder
Also remove the refrence from java build path in external library tab
Clean the project let it actually realize the library is missing :D
Again copy the jar file in libs folder
Add the refrence of library in java build path library tab
set the priority of this library to top (important) because its
possible the order of library export is not ok in your project
Clean the project and build
hope so it will solve your problem
If you have multiple projects opened in such way that one depends on another, and the problematic project depends on others, refreshing all dependent projects will fix the 'missing library' issue for the main project.

Moving from NetBeans to eclipse, can't seem to find generated java files

I have been developing in NetBeans, I am exploring changing over to eclipse.
We have a set of directories for the modules in our project; we have scripts that check things out of Subversion and do other things with that directory tree, and we have NetBeans access the sources, etc., from that directory tree.
All our modules are built, etc., with Maven. All of this works fine from within NetBeans.
I have successfully imported all the projects into eclipse (File / Import / Maven), but evidently something is different in the handling of "generated sources". We have some castor-generated files in one module, and jaxb-generated files in another. The generated files in both cases are not found by the compiler, and therefore these modules have compile errors.
Maven is configured to generate the Java classes which cannot be found; maven clean deletes them and maven compiler:compile puts them back. But in eclipse those java files cannot be found for compiling other classes, but in NetBeans they can.
I tried deleting the generated java files, then rebuilding in eclipse; the java files were regenerated in generated-sources, but still cannot be found by the compiler when it comes time to compile (or syntax-highlight) my code.
It sort of looks like a classpath issue, but we don't set the classpath in NetBeans for a maven project. How should I configure things in eclipse so the generated class files are found?
In Eclipse in the Package Explorer right click the project folder which contains the missing files. Goto "Build Path >" then "Configure Build Path...". Properties for your project folder will open.
Click on the "Source" tab. Then click on the "Add Folder..." on the left. A source folder selector box opens, here select your folder which contains the auto generated sources. Normally it will be in the target directory. Then press ok. If this will not work, try one directory deeper or less deep.
If there is no target directory, build the project folder with maven on a shell.
The generated-sources directory will be stored in the .classpath file in your project folder.
HTH.

I've removed .class file from my project in eclipse thinking it'll be regenerated again. But it didn't

From my eclipse project I've removed .class file for a particular class thinking eclipse will regenerate it for me if I build it again or run the project again.
But when I tried to run the program, It's showing class not found exception.
I'm wondering what's happening there? doesn't eclipse build .class files each time we run the project? when compilation will happen in eclipse?
Compilation will happen either the next time the source file is modified, if you have it set to Build Automatically, or the next time you run. Both of these expect compiled source files to still have their .class files around. Clean the project from the Project menu to force it to recompile regardless.
Make sure that in the Project up-menu, the option "Build Automatically" is checked then just clean your project.
It should regenerate the .class file(s).
Check Your java file is in a source folder (right click project, properties, build path)
and you have automatic build turned on (Project menu -> Build automatically)
Also make sure your project compilation/build succeeds, otherwise Eclipse may not compile all the classes.
Check in the Problem windows if there is any error on the project compilation then fix it first.
Project menu
Click "Clean"
Select "clean all projects" or "Clean selected projects below" and
choose your project
Check "Start a build immediately"
Click "ok"
Now run your program.
Also as suggested by other answers, enable "Build Automatically" in Projects Menu.
Hope it helps

Why can I run my program after cleaning in Eclipse and Maven?

I cannot understand why in Eclipse after these two steps
Run as -> Maven clean
Project -> clean...
I can still run my program using green icon with white arrow?
I thought there are no .class files after cleaning. I was expecting exception like ClassNotFound, but nothing like this happened.
Do you know how to properly clean project in Eclipse?
Clean in eclipse is not the same as clean in maven.
Maven's clean indeed removes all compiled resources (classes etc).
Clean project in eclipse is a kind of mvn clean compile, i.e. it removes *class files and compiles all *.java files again.
I just checked this phenomenon out with one of my Maven projects in Eclipse and this is what I observed. By the way, don't trust what you see in the Package View, use your file system explorer.
Run as -> Maven clean : deletes the target folder
Project -> clean... : creates target folder with empty subfolders "classes" and "test-classes". In Eclipse's Package Explorer view, I see an empty target folder.
At this point I would say the proper way to clean a Maven project in Eclipse is simply to do Run as -> Maven clean.
Now to address why your program runs after cleaning. As soon as I ran a program in the project, Eclipse compiled and populated the "target/classes" and "target/test-classes" folders with *.class files and resources. In Eclipse's Package Explorer view, I still see an empty target folder.
The trick to figuring this out was to look at the project directory and sub directories with the system file explorer and not just with Eclipse's Package View or Project View.
In starting code for your application you can add:
System.out.println("Classpath=" + System.getProperty("java.class.path"));
you can analyze it (nornally I use notepaddpp to split it by classpath separator and sort) - maybe it refers to jars in maven repo which contains classes as well? Sometime class folders are first on classpath and thus are before jars even if in jars there are the same classes.
This is probably because eclipse will auto build your project when you run it

Categories