Eclipse: Create jar containing one class + dependencies - java

I have an eclipse project that contains several classes and packages. I want to create a runnable jar of just one class from the project. But this jar should contain all the dependencies too. Can someone please tell me how can I do this? Most posts on StackOverflow have answers for creating a jar file for the entire project, but I only need jar of one class with all dependencies bundled together.

I could finally figure out how to do it. Just right click on your class file for which you'd like to create a jar. Select Export --> Jar File. Click on next. In select resources to export, select the project name. Expand the project to find the package that contains the class you'd like to export as a jar. Select the class name. Check the box --> Export generated class files and resources. Select the export destination, and check the option --> Compress the contents of the JAR file. Click on next. Under select options for handling problems, leave both the options checked. You don't need to check the option - Save the description of this JAR. Click on next. Here, leave the default options. Under select the class of the application entry point, specify your class (Make sure your class has a main method). And finally click on finish. Once this is done, go to the path where you've saved the jar. Right click, and say open with --> Archive utility. This will create a folder of your jar file. In my case, I named the folder lib. Inside this folder, copy all the JARs that this class needs to execute.
Finally, do this from the folder where you have the folder of your jar file --
myMac$ JAVA_HOME=`/usr/libexec/java_home`
myMac$ libraries=`ls lib/*.jar | tr "\n" ":"`
myMac$ $JAVA_HOME/bin/java -cp $libraries com.test.rick.GeoData
GeoData here is the class which I was trying to export as a JAR. Hope this is useful for someone in the future.

Related

Hermes-JMS Unable to display message

I am user Hermes-JMS to try to display messages on a Java Message Queue.
When I select a message error that it is "Unable to display message" the reason is a Class Cast error.
Is there way to point Hermes to my objects (set the class path for Hermes) so it can use the toString method to display the messages that it sees on the Queue.
I found the solution. First go to your IDE and export to JAR (in RAD or eclipse this is simply right click export) and include all your projects. This should create a jar file with all your class files. Even if you are doing a war or ear file, just export to jar file so Hermes can read it.
Then in Hermes click on options -> configuration select the Providers tab expand the class path groups that already exist (important don't create a new group this was where I was making my mistake every time I tried, just use the existing one)
Right click on "Library" and select add jar file, select the jar file you create above, select ok and scan, you might have to do the add jar again for each jar you added to your project that your classes depend on.

Beginner issues with Eclipse and running an imported .jar file?

I've never used Eclipse before am trying to import a .jar file and then run it without any command-line arguments.
I think the .jar file has been imported correctly, it is sitting in a created lib folder and I can see it in referenced libraries.
The .jar will not run because of the error "selection does not contain a main type".
I can see there is a main class inside the .jar when I extract it, but the manifest file does not include a 'main-class' specification. Because this is an assignment though, I cannot alter the .jar directory. Is there a way that I can run the file within Eclipse regardless of this?
Within the jar file all classes are part of a package. Would this alter the way I could try and run it?
To ensure, you imported the JAR correctly: In package explorer, right click your project, choose Properties, Java Build Path and tab Libraries. If your library doesn't appear, click button Add JARs or Add External JARs and choose your JAR file.
Then right click your project and choose Run As / Run Configurations.
In the dialog window click button New launch configuration. Then on the right side under Main class, input the full qualified class name of the main class which is contained in your JAR file. E.g. com.foo.Bar for class Bar in package com.foo. You can use the button Search to search for a main class.
Then click Apply and Run.
Write a class that will call the main class inside the jar.
Or simply ask your instructor.

eclipse doesn't see class file

I have eclipse project.
I add directory src/directory and put there SomeClass.java
I compile my application.
in somedirectory in filesystem I see .class file.
But in eclipse somedirectory looks empty.
What the reason? How to fix it?
I press F5 many times!
eclipse hides class files when you are in the package explorer. Try to switch to the navigator (Ctrl+3 and type "navigator") then a "bin" folder should appear and that one contains your class file.
First, the Eclipse src folder and output folder that will contain the compiled classes are two different folders. These two folders' location are configured in the Project's properties page, under the Java Build Path tab. More information on the configuration can be found here.
So when you press in F5 in Eclipse to refresh the folder, you are basically refreshing the source folder, and you will never see the .class file in there. The output folder that contains the .class files are hidden by the Project Explorer view.
If you absolutely want to see these files via Eclipse, switch to the Resource perspective and follow the instructions as given by the answer of this SO entry or rather follow instructions given by #markusw , CTRL-3 + Navigator view is way quicker!
The hierarchy of the source directory src, should be the same as the package name of the classes inside it. In other words, if you have a class whose package name is com.example.util, then that class should be located inside the directory src/com/example/util.

How do I make a JAR file for my homework assignment?

My instructor asked us to do a homework assignment in Eclipse and save it as a JAR file so he can open it in his own Eclipse... Previously, I've always zipped my .class files and sent them off, so I don't quite know how to do this...
I did find that I can "export" to a jar file, but when I try to open it again I can't figure out how... so I don't know if I exported it properly or if there's just some unknown method he uses to load it into Eclipse afterward.
Yes, "export" to JAR file is the way to go. To check what it contains, please understand that a jar file is in fact a zip file with a different extension, and so you can make a copy of the jar file, change its file extension to .zip then unzip it, and you'll see if it contains what you need.
Edit
I suggest that you use Export to JAR (and not Export to Runnable Jar) since this has the option of exporting the source files as well.
If you do this, you will see:
and you will wish to make the selection Export to JAR as noted in the figure.
Next you will see:
Which is where you choose the files that should be part of the JAR. Make sure to click "Export Java source files and resources" so that your jar file has text readable source. When done, don't click Finish, but instead click Next >.
Then you will see
The Jar Manifest Specification, where you choose which file that holds the main method, that should be run, by entering it into the Main class: text field, or by finding it using the browse button next to the text field.
Then once you've selected the main file, click Finish:
And you're done. Be sure that in the Jar File Specificaion you choose to export the source files.
In Eclipse you can right click on the project and choose "export as jar"
You could also see the documentation.
In Eclipse, right click on your project in Project Explorer. Then go to export and select JAR file. From here you select which files you want to include and a destination for the JAR file. Once complete, you can attach this to an email and send to your Professor.
Hope I could help.

How do I include .class files in my project in Eclipse? (Java)

Hey all. I am working on a project for school where we are given the .class file but not the source to include in our code. I am using Eclipse, and I want to include the file in my project so I can instantiate objects from it and use it. The file is TokenizerImpl.class, and I want to use it like this: TokenizerImpl tokenizer = new TokenizerImpl(foo);
I put the file in my project folder, and Eclipse says that "TokenizeImpl cannot be resolved as a type", which I assume means it cannot find the class or source. I tried putting it in the "bin" folder of the project and got the same error. Google search and SO search didn't seem to answer this, so I will give it a shot. How do I do this, oh wise ones?
Edit: Oh dear, I found the problem was something else entirely. These solutions worked fine, but I just forgot to create the Tokenizer interface that TokenizerImpl implements. Doh. Thanks for all your help though, I did learn a lot about eclipse.
You can add a directory containing the class files to the Eclipse project, only if it is inside one of your Eclipse projects, either in a generated directory or in one you have created.
This can be done by adding the class folder to the Java build path of the application. You can set this in the Project properties, by visiting Java Build Path -> Libraries -> Add Class Folder. Keep in mind, that you will have to specify the root folder containing the class files in their packages.
Therefore, if you wish to have the compiler access com.stackoverflow.Example.class present in the classes directory under project A (but not in the build path of project A), then you should add 'classes' as a class folder, and not classes/com/stackoverflow as a class folder.
Project -> Properties -> Java Build Path -> Libraries -> Add External Class Folder
The folder must contain a package hierarchy, i.e. if your class is really foo.bar.TokenizerImpl it must be in the subdirectory foo/bar.
You could also JAR the class files that you want to add and add the JAR file to the build dependencies. To me this is the cleanest solution. Internally the JAR file has to have the correct directory structure, of course.
Right click into your project and select Java Build Path to add new dependencies.
Copying it into the bin folder won't work very well because it is meant to hold the result of compiled sources. As soon as you clean anything the file will be gone.
or put everything into a jar file and add this as an external jar.
Other people have now given better answers. This "answer" was mainly to get information from the OP because the original question didn't really tell us, fully, what had been tried. There are now two answers that truly answer the question in a long-term way.
My original answer is left below for context.
Did you copy it to the bin folder within Eclipse or outside Eclipse? If you did this outside Eclipse then you have to right click on the "bin" folder and select "refresh" for Eclipse to see the new file.
A *.class file in the appropriate folder (depending on its package) under the bin directory should do it.
zip the class folder.
Highlight project name, click "Project" in the top toolbar, click "Properties", click "Libraries" tab, click "Add External jars", add the zipped file

Categories

Resources