Use Java .class files from C# or .NET?

  • Thread starter Thread starter Mick
  • Start date Start date
M

Mick

I wrote a C# program that interfaces with a data vendor over the web
using an API they supplied and their examples in C#.

Now I have another data vendor's API and example that I want to add to
my C# program. But this new API is written in Java. They gave me an
example source code that uses their API but the actual API looks like it
is stored in a few .jar files. I unzipped those .jar files into many
.class files. I think these .class files are kind of like Java's
version of compiled .dll files or library files, right? Anyway they are
not source code.

I can convert the example into C# using the "Conversion Assistant" in
.NET. But then it can't access any of the libraries in the actual API.

I don't know anything about Java except for what I have tried to glean
from this newsgroup. I would like to convert the Java API to C# but it
looks like that is not possible, right? Or I would like to access those
.class files from C#, but again, I guess this is not possible. I read
somewhere that those .class files can be converted to COM files and then
access from C#, is this possible?

I downloaded j2sdk-1_4_2-nb-3_5_1-bin-windows.exe from Sun's website. I
guess I could convert my C# program to Java but then I probably won't be
able to access the first vendor's API that is using .NET their .dll.

Sorry if I'm not making much sense here, I'm so confused. Does anyone
have any suggestions?

Thank you,
Mick
 
Mick said:
I wrote a C# program that interfaces with a data vendor over the web
using an API they supplied and their examples in C#.

Now I have another data vendor's API and example that I want to add to
my C# program. But this new API is written in Java. They gave me an
example source code that uses their API but the actual API looks like it
is stored in a few .jar files. I unzipped those .jar files into many
class files. I think these .class files are kind of like Java's
version of compiled .dll files or library files, right? Anyway they are
not source code.


Hi Mick,

Just an idea - I'm wondering if you could write a wrapper for the API in J#
and then talk to the J# wrapper with C#.

Joe
 
You cannot directly do anything with Java class files as far as I know from
C#. But what you could do is look into compiling the Java source code with
J#. This will at least generate IL and then you would at least be able to
use other .NET languages with it like C#. I don't know if J# has anyway to
extract from the jar file or not.
 
IIRC, the jbimp.exe tool from the J# SDK can convert Java 1.1 .class files
to IL, I believe. However Java 1.1 was so long ago that you essentially
have to re-write your Java specifically to compile to 1.1 just to get it to
work, and then you might as well have ported it to J#.

Richard

--
C#, .NET and Complex Adaptive Systems:
http://blogs.geekdojo.net/Richard
Sam Gentile said:
You cannot directly do anything with Java class files as far as I know from
C#. But what you could do is look into compiling the Java source code with
J#. This will at least generate IL and then you would at least be able to
use other .NET languages with it like C#. I don't know if J# has anyway to
extract from the jar file or not.


--
Sam Gentile [C#/.NET MVP]
.NET Blog http://samgentile.com/blog/


Mick said:
I wrote a C# program that interfaces with a data vendor over the web
using an API they supplied and their examples in C#.

Now I have another data vendor's API and example that I want to add to
my C# program. But this new API is written in Java. They gave me an
example source code that uses their API but the actual API looks like it
is stored in a few .jar files. I unzipped those .jar files into many
class files. I think these .class files are kind of like Java's
version of compiled .dll files or library files, right? Anyway they are
not source code.

I can convert the example into C# using the "Conversion Assistant" in
NET. But then it can't access any of the libraries in the actual API.

I don't know anything about Java except for what I have tried to glean
from this newsgroup. I would like to convert the Java API to C# but it
looks like that is not possible, right? Or I would like to access those
class files from C#, but again, I guess this is not possible. I read
somewhere that those .class files can be converted to COM files and then
access from C#, is this possible?

I downloaded j2sdk-1_4_2-nb-3_5_1-bin-windows.exe from Sun's website. I
guess I could convert my C# program to Java but then I probably won't be
able to access the first vendor's API that is using .NET their .dll.

Sorry if I'm not making much sense here, I'm so confused. Does anyone
have any suggestions?

Thank you,
Mick
 
inline:

Mick said:
I wrote a C# program that interfaces with a data vendor over the web
using an API they supplied and their examples in C#.

Now I have another data vendor's API and example that I want to add to
my C# program. But this new API is written in Java. They gave me an
example source code that uses their API but the actual API looks like it
is stored in a few .jar files. I unzipped those .jar files into many
class files. I think these .class files are kind of like Java's
version of compiled .dll files or library files, right? Anyway they are
not source code.

Correct, although you can easily decompile them. Check the web for tools.
There are quite a few that turn java byte code back into java syntax.
I can convert the example into C# using the "Conversion Assistant" in
NET. But then it can't access any of the libraries in the actual API.

I don't know anything about Java except for what I have tried to glean
from this newsgroup. I would like to convert the Java API to C# but it
looks like that is not possible, right? Or I would like to access those
class files from C#, but again, I guess this is not possible. I read
somewhere that those .class files can be converted to COM files and then
access from C#, is this possible?

Only by using J++ or a Java-to-COM bridge as far as I know. I don't
recommend it. You'll be going through too many wrappers.
If you can retrieve the source code from the class files by decompiling
them, and then tweaking them into C#, i'd go that route.
I downloaded j2sdk-1_4_2-nb-3_5_1-bin-windows.exe from Sun's website. I
guess I could convert my C# program to Java but then I probably won't be
able to access the first vendor's API that is using .NET their .dll.

Sorry if I'm not making much sense here, I'm so confused. Does anyone
have any suggestions?

Try using this Java-to-.NET bridge:
http://www.jnbridge.com/index1.htm

I haven't personally used it, so I can't attest to its suitability for your
needs, but it's a start.

-Rob Teixeira [MVP]
 
Yes, the .class files are the output of the java compiler. The java runtime
loads classes either from individual .class file or from .jar files which
are nothing more than zip files containing .class files (you can open a .jar
file with standard zip tools).

J# includes a tool to convert .class or .jar files into .NET assemblies. The
tool is called JBIMP and you should be able to find its documentation in the
Studio 2003 documentation.

But there is no guarantee that JBIMP will solve your problem because J# is
based on an old version of the Java framework (JDK 1.1.4 while most Java
developers use JDK 1.4 these days). So, if your .class files only use JDK
1.1.4 APIs, JBIMP will do the job, but if they use newer APIs, JBIMP won't
be able to convert them. Then, you will need to rework the Java code so that
it only uses JDK 1.1.4 APIs (you 'll need the source) or investigate other
options (like communicating with a separate process that runs your java
code).

Bruno.
 
Bruno,

Thank you, that was informative. I did try JBIMP but it had so many
Unresolved classes that it failed. So I guess this means it was
compiled using a newer JDK, right? And since I don't have the source
code for the .classes then I can't convert, unless I decompile them.

Thank you,
Mick
 
Rob,

Ok I will look for a decompiler.

I looked at www.jnbridge.com, it looks like a wrapper instead of a
decompiler, but I haven't tried it yet, plus I can't afford it right
now.

Do you know any good/cheap decompilers?

Thank you,
Mick

P.S. Thank you to everyone that replied, it was very helpful.
 
Unfortunately the only source code I have is just an example that uses
the .class files. I don't have the .class files' source code. I have
tried to compile the example into J# but then it can't find most of the
import stuff. I think this is because all the .classes are still in
Java and it can't find them.
 
JNBridge isn't a decompiler, it was an alternative to decompilation,
allowing you to use the .class java APIs from .NET in a last ditch effort to
do something.

The most famous Java decompiler is probably Mocha. Do a search for that, you
can't miss it.
Just be very careful about licensing and legal terms with regards to the
software you trying to get source code for.

-Rob Teixeira [MVP]
 
Rob,

JNBridge: Ok, I get it now.

Decompilers: Yes you were right I found several.

Thank you very much for all your advice! Thanks to you and the other
repliers I think I can figure this thing out.

-Mick
 
Back
Top