Trying to add external Jars to a Java Project (without Maven) - java

I am trying to add external jars to my Java (without Maven) Project.
But I don't get it to work at all.
The official documentation: https://code.visualstudio.com/docs/java/java-project
says there are 2 ways to do it:
1."You can use the Java Dependency Viewer to add any JAR file to your project."
https://code.visualstudio.com/assets/docs/java/java-project/manage-dependencies.gif
As you can seee in the gif, the dev opens a "referenced libraries" folder.
This folder DOES NOT EXIST for me.
or 2."The other easy way to bring additional JAR files as dependencies is to create a lib/ folder in the root directory of the standalone files and place your JAR files there. Source for foo.jar is automatically detected if there is a foo-sources.jar in the lib/ folder."
https://code.visualstudio.com/docs/java/java-project/lib.mp4
I have the Jar file in the lib\ folder but it still shows me he same error messages.
Are there any steps I am missing?

As it says, place the external jar files in the ...\JAVArpg\lib folder.
So copy them from wherever you have them now. cp ../lib/*.jar lib/ is just an example.
You can use the Windows Explorer. You don't have to copy the files using a command-line.

Related

Java Project Environment Preparation

I am new to project environment setup. Below is my project structure in eclipse
Project Name
--> .settings
--> .bin
--> lib
--> resources
--> src
--> .classpath
--> .project
I am attempting to export src folder as jar.
When i export to jar, all the above folders & files are created in jar. But i need to convert only src folder as.
Also when i export to executable jar, all the third party libraries are exported as class files in jar. is it right.
What is the best practice to export project. Only src folder or everything.
Which i need to use jar/runnable jar. My requirement is to write start/stop bat file to call jar and execute java program.
Please advice me. Thanks in advance.
First it's important to know what these folders actually do. Following are the workings of several of these files.
.settings -> This file records project specific settings and workspace preferences.
.bin -> folder is usually where the compiled files are copied to.
lib -> contains external libraries that are used in your project (like Apache Commons)
resources -> the resources like images, text, pdf, audio, video are usually copied here
src -> the folder where the project's source files are located.
.classpath -> It contains information that the JDT feature needs in order to properly compile the project: the project's source folders, the output folders , and classpath entries.
.project -> This file is maintained by the core Eclipse platform, and its goal is to describe the project from a generic, plugin-independent Eclipse view.
So you can see that if you exclude some of the files like lib, resources, bin etc... Your jar file will probably stop working. Your jar file needs compiled files and their dependencies.
For example: All your compiled .class files are in bin folder. And your jar works because of these .class files and NOT .java files that are in src. If you delete this bin folder then your jar will probably stop working.
Also, your project may be using some external library supplied by someone else. Like Apache Commons or google/guava and these are usually in lib folder. So you can't delete this folder as well.
However, if you no longer expect to use .java code, then you can exclude files that were created by eclipse to manage this project. See this post.
see also:
1. What's in an Eclipse .classpath/.project file?
2. exclude files from jar or war in eclipse

Running my java project from a Jar

I have a Java project that I'm working on in Eclipse. I have all my code in a src. Eclipse automatically compiles my .java files and stores the .class files in a directory called tmp.
Earlier, I was suing Ant to run my project. My ant script would would compile my Java code, and Jar all the .class files into a .jar and store it into a jar directory. In this jar directory I have some other files e.g. settings.cfg and data.lst. My application needs these files to run but they should not be included in the Jar. They will be deployed along with my JAR and should reside in the same directory. Currently while developing, I have modified and customised my settings and properties files to contain additional information. I don't want these files replaced every time I have a new build as I would like the data to persist across builds. I'm just looking to get the built JAR into the jar directory and execute that. The rest of the files stay the same.
How can I configure my Eclipse so that it always compiles my code, JARs it to the jar directory and executes that JAR. That way, my "extra files" are always in the same place as the JAR.
I'm having a lot of trouble figuring out how to accomplish this.
If you need this feature for fast running/debuging, create new Run Configuration and in the Arguments tab, Working Directory specify the "jar" directory.
The answer was found on another SO post. It was about creating a new "Run Configuration", removing the default classpath entries and adding a the folder containing the JAR i.e. the jar directory and also the JAR file itself.
Here: https://stackoverflow.com/a/1732316/304151
I also had to modify the build configuration a bit. For automatic builds, it used the Java Builder to compile all the .java files into .class files and store it in the tmp directory. I used a custom Ant task to package all the .class files from the tmp directory into a JAR and store it in the jar directory. This is the file that Eclipse executes.
In your project, right click Properties, in "Java build path" menu you can set "default output directory" in the Source tab and make it point to jar directory:

Creating a .jar in Android

I have a Android project with some classes that I would like to compress to a JAR.
I create the JAR by right clicking my project - export - JAR - and the JAR gets created without any warnings.
I can access all classes from the JAR in my code but when I try to build the project get this exception :
java.lang.NoClassDefFoundError: name.of.class.in.package
Does anyone know what resources should be exported with the JAR? The options you get is
.class path
.prject
AndroidManifest.xml
proguard.cfg
project.properties
Right now I have included all of the above.
Making the JAR into a library project is not an option. And just to make it clear I don't have anything in the RES folder of my JAR project.
Open the project properties -> Java Build Path -> Order and Export and check item Android Dependencies are checked to be exported to the result APK.
And if .jar exist in Library tab under Android Dependencies item.
If you have .jar in the "libs" folder it should be included by default.
Just add your JAR to classpath or build your project with Maven and include dependencies
Try generating the jar in a different way. Basically jar is a zipped file of all class files with an extension of .jar.
Select all your class files that needs to be included in a jar file.
Right click and send to compressed (Zip File).
you will have a zipped file which will contain all the class files.
Rename this zipped file with having an extension .jar
Now include this jar file into your project whichever you want to use.
Or if you want to zip all the files by a different method then make sure all the files are zipped together without a folder. So when you unzip the files you should get all the files without contained in a folder.
It looks similar with this topic. Follow the very clear instructions written by Russ Bateman.

Difference between Libraries & lib folders ? How do we add jar files to lib folder?

When i created the dynamic web project, i will have 2 folders with name Libraries and lib. So during the project i added .jar files as follows
Build Path --> Configure Build Path--> Libraries (tab) --> Add external JAR's -- > OK
When i do this, it will add files to Libraries, However i would like to add .jar files to lib folder.
I tried copying all .jar files from Libraries to lib, but it says
Copied .jar files directly to lib folder in directory structure, but i doesn't display in eclipse.
I would appreciate your suggestions and inputs.
Copy jars into WEB-INF/lib directory by copying files themselves (not from Libraries in eclipse view) and hit refresh from eclipse, they will appear in the Web App Libraries
P.S: It is almost always better to use a build tool like maven/ant to manage dependencies
Open the lib folder in the file browser and paste all the copied
jars.
Refresh the lib folder in the

Adding External jar files in Eclipse for EAR Project

I needed to add external jar files to a EAR Project.
I did add but the jar files are not reflecting in the lib directory, it is showing below to lib directory.
I added these jar files on right clicking on Deployment Descriptor, Import-> Java EE Utility Jar and choosing to copy from the folder.
When I dragged the jar file on the lib directory the velocity jar file gets changed to .ear file in the eclipse project explorer.
I have attached a snapshot of the project explorer. Could you please let me know how to add external jar files to ear project as my EJB requires external jars to be included.
I have even added classpath for jar files in my EJB manifest file
Class-Path: ErpServices-ejb.jar velocity-1.6.4.jar velocity-1.6.4-dep.jar xwork-core-2.2.1.1.jar
Looking forward to your reply. thanks.
Add those jars under folder EarContent/lib directly , then all is done.
I.E., EarContent/lib/foo.jar will work but EarContent/lib/dir/foo.jar won't.
A similar question : here
Just try project root folder right click properties -> build path -> add external jars
Instead of drag-and-drop, add it to the lib folder manually (outside the IDE), refresh the IDE, right-click the EAR project and in modules and dependencies, make sure that it is included in the lib folder and as a dependency.

Categories