ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
Hi, I want to know something.
How can I some Java code I have to a JAR ? (I usually know it but allways become entangled with this, so I want an easy way to do it)
and second thing,
Lets say I have a simple program that is doing something, and I want it ti use a JAR, I mean in the JAR there is a main.class that I want my program to call it and just run it - simple is it gets.
How do I do it?
"How can I some Java code I have to a JAR ? (I usually know it but allways become entangled with this, so I want an easy way to do it)"
A jar file is pretty much similar to a tar file. A tar file, however, has no compression rate at all. It is possible, however, to make a jar file executable (and it is perhaps the most efficient way to use a jar). To create a executable jar, you've to create a file called manifest file. This is how you do that:
"Lets say I have a simple program that is doing something, and I want it ti use a JAR, I mean in the JAR there is a main.class that I want my program to call it and just run it"
Basically, yes. I use several .jar files together with my programs. For example, my weblog uses extern frameworks such as hibernate, which are basically a .jar file. From my program I can access all classes within that jar without a problem. I never create a jar myself to use with another program yet. Usually the jars I use are third part API's. If that is what you mean
I would highly recommend you to use an IDE such as Eclipse, Netbeans or even Bluej. They all have easy ways to handle several jars in a project and build executable jars without the need to create manifest files by hand...
1. This article might help answer some of your questions about "how the heck does Java find the right class (either from a .jar, and/or fromt he environment):
2. If you "public static void main (String[] args)" happens to be in .class file, you start your program like this:
Quote:
cd CORRECT DIRECTORY
java MyClass
If, on the other hand, your "main ()" happens to be in a .jar, then:
Quote:
java -cp JARFILE MyClass
3. You can have many different "static public void main()" entries, each in different public classes. But your program will only ever invoke *one* of them - it all depends on how you start the program with your java command line.
4. A second issue you brought up relates to "packages" and "qualified names". Please read this article, it might help answer some of your questions on this second issue:
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.