views:

260

answers:

9

So, I've been programming for a while now, but since I haven't worked on many larger, modular projects, I haven't come across this issue before.

I know what a .dll is in C++, and how they are used. But every time I've seen similar things in Java, they've always been packaged with source code. For instance, what would I do if I wanted to give a Java library to someone else, but not expose the source code? Instead of the source, I would just give a library as well as a Javadoc, or something along those lines, with the public methods/functions, to another programmer who could then implement them in their own Java code.

For instance, if I wanted to create a SAX parser that could be "borrowed" by another programmer, but (for some reason--can't think of one in this specific example lol) I don't want to expose my source. Maybe there's a login involved that I don't want exploited--I don't know.

But what would be the Java way of doing this? With C++, .dll files make it much easier, but I have never run into a Java equivalent so far. (I'm pretty new to Java, and a pretty new "real-world" programmer, in general as well)

A: 

You could publish a collection of compiled *.class files.

Dima
+4  A: 

Google JAR files.

Edit: Wikipedia sums it up nicely: http://en.wikipedia.org/wiki/JAR_%28file_format%29

Software developers generally use .jar files to distribute Java applications or libraries...

TreDubZedd
Thank you very much. I was aware full applications could be distributed in .jar files, but I did not realize they could act as libraries as well.
Matt D
+11  A: 

Java .jar library is the Java equivalent of .dll, and it also has "Jar hell", which is the Java version of "dll hell"

http://en.wikipedia.org/wiki/JAR_(file_format)

SHiNKiROU
Thanks for the info. Jar hell/Dll hell are not things I'd experienced, so I'll definitely take them into consideration in the future!
Matt D
+2  A: 

The Java analog to a DLL is the .jar file, which is a zip file containing a bunch of Java .class files and (perhaps) other resources. See Sun's, er, Oracle's documentation.

Jim Ferrans
A: 

The most common way to package up Java code is to use a ".jar" file. A .jar file is basically just a .zip file.

To distribute just your compiled code, you'll want to build a .jar that contains your .class files. If you want to additionally distribute the source code, you can include the .java files in a separate area of the .jar.

There are a lot of tools and tutorials out there that explain how to build a .jar.

Andy White
A: 

You don't have to distribute your source code. You can distribute compiled .class files, which contain human-unreadable bytecode. You can bundle them into .jar files, which are just zip files, and are roughly Java equivalent of native .dll files.

Note taht .class files can be easily decompiled (although decompilers cannot recover 100% of information from sources). To make decompilation more difficult, you can use obfuscator to make sources much less legible.

el.pescado
Bytecode is very human-readable, if you have a program that can read it... heck, _ASCII_ isn't human-readable unless you have a program that can read it!
Vuntic
Good point. ASCII readers are much more common than Java decompilers, though.
el.pescado
+3  A: 

A jar is just a uncompressed zip of your classes. All classes can be easily decompiled and viewed. If you really don't want to share your code, you might want to look at obfuscating your code.

wsxedc
A: 

Technically, you can compile Java bytecode down to native code and create a conventional DLL or shared library using an Ahead-Of-Time compiler.

However, that DLL would need the Java runtime specific to the AOT compiler, and two Java runtimes may not coexist in one process. Also, one would have to employ JNI to make any use of that DLL.

Unfortunately, obfuscation has too many weaknesses...

Dmitry Leskov
+1  A: 

Java's simple moto 'Write Once, Run anywhere'. create your all java classes as jar file but there are possibilities that still some one can see the Java code by using Decompilers. To prevent someone really looking at your code then Obfuscate the jar using the below link.

Java Obfuscation

Phani