Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Calling java from C#

Calling java from C#

Scheduled Pinned Locked Moved C#
csharpjavatutorialquestion
13 Posts 8 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • K KASR1

    How to call java within c#?. It would be helpful if we know the steps to use java code in C#?

    A Offline
    A Offline
    Abhinav S
    wrote on last edited by
    #4

    Three approaches discussed here[^] - all worth considering.

    1 Reply Last reply
    0
    • D David Skelly

      If you are talking about directly using Java code from within a C# application, you might want to look at JNBridge. http://www.jnbridge.com/[^] I've never used it and I have no idea how much it costs, but it has a good reputation. It seems to do the job pretty well from what I can gather. If you are talking about reusing functionality but don't need direct integration then it might be easier (and cheaper) to write a wrapper that lets you talk between the two components, e.g. a web service or a call through a TCP socket or something like that. It depends what your requirements are.

      K Offline
      K Offline
      KASR1
      wrote on last edited by
      #5

      The requirement is here. There are two methods available in java. Zip(string sourcefolder, string targetloction, string zipfilename); UnZip(string zipfilename, string targetfolder); We need call these methods from C#. Is there any ways to achieve whitout using JNBridge /3rd party?

      D B 2 Replies Last reply
      0
      • K KASR1

        The requirement is here. There are two methods available in java. Zip(string sourcefolder, string targetloction, string zipfilename); UnZip(string zipfilename, string targetfolder); We need call these methods from C#. Is there any ways to achieve whitout using JNBridge /3rd party?

        D Offline
        D Offline
        David Skelly
        wrote on last edited by
        #6

        You have a number of options: - use third-party tool like JNBridge or IKVM or similar - use a web service or other cross-process communication - write your own wrapper DLL using JNI I have no idea what your Zip/Unzip methods do, but one option (which I am sure you have considered) is to bite the bullet and rewrite them in C#. If the Java functionality isn't too complicated, this would be my preferred option if it was my project. Don't forget, you will need a JVM up and running in order to call the Java methods. They will not run natively in .NET.

        K 1 Reply Last reply
        0
        • D David Skelly

          You have a number of options: - use third-party tool like JNBridge or IKVM or similar - use a web service or other cross-process communication - write your own wrapper DLL using JNI I have no idea what your Zip/Unzip methods do, but one option (which I am sure you have considered) is to bite the bullet and rewrite them in C#. If the Java functionality isn't too complicated, this would be my preferred option if it was my project. Don't forget, you will need a JVM up and running in order to call the Java methods. They will not run natively in .NET.

          K Offline
          K Offline
          KASR1
          wrote on last edited by
          #7

          Thanks for your suggessions. I will try to implement wrapper DLL using JNI.

          1 Reply Last reply
          0
          • K KASR1

            The requirement is here. There are two methods available in java. Zip(string sourcefolder, string targetloction, string zipfilename); UnZip(string zipfilename, string targetfolder); We need call these methods from C#. Is there any ways to achieve whitout using JNBridge /3rd party?

            B Offline
            B Offline
            Bernhard Hiller
            wrote on last edited by
            #8

            Why don't you try some zip libraries for .NET? E.g. ICSharpCode.SharpZipLib.dll. I'd rather look for .NET alternatives than try to deal with the problems of running java code from C#. Another alternative would be to write a java "executable" which you can call with System.Diagnostics.Process.Start()

            K 1 Reply Last reply
            0
            • B Bernhard Hiller

              Why don't you try some zip libraries for .NET? E.g. ICSharpCode.SharpZipLib.dll. I'd rather look for .NET alternatives than try to deal with the problems of running java code from C#. Another alternative would be to write a java "executable" which you can call with System.Diagnostics.Process.Start()

              K Offline
              K Offline
              KASR1
              wrote on last edited by
              #9

              Trying to avoid 3rd party libs. If no solution available then we may need to use 3rd party libs. Do you know any sample code that explains about writting wrapper DLL using JNI to call Java?

              L 1 Reply Last reply
              0
              • K KASR1

                Trying to avoid 3rd party libs. If no solution available then we may need to use 3rd party libs. Do you know any sample code that explains about writting wrapper DLL using JNI to call Java?

                L Offline
                L Offline
                Lost User
                wrote on last edited by
                #10

                Why are you doing this? There is no point in writing a C# program that requires a Java library since you have to cross too many boundaries (managed -> unmanaged -> JVM) to make it work. Write your library in C# and do it the easy way.

                Unrequited desire is character building. OriginalGriff I'm sitting here giving you a standing ovation - Len Goodman

                P 1 Reply Last reply
                0
                • L Lost User

                  Why are you doing this? There is no point in writing a C# program that requires a Java library since you have to cross too many boundaries (managed -> unmanaged -> JVM) to make it work. Write your library in C# and do it the easy way.

                  Unrequited desire is character building. OriginalGriff I'm sitting here giving you a standing ovation - Len Goodman

                  P Offline
                  P Offline
                  Pete OHanlon
                  wrote on last edited by
                  #11

                  To be fair to him, he is trying to follow the advice given here[^].

                  Forgive your enemies - it messes with their heads

                  "Mind bleach! Send me mind bleach!" - Nagy Vilmos

                  My blog | My articles | MoXAML PowerToys | Mole 2010 - debugging made easier - my favourite utility

                  L 1 Reply Last reply
                  0
                  • P Pete OHanlon

                    To be fair to him, he is trying to follow the advice given here[^].

                    Forgive your enemies - it messes with their heads

                    "Mind bleach! Send me mind bleach!" - Nagy Vilmos

                    My blog | My articles | MoXAML PowerToys | Mole 2010 - debugging made easier - my favourite utility

                    L Offline
                    L Offline
                    Lost User
                    wrote on last edited by
                    #12

                    :thumbsup:

                    Unrequited desire is character building. OriginalGriff I'm sitting here giving you a standing ovation - Len Goodman

                    1 Reply Last reply
                    0
                    • K KASR1

                      How to call java within c#?. It would be helpful if we know the steps to use java code in C#?

                      J Offline
                      J Offline
                      jschell
                      wrote on last edited by
                      #13

                      In java and only in java. 1. Wrap the java code in a java exe framework (so it can be run at the command line with 'java') 2. Add a communication layer (sockets, files or stdio) written in java. 3. Test the above. In C# and only C#. 1. Use Process to run the above 2. Write code to talk to the communication layer of the above. Benefits. A. Java code is all java. B. C# code is all C# C. No reliance on third parties. D. Unit testing can be done without relying on the other language.

                      1 Reply Last reply
                      0
                      Reply
                      • Reply as topic
                      Log in to reply
                      • Oldest to Newest
                      • Newest to Oldest
                      • Most Votes


                      • Login

                      • Don't have an account? Register

                      • Login or register to search.
                      • First post
                        Last post
                      0
                      • Categories
                      • Recent
                      • Tags
                      • Popular
                      • World
                      • Users
                      • Groups