Running an eclipse created jar file - eclipse

I have created a jar file in eclipse and now I want to execute the jar file in command prompt.
How can I do that?

When your creating jar file using eclipse
While reaching JAR manifest specification
enter main class

Run this command from command prompt:
java -jar <JarName>.jar
Note: you will need to jar a file called META-INF/MANIFEST.MF in order to run the jar after compilation.
If you are creating the jar using Eclipse do this:
Go to file > Export > Java > Jar file (hit next)
Select your project and all the files you would like to jar
Type below the destination of the jar (e.g. c:\MyJar.jar) (hit next x2)
On the bottom Main class either hit browse and choose the class which contains main or type: package_name.class_name (wihch contains the main entry point)
Hit Finish
Go to the jar location in command prompt and run as i mentioned above

Related

How to execute Testng project without main method from command line

I have a TestNG project where I use testng.xml file to execute.
How to take a jar of my project and execute it on another machine.
I know how to execute it on the same machine by giving path of bin & lib folders i.e
cd C:\Workspace\projectname java -cp
C:\Workspace\projectname\lib*;C:\Workspace\projectname\bin
org.testng.TestNG testng.xml.
But if i want to do on another machine how to do this?
You can create a bash file which points to your testng.xml file.
Use this:
Bash file is running fine in windows for testng but it is not working in linux/mac
Steps:-
1. Create a notepad file
2. Copy -> java -cp ./src/lib/*:./bin org.testng.TestNG testng.xml(use ; instead of : if you are using windows) (./src/lib/* -> All your jars files must present in this location, ./bin -> all your class files must be present here)
3. save the file.
4. Rename the file as something.bat
5. Double click on file if you are using windows or use **bash filename** if using linux or mac
Note: - Java, require browsers must be present in other computer and it's a prerequisite which is nothing to do with your automation script run. they must be pre-installed

add jar files to classpath to eclipse

in this link http://mina.apache.org/mina-project/userguide/ch1-getting-started/first-steps.html says "Put the following jars in the classpath"
what does that mean? how can I do that in eclipse? without eclipse?
Here's how you could do it in eclipse:
Right click on your project
Select Build path -> Configure Build Path
Select libraries tab
Click on add external jar and select the jar file by browsing your file system and click on open.
click on ok on your configure build path screen.
Without eclipse, when you are about to compile, you will need to use:
javac -classpath <all jar files class path seperated by ; in windows or : on linux> java source file
And similar option while running the class via java command.

Creating jar executable w/ external jar (JXL) in ECLIPSE java

I can't find a working solution for my jar executable. The program runs perfect in my Eclipse IDE, but when I attempt to follow Eclipse instructions, the executable does not work with external JAR.
The JXL jar is in my build path. Anyone know how to get this working through Eclipse? I do not have access to command line.
1.) Right-click on project, select pop-up menu entry "Export...".
2.) Select "Java -> Runnable JAR file".
3.) Choose a working launch configuration for running your program via java -jar MyExecutable.jar from the command line later. Select an output folder and file name. Select "Package required libraries into generated JAR".

Eclipse jar file is not exported with project

I have a simple project that I need to be very easily imported into eclipse and started. When I export my project and import it again it comes with an error saying it is missing a required jar file. This jar file is added to the build path, but does not carry over when the project is exported. Why is this? I have been searching all day for an answer on Google and none have helped.
Would I just have to send the jar file along with the project and have the users manually add it?
Things I have tried:
Going into build path order and export and clicking the jar file.
using a clean workspace for the import / restarting eclipse.
For anyone with a future problem like this one. I was putting the jar in the lib folder and also adding it to my build path, but the jar would not export over. When I added the jar to the WEB-INF / lib folder it successfully carried over with the export.
You may want to try this:
Right click on the imported project select Build Path -> Configure Build Path -> Java build Path -> Libraries Tab and check if the jar file which eclipse says missing is present in the Libraries tab.
If it is present then select it and click Edit, a "File dialog" will pop up, find the desired jar which is in your computer's directory. and click Ok then wait for eclipse to build
the workspace. If it is not present then add it through Add JARs... or Add External JARs...
Export it as an Archive file of the project rather than a jar. Then it's just a matter of someone else importing it as an Existing Project. Exporting it as a jar is only something you do when you want to deploy the jar and run it.

How to create a Jar file in Netbeans

Well I have my source code that i have done using the IDE netbeans. Now I wanted to move this java application to a web application. For that I need to create a jar file from my source code, so that I could invoke it in ma jsp file.
I have not been able to find any option in netbeans or any other way to create a .jar file of this source code.
Could someone tell me how to do that.
Thanks
Create a Java archive (.jar) file using NetBeans as follows:
Right-click on the Project name
Select Properties
Click Packaging
Check Build JAR after Compiling
Check Compress JAR File
Click OK to accept changes
Right-click on a Project name
Select Build or Clean and Build
Clean and Build will first delete build artifacts (such as .class files), whereas Build will retain any existing .class files, creating new versions necessary. To elucidate, imagine a project with two classes, A and B.
When built the first time, the IDE creates A.class and B.class. Now you delete B.java but don't clear out B.class. Executing Build should leave B.class in the build directory, and bundle it into the JAR. Selecting Clean and Build will delete B.class. Since B.java was deleted, no longer will B.class be bundled.
The JAR file is built. To view it inside NetBeans:
Click the Files tab
Expand Project name >> dist
Ensure files aren't being excluded when building the JAR file.
Please do right click on the project and go to properties.
Then go to Build and Packaging.
You can see the JAR file location that is produced by defualt setting of netbean in the dist directory.
I also tried to make an executable jar file that I could run with the following command:
java -jar <jarfile>
After some searching I found the following link:
Packaging and Deploying Desktop Java Applications
I set the project's main class:
Right-click the project's node and choose Properties
Select the Run panel and enter the main class in the Main Class field
Click OK to close the Project Properties dialog box
Clean and build project
Then in the fodler dist the newly created jar should be executable with the command I mentioned above.
Now (2020) NetBeans 11 does it automatically with the "Build" command (right click on the project's name and choose "Build")