I am creating a source code editor for Java using C# in .NET 4. I need to know how I can interact with the jre to compile the java code on my application editor interface from .net. I wish to display the user any errors on the code as well. Any ideas on how to accomplish this will be greatly appreciated.
Note that the JRE is the runtime, and as such doesn't come with the compiler. You will require the JDK to compile.
I think the simplest solution is to spawn off an instance of javac from within your .Net application. Perhaps a preferable solution (however) would be to write your app in Java itself, and you can then make use of the Java compiler API directly within your app.
Perhaps the most natural interoperability method is to run the Java code in a JVM, the .NET code in a CLR, and use a runtime bridge to manage the communications between them. In this scenario, the .NET code calls .NET classes and objects that act as proxies for the Java classes and objects. The proxies manage the communication; the calling .NET classes aren't even aware that they're ultimately calling Java classes. A runtime bridge provides interoperability for a wide variety of architectures, because the Java code and the .NET code can be on different machines, the Java code can run in a standalone JVM or in a Java EE application server, and the solution provides interoperability for any JDK version. As with bytecode translation solutions, you need only the Java bytecodes, not the source.
The code for this article uses JNBridgePro from JNBridge.
Related
I have a c# dll that needs to be called in Java.I see that there is a method using jni to call c++ dlls.How can I do it for a c# dll..Please help..I couldnt find any good material on this
From here:-
IKVM.NET is an implementation of Java for Mono and the Microsoft .NET
Framework. It includes the following components:
A Java Virtual Machine implemented in .NET
A .NET implementation of the Java class libraries
Tools that enable Java and .NET interoperability
You can use Java Native Interface. Or you can create a COM assembly from the C# code and use J-Interop to invoke it.
If you have C# dll sources you need to use maybe the better way will be to translate it to Java using some tools like GrassHopper.
According to GrassHopper key feature explanation it can convert MSIL to Java bite code. So can use without sources of c# dll
Check this: http://www.javonet.com
If you look for quick and easy solution then Javonet should work fine for you. It is light counterpart of IKVM and J-Integra works also as native bridge.
All you have to do is:
add Javonet.jar do your project call
call Javonet.addReference("yourlib.dll")
use your .NET library like it was almost JAVA package
Sample:
NObject obj = Javonet.New("yourDotNetClass");
obj.invoke("YourMethod","arg1", 2);
The syntax is not strongly-typed and works like reflection but gives you fastest access to any custom .NET code, third-party libs or .NET framework as no changes are needed on .NET side. If you need it is also possible to implement custom strongly-typed wrappers.
I do recommend this bridge as in my opinion it is easiest to quickly get things done but also other native bridges are worth checking as this is best approach for such case.
I would avoid going into custom JNI or COM unless you have a lot of time and you just want to learn, if you need quick and reliable solution take one of third-party bridges.
I have a game server which is made in Java. I want to make a plugin system that loads a .NET DLL and calls events / functions inside that DLL, then inside those I'll have to call functions in the game server (Java). The only part that is giving me trouble at the moment is how to interface java and a .NET dll.
I've been searching and found some things but they were all based on products and I want to make my own interface for that. Ah, not to mention it needs to have high performance, the code will be called a lot of times in a second if it has to. Could someone point or give-me ideas how could I work this out?
EDIT:
To make it more explicit:
Game Server (Java application) calls a function in .NET dll
The .NET function just called by java, calls multiple functions from Game Server (Java Application).
Take a look at jni4net if you're targeting Windows. It's an alpha quality release, but Robocode already uses it to run .NET robots inside the Java runtime.
Another option is to use a high-performance messaging approach. You'll need a second process - likely a .NET plug-in host. That process then exchanges messages with the main Java game process. Messaging libraries like 0MQ are pretty darn fast but may not be fast enough for what you have in mind. In addition, you'll have to create a lot of message plumbing which may be cost/time prohibitive.
Try using iKVM:
IKVM.NET is an implementation of Java for Mono and the Microsoft .NET
Framework. It includes the following components:
A Java Virtual Machine implemented in .NET
A .NET implementation of the Java class libraries Tools that enable Java and .NET
interoperability
http://www.ikvm.net/
If you only have a few methods you are calling you might just use JNI and do it yourself instead of a 3rd Party tool (though I admit I don't know the details of jni4net). Just a word of caution, the project I'm on had to do a similar thing (C# -> C/C++ -> Java via JNI) and we had nothing but problems with it. Problems mainly because the java api didn't have any good documentation so that might have been part of it. If at all possible try to keep it to one language but if that is not possible, make sure you do lots of error checking. When the app crashes, it is very hard to find the problem (unless you own both the java and C# sutff). Just my $0.02...
I know this is a strange question but the idea is simple: I prefer C# syntax rather than C++:
-Setters and getters directly inside a property
-interfaces
-foreach statement
-possibility to declare an implicit cast operator
other small things...
What I really don't know is if is possible to import a c++ dll (expecially std libraries) in C# if I don't use any namespace (even System)
The idea is just to write a program using everything that you will normally use in C++ (nothing from CLR so), even printf for example
Thanks for any answer
No it is not possible to simply import existing C or C++ files into a C# project. They are very different languages and cannot be mixed at the source level.
The way to mix C# and C++/C applications is at the PInvoke or COM Interop level. This works by duplicating the signatures in C# and allowing the C# compiler to determine the binary layout of the native type in order to marshal between the languages.
There's now something close to this
.NET Native compiles C# to native machine code that performs like C++. You will continue to benefit from the productivity and familiarity of the .NET Framework with the great performance of native code.
It's for Windows Store apps only (desktop apps may come in the future):
Desktop apps are a very important part of our strategy. Initially, we are focusing on Windows Store apps with .NET Native. In the longer term we will continue to improve native compilation for all .NET applications.
And
apps will get deployed on end-user devices as fully self-contained natively compiled code (when .NET Native enters production), and will not have a dependency on the .NET Framework on the target device/machine
No; this is not directly possible. In particular, C++ templates are not supported by C#.
It's my understanding that C#, though created by Microsoft, is a programming language that can be used or implemented for things other than .NET programming. For example, I could create a compiler for C# to target native x86 or other processors or even a JVM. Is my understanding incorrect? Does anyone know if C# is used for anything besides .NET?
C# is used for an experimental operating system called 'Singularity' which is written in the managed level from the ground up. An interesting project to watch!
Edit: Thanks Jörg W Mittag for his comment - Helios is actually a modified form of Singularity to support satellite kernels, to quote from the paper I am reading right now 'Helios is an operating system designed to simplify the task of writing, deploying and tuning applications for heterogenous platforms. Helios introduces satellite kernels, which export a single uniform set of OS abstractions across CPUs of disparate architectures and performance characteristics. Satellite Kernels allows developer to write applications against familiar operating system APIs and abstractions.'...interesting...
Hope this helps,
Best regards,
Tom.
You are correct that it can be used for other things. It's a language specification...you can compile it into anything you like if you take the time, but it'd be a tremendous effort.
I don't know of any other uses besides Mono as jrcs3 said, but hopefully Eric Lippert will chime in and give you a compiler point of view on C#
C# itself is a specification specified by the ECMA that "specifies the form and establishes the interpretation of programs written in the C# programming language." as is the CLR. The most popular implementation of these is currently provided by Microsoft, which is the C#.net that you know. C# and the CLR are open standards, meaning that anyone can implement them themselves, hence the formation of mono, a C# compiler for mac/linux. Compare this to java, who's only implementation is provided by sun. It's not an open standard, so you can't create your own implementations.
from wikipedia:
Mono "An open source, cross-platform, implementation of C# and the CLR that is binary compatible with Microsoft.NET"
DotGNU "is a part of the GNU Project that aims to provide a free software replacement for Microsoft's .NET Framework"
Dot Net Anywhere is a .net CIL interpreter for targeting embedded systems.
As far as I'm aware, all currently existing implementations of C# build to CIL and run in the .NET CLR or compatible environment, and the only alternative to the .NET CLR I am aware of is Mono.
In other words, all current versions of C# build to run in .NET or Mono.
.NET and Mono use the same 'binaries', so effectively all current C# compilers have only one target.
Another area of use is for iPhone app development (and I believe iPad now as well) via MonoTouch.
What is MonoTouch?
MonoTouch allows developers to create C# and .NET based applications that run on Apple's iPhone and Apple's iPod Touch devices, while taking advantage of the iPhone APIs and reusing both code and libraries that have been built for .NET, as well as existing skills.
Roughly speaking, it goes like this:
MonoTouch -> MonoDevelop/C# -> write an iPhone app -> compiled to native code -> iPhone app.
Relevant links:
MonoTouch
Writing your First IPhone application in C# using MonoTouch
Note that Unity 3D also allows "C# Scripts", and compiles to target Mac, Windows, Web, Wii and iPhone (and therefore iPad).
There are indeed tools to convert a .NET assembly to a standalone executable or library, so that you don't need your clients to have the .NET framework. This also further protects your code against reverse-engineering to some degree -- you can't use Reflector to see the .NET source on a native binary.
See Remotesoft Salamander .NET Protector, for instance.
But, that isn't strictly C# -> native; Salamander converts the MSIL to native code.
On the other hand, there is at least one academic project on making a C# -> native compiler using LLVM, called LLVM#.
You can compile a C# program on Mono. But that probably doesn't count.
Mono is an alternative implementation of the Common Language Infrastructure (CLI) that runs on Linux and various other operating systems. As mentioned in other places, both C# and the CLI are ECMA standards and can be implemented by anyone who cares to do so.
Rotor is another non .NET implementation.
You can use it on iPod apps if you use MonoTouch.
Is it possible to write a user interface in Java for an application written in C#?
I am working on a user interface of a project that is written in C#, but I have no experience with C# and I am an avid Java user. Is it possible to build the user interface in Java using Java's Swing and AWT libraries that operates an application primarily written in C#.
If this sounds like a really stupid question, I apologize in advance.
You might be able to leverage some of the interoperability features that are integrated into Mono 2.0
http://www.mono-project.com/Main_Page
JNBridge is another possible interoperability solution:
http://www.jnbridge.com/
However, a more optimal approach might be to expose your .NET code as Services - and then access them from the Java client (or through a light-weight ESB).
Of course, time, budget, resources are constraints you'll have to consider.
In addition to http://www.jnbridge.com (proprietary)
you can try http://www.janetdev.org, - open source implementation of a Java 5 SE JDK environment for the .NET platform. Currently it supports .Net 3.5 only (not Mono).
We did this recently and went the route of using a low level socket connection, but pushing xml through it. C# was the server side, and we used the Microsoft 'xsd' tool to generate the XSD schema for the objects and then used JAXB on the java side to generate java code to parse and hold the same objects.
As Barry mentions most of the work/problems was around the socket connection - but that depends on how comfortable you are with that.
Also, for a solution that cross-compiles your java to run in the CLR: http://www.ikvm.net/
I am author of jni4net, open source interprocess bridge between JVM and CLR. It's build on top of JNI and PInvoke. No C/C++ code needed. I hope it will help you.