Running my java project from a Jar - java

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:

Related

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

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.

Eclipse Java project: can I set eclipse to store .class and .java files in 1 folder?

I just need a simple environment of .class and .java files in a single folder so that I can execute java files later on using "java xx" in command line without adding any extra syntax. I'm not planning to use packages or sub-directories in my project and I won't be executing any files directly from eclipse either.
You could create a executable jar file and run it from command line using:
java -jar MyApplication.jar
Here is the link to create an executable jar file from eclipse:
How to create an Executable jar file
I think you are looking for this:
Create new project->Java Project->In the Project Layout section change to 'User project folder as root for sources and class files' (using Eclipse Kepler, it should be similar in other versions of Eclipse).
But keep in mind that it might become a mess after some time.

How to create Jar file with external folders and Jars

I made a simple standard-lone java Application using Spring,Apache Camel,Activemq for processing messages.
Note: My Application don't have any GUI.
My project structure is in the following way.
SACLib folder have nearly 70 external jars(all Spring,Camel and Activemq corresponding jars).
It's working fine in Eclipse. SO Now We want to deploy into Jar file.I tried in Eclipse,But I didn't seen Rod1,Rod2,Copy1 and SACLib folders in my Jarfile.
after Deploying Jar, If I run FirstConsumer.java it runs Rod1-->ThMapInfratab1-2.exe file. For this I mention Real paths of .exe file.
How can I make Jar file with including all my folders.
Thanks
Well, this is a kind of work that is typically done with build automation tools like Apache Ant, Maven or Gradle, so you can investigate there if you want to make this happen automatically next time.
But, if you want to do it manually...
First, you project needs a META-INF folder where you will place a file called a MANIFEST.
That manifest contains a Main-Class entry pointing to you main class. You can read about this in the Java Tutorial: Setting Application's Entry Point.
But it can also contain a Class-Path entry, pointing to all other jars required by your application and that should be loaded by the executable jar.
You can read about it the Java Tutorial: Adding Classes to your Jar Class Path.
If you are building your executable jar with Eclipse, it will let you choose the MANIFEST file that you want to use during the creation process.
Now, if you want to use build automation tools, there are other answers here that explain how to do it:
Creating a bundle jar with ant
How to create executable jar with dependencies with Maven
How to export an executable jar in Gradle
simply using ant download it , and then make a build.xml file and put it
Here's an simple example of an ant target that will create a jar (named test.jar) that includes all jar files under the lib directory. Maybe this will solve your problem?
for using apache ant, see this
http://ant.apache.org/manual/using.html

How to make jar file created by netbeans work from any location in the system

I have a java desktop application in netbeans. I have created an executable jar file for the project using clean and build command provided by the netbeans. By using this command the executable jar file gets created under netbeansProjects//dist/.jar. I am able to execute this jar file from command line using java -jar .jar from within project path. But the problem is that when i move this jar outside of netbeans projects folder, say to desktop and run the jar file, it is giving error of type "Exception in thread "main" java.lang.NoClassDefFoundError". How to solve this problem and make the jar file executable from any location of the system.
Complete instructions may be found in dist/README.TXT:
To distribute this project, zip up the dist folder (including the lib folder)
and distribute the ZIP file.
Ensure that the manifest inside of the jar file contains the necessary classpaths. If you are unfamiliar with the concept, go here: http://docs.oracle.com/javase/tutorial/deployment/jar/downman.html
Netbeans has probably included any external projects/libraries/Jars in the dist/lib folder.
In order to run the application, you must include all the files in the dist folder when you copy the application
Check if in your projects Manifest.mf file has the Attribute
"Main-Class" set to your projects current main Class file.

Java export .properties file to build folder?

I have just created a .properties file in Java and got it to work. The problem is where/how to store it. I'm currently developing a "Dynamic Web project" in Eclipse, and have stored the properties file under build/classes/myfile.properties, and I'm using this code to load it:
properties.load(this.getClass().getResourceAsStream("/myfile.properties"));
But won't this folder get truncated when building the project, or not included when exporting as a WAR file? How can I add this file to the build path, so it will be added to /build/classes on every export (in eclipse)?
If you add your .properties file into the source folder, it will get copied into the classes folder at build time. I usually create a separate "Source Folder" in my projects to hold .properties files and other non-Java source files.
See my question on copying property files using an ant task. Eclipse will do this automatically, as #highlycaffeinated has suggested, but you have to make sure your list-of-files is up-to-date (refresh files on your project before you debug/run/deploy).
I use ant for more formal control over this.
Putting it anywhere in classpath will do. Just make sure it is included in WEB-INF/classes after deployment/build.
In maven projects the standard place for property files is /src/main/resources. You can put it there ant add this folder as eclipse "source folder".
You can put the properties into the "Web App Libraries/classes" folder, which corresponds to "src/main/webapp/WEB-INF/class" folder on the disk (eclipse automatically creates the shorthand version above):

Categories