I am learning Java these days and I've spent a lot of time with .NET so when I want to export or import libraries, they are usually in .dll format which is called assemblies in .NET environment and they are compiled to IL and they can have resources like images, XML, audio and so on, anyways.
I am wondering the same process in Java as well. I've read documents but they actually confused me a little bit and to clarify things out I need your help guys.
Questions:
.NET Assembly is the same thing as Java .jar?
.dll contains compiled IL code and .jar contains compiled .class/byte code files?
They say resources, what kind of resources we are talking about here? Images, .txt files, etc. or all of them possible?
I've examined AWS (Amazon Web Service API for java) and I saw three .jar file and they are
aws-java-sdk-1.1.1.jar
aws-java-sdk-1.1.1-javadoc.jar
aws-java-sdk-1.1.1-sources.jar
and they contain .class files - java documentation in html format and .java files which are still not compiled. So then I've realized .jar doesn't just include compiled byte codes (.class) and also other things.
When I want to import java libraries, will I always need to import .jar files?
When I want to export my own java libraries, should I need to export in .jar file.
Thanks for help in advance.
Is a .NET Assembly the same thing as a Java .jar?
They play the same role, yes.
.dll contains compiled IL code and .jar contains compiled .class/byte code files?
Yes. Although JARs are just zip files (you can open them in your favorite Zip tool), so they can really contain just about anything.
They say resources, what kind of resources we are talking about here? Images, .txt files, etc. or all of them are possible?
Any file type is allowed in a JAR. Your Java code can access the contents of files within the jar via, for example, getResourceAsStream or getResource. Obviously .class files in a JAR are treated specially (as bytecode).
I've examined AWS (Amazon Web Service API for Java) and I saw three .jar files and they are: aws-java-sdk-1.1.1.jar, aws-java-sdk-1.1.1-javadoc.jar, aws-java-sdk-1.1.1-sources.jar
The above packaging is fairly common. Sometimes people use .jar as an alternative file extension to .zip. I find this practice confusing, but it is not uncommon, and Amazon has done it in this case. I think this practice became more common with the launch of Maven, which stores reference source code in files named .jar.
aws-java-sdk-1.1.1.jar - This is the only file necessary for compilation and execution. It contains the classes (.class files) and resources necessary for the library to function.
aws-java-sdk-1.1.1-sources.jar - This is just a zip file that contains the source code (.java files) for the library. It is not necessary for compilation or execution. It is provided to help you troubleshoot problems you may encounter while using the library. You could unzip this file and read the source code from your hard drive. However, your IDE can probably utilize this file directly by creating a "source attachment" to the main library JAR. With this library->source association set up in your IDE, your IDE will be able to show you the source code of the library when you use your IDE's "Go to Definition" feature on a library class.
aws-java-sdk-1.1.1-javadoc.jar - This is just a zip file that contains the JavaDoc for the library. Again, it is not necessary for compilation or execution. JavaDoc is just HTML, so you could unzip this file and read the JavaDoc HTML directly from your hard drive. However, most IDEs can create a "Javadoc attachment" to the main library, which allows you to pull up context-sensitive JavaDoc for the library from within the IDE. Actually, most IDEs can generate the JavaDoc on the fly from the -sources.jar, so I rarely use Javadoc jars anymore.
When I want to import Java libraries, will I always need to import .jar files?
It's usually called "adding JARs to the classpath", but yes. You pretty much always need to help Java find all the JARs you're using by constructing a classpath whenever you build or execute. In general, using JARs and classpaths is a much more manual and explicit process than using DLLs in .NET. For example Java has no direct equivalent of .NET's "global assembly cache"; also vanilla Java will not automatically load .jars from the working directory.
There are a few other techniques for creating a more "automatic" feeling classpath, but they are kind of advanced: Manifest.MF classpaths are possible, for example -- but this approach is used infrequently as it is brittle. As of Java 6, Java has limited support for using wildcards in the classpath.
When I want to export my own Java libraries, should I need to export in .jar file
This is standard, yes.
I can answer part of that question :-)
As I understand it, a jar is just a "Java Archive" file. It can contain anything you want, but is most often used to distribute binary versions of libraries. It can also be used for distributing executables -- double-clicking a JAR file will launch the application, if that's how it's been packaged.
As such, it may be difficult to tell what a particular jar file is intended for. For example, what is aws-java-sdk-1.1.1-sources.jar?. Is it a self-installer? A compressed code archive? Without reading documentation, it's anybody's guess.
Edit: case in point, I just downloaded the JAI jar file assuming it was a binary build of the library. But in fact, it contains the installer application.
To answer this specific question.
aws-java-sdk-1.1.1.jar
aws-java-sdk-1.1.1-javadoc.jar
aws-java-sdk-1.1.1-sources.jar
The 1) is the compiler java archive file (jar), which in .NET is called a DLL. The 2) is the javadoc. An HTML documentation of java classes (found in 1)) along with method description. The 3) is the source code (java files) of 1).
When I want to import java libraries, will I always need to
import .jar files?
When I want to export my own java libraries, should I need to
export in .jar file.
For 1) If you need to use a class that is found in a jar file, then you'll need to import that jar into your project.
For 2) You don't need to. You can bundle your dependency jars into your library or package each into their own jars and import them into your project.
.NET Assembly is same thing as java .jar? Answer: Yes.
.dll contains compiled IL code and .jar contains complied .class/byte code files?
Answer: .jar files is an archived file of java resources, classes, images/icons/etc. that is necessary for an application, just like in .DLL. Class files contains compile java source codes (Called java bytecode).
Related
I developed a small project using C# and got everything working perfectly.
The only problem is that this app will be regularly sent via emails, therefore I'm trying to assemble all the files as one executable. The user will only have to open and see one executable, which will load the needed files and start the application.
The app does not use any external libraries. The only .dll file is for the IWshRuntimeLibrary.
The files that need to be assembled:
Note that I know that some files can be removed (svhost etc.). However this won't help because I'll still have more than one file. Also I've tried some free assemblers online but none of them worked.
In Solution Explorer, select the reference of Interop.IWshRunctime... and in the property window, change the value of Embed Interop Types to True.
This will not generate a separate dll for Interop Types.
PS: This is not a generic solution, but works with Interop Type only.
I've used https://github.com/Fody/Costura in the past. Nuget install Install-Package Costura.Fody (https://www.nuget.org/packages/Costura.Fody/)
Description about what it does from the source:
Take all assemblies (and pdbs) that have been marked as "Copy Local" and embed them as resources in the target assembly.
Actually you are looking for a method to merge some assemblies and resources into one executable file.
Custora.Fody, ILMerge, Obfuscators with assembly merging feature are some examples that can be used for this purpose.
You can find some answers here:
Embedding DLLs in a compiled executable
How to merge multiple assemblies into one?
List of obfuscators for .NET
If this is not about security reason, I would create the directory structure as follow:
- root
└ Startup.bat
└ lib
└ DiagnosticsSwitch.exe
└ DiagnosticsSwitch.exe.config
└ DiagnosticsSwitch.exe.manifest
└ Interop.IWshRuntimeLibrary.dll
And inside the Startup.bat
"lib/DiagnosticsSwitch.exe"
Is it possible to package a whole c# console application with it's applications folder/files into one executable .exe file so it dosen't need to be installed on the computer when it's going to run?
If it doesn't have a lot of resource DLLs (with translated strings, in separate subfolders) then you can use ILMerge to do this for the DLL files:
http://research.microsoft.com/en-us/people/mbarnett/ilmerge.aspx
ILMerge is a utility that can be used to merge multiple .NET
assemblies into a single assembly.
If you have other files too, you will have to add them as resources to your program and extract them at run-time.
See here for more details on how to embed resource files and extract them at run-time:
http://support.microsoft.com/kb/319292
Briefly: After you have added an embedded resource to your project, you do this:
var assembly = Assembly.GetExecutingAssembly();
var stream = assembly.GetManifestResourceStream("MyNamespace.MyResourceFileName");
// ... do something with stream, e.g. write it out to a file.
[EDIT]
You can actually do away with ILMerge altogether and have a much more robust solution.
See here for details: http://blog.nenoloje.com/2011/08/better-alternative-to-ilmerge.html
And here: http://blogs.msdn.com/b/microsoft_press/archive/2010/02/03/jeffrey-richter-excerpt-2-from-clr-via-c-third-edition.aspx
The author of ILMerge says of this "As the author of ILMerge, I think this is fantastic! If I had known about this, I never would have written ILMerge"
It's written by Jeffrey Richter, who I hope most people here will have heard of. :)
I am currently working on converting .JPEG file to .WSQ file, I have successfully done it in Java using a library from http://sourceforge.net/projects/jmrtd/.
Now I wanted to do the same thing in C#, I converted the .jar files into .dll files using IKVM and I can call the libraries of Java in my C# application. But there is a problem, in Java, I found out that the function to convert JPEG to WSQ is available in the external library which is added as an external SPI from the META-INF/service folder. Something like http://docs.oracle.com/javase/tutorial/sound/SPI-intro.html.
The external library is called "indirectly" in Java from what I understand, how can I do this in C#? Is this even possible?
If the "external library" a second jar file then compile both jar files in one step with the parenthesis syntax. Then it should work.
For details see the IKVM wiki:
Convert a jar file to a dll and use it as library
ClassLoader
I have been facing an error when I trigger XSLT from a C# code, It mentions something about dll file access failure every time, so just want to know whether this transformation code creates any dll files (in C:\Windows\temp directory) as such??
(I have mentioned the error and triggering program in my previous question),
If you are using compiled transforms, I believe a DLL will be created for the XSLT files.
May be it is mean that your system miss some DLLs necessary for XSLT transformation. To ensure use Microsoft's sysinternal utility (http://technet.microsoft.com/en-us/sysinternals/bb896642.aspx) -start it before your app and get error report what DLL can't be loaded.
Dll files are created only if there is an Inline scripting being used .. otherwise (for simple xsl files without inline scripts) it is not required ..
Practically tested and proved.
I am creating a C# class library for use by other developers. How should I package it up - just zip up the DLL and put it on the website?
The recommended way is to upload the code source including build scripts to a web site such as googlecode so that other developers could download it and compile it but if it is closed source library zipping the assembly (mylibrary.DLL), documentation (mylibrary.XML) and debug information file (mylibrary.PDB) should be enough.
If it is just a DLL then I wouldn't bother zipping it. However it would be nice if you did zip up some documentation with it along with a sample configuration file if applicable.
Well, have you ever used another third party one? What would you like to see in the perfectly packaged third party download?
I'd zip it up with the following in at a minimum:
your dll in a /bin/ folder
readme (txt or html) explaining what it is, how to install and where to get more info
if you are including the source then
* source in /source/ folder
* Any tests in a /tests/ folder
If its something good you might want to stick it up on google code or codeplex rather than your own site?
Yes.
You should include documentation in your ZIP file, including a link back to your website. NDoc is great for generating documentation from your XML comments.
Look at 3rd party prijects for inspiration, but yes, a zipped dll is fine.
If your documentation is large then you should put it into a separate zip, and if your releasing the source you should put that into a 3rd zip.
Either way your dll should comes with a readme describing what version it is, what the purpose is, who wrote it and how to get in touch with them, along with any dependencies or other useful snippets of information.
Documentation is really important!
I would suggest that, if this is a product, you would want a setup project that installs it. This would include placing it in the GAC and leaving documentation somewhere.
edit
Just to clarify, I mean real documentation, not just what's automatically generated from comments.