LinuxQuestions.org
View the Most Wanted LQ Wiki articles.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 12-24-2006, 02:47 AM   #1
EAD
Member
 
Registered: Mar 2006
Distribution: ARCH linux
Posts: 137

Rep: Reputation: 15
Question Java and Jars ?


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?
 
Old 12-24-2006, 02:56 AM   #2
Mega Man X
Guru
 
Registered: Apr 2003
Location: ~
Distribution: Ubuntu, FreeBSD, Solaris, DSL
Posts: 5,339

Rep: Reputation: 63
"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:

http://csdl.ics.hawaii.edu/~johnson/...jar-files.html

"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...

Regards!

More links:
JAR (file format) >> http://en.wikipedia.org/wiki/JAR_%28file_format%29

Last edited by Mega Man X; 12-24-2006 at 03:01 AM.
 
Old 12-25-2006, 01:17 AM   #3
EAD
Member
 
Registered: Mar 2006
Distribution: ARCH linux
Posts: 137

Original Poster
Rep: Reputation: 15
Ok, but lets say that someone give me a JAR file, and he say it has a MAIN.class I shuld run, How do I do it from within my program?
 
Old 12-25-2006, 04:53 AM   #4
jlliagre
Moderator
 
Registered: Feb 2004
Location: Outside Paris
Distribution: Solaris10, Solaris 11, Mint, OL
Posts: 9,506

Rep: Reputation: 360Reputation: 360Reputation: 360Reputation: 360
To run the jar, use "java -jar yourFriendsApp.jar".

To call the main method from within your program:
Code:
somepackage.someClass.main(args);
 
Old 12-26-2006, 12:58 PM   #5
EAD
Member
 
Registered: Mar 2006
Distribution: ARCH linux
Posts: 137

Original Poster
Rep: Reputation: 15
Ok tanx, Do I need to put it in my import files or something??
 
Old 12-31-2006, 02:28 AM   #6
EAD
Member
 
Registered: Mar 2006
Distribution: ARCH linux
Posts: 137

Original Poster
Rep: Reputation: 15
any one?
 
Old 12-31-2006, 02:10 PM   #7
paulsm4
Guru
 
Registered: Mar 2004
Distribution: SusE 8.2
Posts: 5,863
Blog Entries: 1

Rep: Reputation: Disabled
Hi -

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):

class loader basics

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:

Package Basics

5. Finally, there's a special-purpose "JarClassLoader":

JarClassLoader Basics

I *don't* think JarClassLoader is what you want - but I thought I'd mention it anyway.

'Hope that helps .. PSM

Last edited by paulsm4; 12-31-2006 at 02:15 PM.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Java plugin installed correctly for Firefox but not able to view any java applet tvn Linux - Software 10 04-15-2010 02:13 AM
LXer: Java news met with cautious optimism in free Java community LXer Syndicated Linux News 0 11-14-2006 10:21 PM
Firefox refuses to load Java jnlp files - plugin and java ok Melsync Linux - Software 1 06-25-2006 04:09 PM
Synaptic Package Manager shows 'java-common' and 'java-gcj-compat' as Installed. swiadek Ubuntu 3 02-12-2006 11:54 AM


All times are GMT -5. The time now is 05:09 AM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration