MASES.JNet
2.5.6
See the version list below for details.
dotnet add package MASES.JNet --version 2.5.6
NuGet\Install-Package MASES.JNet -Version 2.5.6
<PackageReference Include="MASES.JNet" Version="2.5.6" />
paket add MASES.JNet --version 2.5.6
#r "nuget: MASES.JNet, 2.5.6"
// Install MASES.JNet as a Cake Addin #addin nuget:?package=MASES.JNet&version=2.5.6 // Install MASES.JNet as a Cake Tool #tool nuget:?package=MASES.JNet&version=2.5.6
title: Usage Java/JVM suite for .NET _description: Describes how to use JNet, set-up environment, identify the JVM and write good code
JNet usage
To use JNet classes the developer can write code in .NET using the same classes available in the official Java packages. If classes or methods are not available yet it is possible to use the approach synthetized in What to do if an API was not yet implemented
Environment setup
JNet accepts many command-line switches to customize its behavior. The full list is available at Command line switch page.
JVM identification
One of the most important command-line switch is JVMPath and it is available in JCOBridge switches: it can be used to set-up the location of the JVM library (jvm.dll/libjvm.so) if JCOBridge is not able to identify a suitable JRE installation. If a developer is using JNet within its own product it is possible to override the JVMPath property with a snippet like the following one:
class MyJNetCore : JNetCore
{
public override string JVMPath
{
get
{
string pathToJVM = "Set here the path to JVM library or use your own search method";
return pathToJVM;
}
}
}
IMPORTANT NOTE: pathToJVM
shall be escaped
string pathToJVM = "C:\\Program Files\\Eclipse Adoptium\\jdk-11.0.18.10-hotspot\\bin\\server\\jvm.dll";
string pathToJVM = @"C:\Program Files\Eclipse Adoptium\jdk-11.0.18.10-hotspot\bin\server\jvm.dll";
Special initialization conditions
JCOBridge try to identify a suitable JRE/JDK installation within the system using some standard mechanism of JRE/JDK: JAVA_HOME
environment variable or Windows registry if available.
However it is possible, on Windows operating systems, that the library raises an InvalidOperationException: Missing Java Key in registry: Couldn't find Java installed on the machine.
This means that neither JAVA_HOME
nor Windows registry contains information about a default installed JRE/JDK: some vendors may not setup them.
If the developer/user encounter this condition can do the following steps:
- On a command prompt execute
set | findstr JAVA_HOME
and verify the result; - If something was reported maybe the
JAVA_HOME
environment variable is not set at system level, but at a different level like user level which is not visible from the JNet process that raised the exception; - Try to set
JAVA_HOME
at system level e.g.JAVA_HOME=C:\Program Files\Eclipse Adoptium\jdk-11.0.18.10-hotspot\
; - Try to set
JCOBRIDGE_JVMPath
at system level e.g.JCOBRIDGE_JVMPath=C:\Program Files\Eclipse Adoptium\jdk-11.0.18.10-hotspot\
.
IMPORTANT NOTES:
- One of
JCOBRIDGE_JVMPath
orJAVA_HOME
environment variables or Windows registry (on Windows OSes) shall be available JCOBRIDGE_JVMPath
environment variable takes precedence overJAVA_HOME
and Windows registry: you can setJCOBRIDGE_JVMPath
toC:\Program Files\Eclipse Adoptium\jdk-11.0.18.10-hotspot\bin\server\jvm.dll
and avoid to overrideJVMPath
in your code- After first initialization steps,
JVMPath
takes precedence overJCOBRIDGE_JVMPath
/JAVA_HOME
environment variables or Windows registry
Basic example
Below a basic example which demonstrates how to create a program based on JNet and some other features available like generics and exception handling. Within the program the comments try to explain how the code works.
using Java.Util;
using MASES.JNet.Extensions;
using System.Diagnostics;
using Java.Lang;
namespace MASES.JNetExample
{
// this class defines a concrete implementation of JNetCore<>
class MyJNetCore : JNetCore<MyJNetCore>
{
}
class Program
{
static void Main(string[] args)
{
// the first step is mandatory:
// it invokes the method CreateGlobalInstance to allocate the JVM and prepares the environment
MyJNetCore.CreateGlobalInstance();
// at the end of initialization the arguments in the command line not used from JNet (and JCOBridge)
// are available to be used like any developer does with the args in the Main
var appArgs = MyJNetCore.FilteredArgs;
// now we go into .NET/JVM interaction based on generics
try
{
// in the first step the code allocates a java.util.Set<String> within the JVM using the java.util.Collections class
// and returns a Java.Util.Set<string> in .NET
Java.Util.Set<string> set = Collections.Singleton("test");
// then the code tries to Add a new value if it is available in command-line,
// but we expect the JVM raises an exception
if (appArgs.Length != 0) set.Add(appArgs[0]);
}
// if the Add is invoked the operation on java.util.Set<String> cannot be performed
// because Collections.Singleton returns an immutable java.util.Set<String>
// see https://docs.oracle.com/en/java/javase/11/docs/api/java.base/java/util/Collections.html#singleton(T))
catch (UnsupportedOperationException)
{
// so we enter here because the engine translates the Java exception in an equivalent exception managed from C#
System.Console.WriteLine("Operation not supported as expected");
}
// this piece of code is for any convenience because we want a clean close of the application
catch (System.Exception ex) { System.Console.WriteLine(ex.Message); }
}
}
}
Avoid Java.Lang.NullPointerException
writing a good code
Sometime during execution a Java.Lang.NullPointerException
can be raised and seems there isn't neither a real problem in the .NET code you wrote nor a specific pattern or time when it is raised.
The problem is behind the scene and it is correlated on how Garbage Collector and code optimizer works.
In the code of the previous chapter the Collections.Singleton("test")
creates an object which is used from set.Add(appArgs[0])
and in this case the Garbage Collector does not retires the object.
Considering the following code snippet:
using Java.Util;
using MASES.JNet.Extensions;
using System.Diagnostics;
using Java.Lang;
namespace MASES.JNetExample
{
class MyJNetCore : JNetCore<MyJNetCore> { }
class Program
{
static void Main(string[] args)
{
MyJNetCore.CreateGlobalInstance();
try
{
Java.Util.Set<string> set = Collections.Singleton("test");
ArrayList<string> arrayList = new();
arrayList.AddAll(0, set); // this point can raise Java.Lang.NullPointerException
}
catch (System.Exception ex) { System.Console.WriteLine(ex.Message); }
}
}
}
the Collections.Singleton("test")
ends its life, from .NET point of view, when arrayList.AddAll(0, set)
is invoked:
Java.Util.Set<string>
is a .NET container for JVMjava.util.Set<String>
arrayList.AddAll(0, set)
receives theJava.Util.Set<string>
instance and sends to JVM the reference tojava.util.Set<String>
of JVM- from .NET point of view
Java.Util.Set<string>
has ended its life and can be retired because does not have any other root referencing it - .NET Garbage Collector activates arbitrarily when some conditions meet: https://learn.microsoft.com/en-us/dotnet/standard/garbage-collection/
Most of the time the code above works without problem, but sometimes the JVM can raise a Java.Lang.NullPointerException
because Java.Util.Set<string>
was retired from .NET GC.
To solve the issue, and force the GC to not retire the instance, there are some possible code snippet a developer can follows:
using
or try-finally
with Dispose
patterns
All classes implements IDisposable
interface, the code snippet becomes:
using Java.Util;
using MASES.JNet.Extensions;
using System.Diagnostics;
using Java.Lang;
namespace MASES.JNetExample
{
class MyJNetCore : JNetCore<MyJNetCore> { }
class Program
{
static void Main(string[] args)
{
MyJNetCore.CreateGlobalInstance();
try
{
using (Java.Util.Set<string> set = Collections.Singleton("test"))
{
ArrayList<string> arrayList = new();
arrayList.AddAll(0, set);
}
}
catch (System.Exception ex) { System.Console.WriteLine(ex.Message); }
}
}
}
or
using Java.Util;
using MASES.JNet.Extensions;
using System.Diagnostics;
using Java.Lang;
namespace MASES.JNetExample
{
class MyJNetCore : JNetCore<MyJNetCore> { }
class Program
{
static void Main(string[] args)
{
MyJNetCore.CreateGlobalInstance();
try
{
Java.Util.Set<string> set = null;
try
{
set = Collections.Singleton("test");
ArrayList<string> arrayList = new();
arrayList.AddAll(0, set);
}
finally { set?.Dispose(); }
}
catch (System.Exception ex) { System.Console.WriteLine(ex.Message); }
}
}
}
SuppressFinalize
/ReRegisterForFinalize
pattern
Over every .NET object can be invoke the SuppressFinalize
, the code snippet becomes:
using Java.Util;
using MASES.JNet.Extensions;
using System.Diagnostics;
using Java.Lang;
namespace MASES.JNetExample
{
class MyJNetCore : JNetCore<MyJNetCore> { }
class Program
{
static void Main(string[] args)
{
MyJNetCore.CreateGlobalInstance();
try
{
Java.Util.Set<string> set = Collections.Singleton("test");
try
{
System.GC.SuppressFinalize(set);
ArrayList<string> arrayList = new();
arrayList.AddAll(0, set);
}
finally { System.GC.ReRegisterForFinalize(set); }
}
catch (System.Exception ex) { System.Console.WriteLine(ex.Message); }
}
}
}
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET | net6.0 is compatible. net6.0-android was computed. net6.0-ios was computed. net6.0-maccatalyst was computed. net6.0-macos was computed. net6.0-tvos was computed. net6.0-windows was computed. net7.0 was computed. net7.0-android was computed. net7.0-ios was computed. net7.0-maccatalyst was computed. net7.0-macos was computed. net7.0-tvos was computed. net7.0-windows was computed. net8.0 is compatible. net8.0-android was computed. net8.0-browser was computed. net8.0-ios was computed. net8.0-maccatalyst was computed. net8.0-macos was computed. net8.0-tvos was computed. net8.0-windows was computed. |
.NET Framework | net462 is compatible. net463 was computed. net47 was computed. net471 was computed. net472 was computed. net48 was computed. net481 was computed. |
-
.NETFramework 4.6.2
- MASES.CLIParser (>= 3.2.1)
- MASES.JCOBridge (>= 2.5.17)
- Microsoft.Bcl.AsyncInterfaces (>= 8.0.0)
-
net6.0
- MASES.CLIParser (>= 3.2.1)
- MASES.JCOBridge (>= 2.5.17)
-
net8.0
- MASES.CLIParser (>= 3.2.1)
- MASES.JCOBridge (>= 2.5.17)
NuGet packages (2)
Showing the top 2 NuGet packages that depend on MASES.JNet:
Package | Downloads |
---|---|
MASES.KNet
Core of .NET suite for Apache Kafka. KNet is a comprehensive .NET suite for Apache Kafka providing all features: Producer, Consumer, Admin, Streams, Connect, backends (ZooKeeper and Kafka). |
|
MASES.JNetPSCore
JNetPSCore - JNet (Java/JVM suite for .NET) PowerShell base library |
GitHub repositories
This package is not used by any popular GitHub repositories.
Version | Downloads | Last updated |
---|---|---|
2.5.10 | 62,768 | 11/4/2024 |
2.5.9 | 142,262 | 9/19/2024 |
2.5.8 | 54,217 | 7/31/2024 |
2.5.7 | 5,443 | 7/30/2024 |
2.5.6 | 268 | 7/28/2024 |
2.5.5 | 82,954 | 6/27/2024 |
2.5.4 | 28,097 | 6/22/2024 |
2.5.3 | 263 | 6/18/2024 |
2.5.2 | 268 | 6/11/2024 |
2.5.1 | 42,107 | 5/25/2024 |
2.5.0 | 14,065 | 5/14/2024 |
2.4.0 | 159,209 | 2/29/2024 |
2.3.0 | 1,010 | 2/20/2024 |
2.2.5 | 59,164 | 1/30/2024 |
2.2.4 | 48,720 | 1/27/2024 |
2.2.3 | 2,199 | 1/24/2024 |
2.2.2 | 1,262 | 1/24/2024 |
2.2.1 | 12,641 | 1/20/2024 |
2.2.0 | 1,585 | 1/17/2024 |
2.1.1 | 2,457 | 12/18/2023 |
2.1.0 | 92,880 | 11/25/2023 |
2.0.2 | 47,658 | 10/18/2023 |
2.0.1 | 195,146 | 7/11/2023 |
2.0.0 | 37,058 | 7/5/2023 |
1.5.5 | 56,725 | 5/5/2023 |
1.5.4 | 45,970 | 4/16/2023 |
1.5.3 | 32,674 | 4/10/2023 |
1.5.2 | 64,937 | 3/13/2023 |
1.5.1 | 55,319 | 2/9/2023 |
1.5.0 | 946 | 2/8/2023 |
1.4.15 | 144,033 | 11/21/2022 |
1.4.14 | 1,378 | 11/9/2022 |
1.4.13 | 1,056 | 11/9/2022 |
1.4.12 | 19,937 | 10/30/2022 |
1.4.11 | 1,107 | 10/27/2022 |
1.4.8 | 19,598 | 10/20/2022 |
1.4.7 | 1,113 | 10/13/2022 |
1.4.6 | 236,336 | 8/18/2022 |
1.4.5 | 1,127 | 8/5/2022 |
1.4.4 | 1,177 | 6/19/2022 |
1.4.3 | 34,582 | 5/19/2022 |
1.4.2 | 1,400 | 5/7/2022 |
1.4.1 | 1,148 | 4/29/2022 |
1.4.0 | 992 | 4/13/2022 |
1.3.0 | 5,563 | 3/28/2022 |
1.2.0 | 924 | 3/25/2022 |
1.1.1 | 1,370 | 3/19/2022 |
1.1.0 | 896 | 3/19/2022 |
1.0.0 | 936 | 3/19/2022 |