Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[submodule "dynamicLinker"]
path = dynamicLinker
url = https://github.com/Marqin/dynamicLinker
31 changes: 31 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
cmake_minimum_required (VERSION 3.8)
project (ASA4J)

# TODO wall
set(CMAKE_CXX_STANDARD 14)
set(CMAKE_CXX_STANDARD_REQUIRED ON)

# 64bit compile
SET(CMAKE_CXX_FLAGS "-m64")
SET(CMAKE_C_FLAGS "-m64")
SET(CMAKE_EXE_LINKER_FLAGS "-m64")
SET(CMAKE_MODULE_LINKER_FLAGS "-m64")
SET(CMAKE_SHARED_LINKER_FLAGS "-m64")

set(CMAKE_POSITION_INDEPENDENT_CODE ON) # fPIC

find_package(JNI REQUIRED)
find_package(Java REQUIRED)
include(UseJava)

message (STATUS "JNI_INCLUDE_DIRS=${JNI_INCLUDE_DIRS}")
message (STATUS "JNI_LIBRARIES=${JNI_LIBRARIES}")

add_library(tar SHARED CoreCLRHost.cpp CoreCLRHost.hpp utils.hpp Sample1.h
dynamicLinker/dynamicLinker.cpp)
target_link_libraries(tar dl stdc++fs)
target_include_directories(tar PUBLIC ${JNI_INCLUDE_DIRS} dynamicLinker)

add_jar(sample1 Sample1.java GENERATE_NATIVE_HEADERS sample-native)

execute_process(COMMAND dotnet build)
6 changes: 3 additions & 3 deletions CoreCLRHost.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ int runFromEntryPoint(
std::cerr << e.what() << std::endl;
return -1;
} catch ( dynamicLinker::dynamicLinkerException e ) {
std::cerr << e.what() << std::endl;
std::cerr << "dynamicLinkerEx: " << e.what() << std::endl;
return -1;
}

Expand Down Expand Up @@ -215,8 +215,8 @@ JNIEXPORT jint JNICALL Java_Sample1_coreClrHost(JNIEnv *env, jobject obj, jstrin
assemblyDir = cwd + assemblyDir;

int exitCode = runFromEntryPoint(
cwd+std::string("./tar.so"), // absolute path to this exe
std::string("/usr/share/dotnet/shared/Microsoft.NETCore.App/2.0.0-preview1-002111-00/"), // absolute path to coreCLR DLLs
cwd, //+std::string("./tar.so"), // absolute path to this exe
std::string("/usr/share/dotnet/shared/Microsoft.NETCore.App/2.2.1/"), // absolute path to coreCLR DLLs
assemblyDir, // absolute path to DLL to run
assemblyName,
std::string("Managed"),
Expand Down
16 changes: 8 additions & 8 deletions CoreCLRHost.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@
* Licensed under the MIT license. See LICENSE file in the project root for full license information.
*/

//#pragma once
#pragma once

//#if not defined (__unix__) && not defined(__unix) && not defined (unix) && ( (not defined (__APPLE__) || not defined (__MACH__)) )
// #error THIS SOFTWARE IS ONLY FOR UNIX-LIKE SYSTEMS!
//#endif
#if not defined (__unix__) && not defined(__unix) && not defined (unix) && ( (not defined (__APPLE__) || not defined (__MACH__)) )
#error THIS SOFTWARE IS ONLY FOR UNIX-LIKE SYSTEMS!
#endif

#include <functional>
#include <iostream>
Expand All @@ -25,11 +25,11 @@
#define PATH_MAX FILENAME_MAX
#endif

//#if defined(__APPLE__)
// std::string coreClrDll = "libcoreclr.dylib";
//#else
#if defined(__APPLE__)
std::string coreClrDll = "libcoreclr.dylib";
#else
std::string coreClrDll = "libcoreclr.so";
//#endif
#endif

class myClass {
private:
Expand Down
30 changes: 0 additions & 30 deletions Makefile

This file was deleted.

7 changes: 7 additions & 0 deletions Managed.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>library</OutputType>
<TargetFramework>netcoreapp2.2</TargetFramework>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
</PropertyGroup>
</Project>
22 changes: 9 additions & 13 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,29 +1,25 @@
[![Build Status](https://dev.azure.com/marcozo0520/JavaCallCSharp/_apis/build/status/eisber.JavaCallCSharp?branchName=master)](https://dev.azure.com/marcozo0520/JavaCallCSharp/_build/latest?definitionId=1?branchName=master)


# JavaCallCSharp
Java call C# lib build with .NET CORE 2.0 via C++ as wraper
The code is based on [example from coreCLR](https://github.com/dotnet/coreclr/tree/master/src/coreclr/hosts/unixcoreruncommon)

Java using JNI to call C++ code. then C++ host a core CLR to run C# code.
# pre-require
JDK: You should edit the Makefile and set the JAVALIB PATH

.NET CORE SDK 2.0: I only tested it in Ubuntu 16.04 x64
.NET CORE SDK 2.0: I only tested it in Ubuntu 18.04 x64 (Follow https://dotnet.microsoft.com/download/linux-package-manager/ubuntu18-04/sdk-current)

gcc 6: std C++ 14 for the filesystem

# usage:
javac Sample1.java

javah Sample1

make

cd cs

dotnet restore
git submodule init
git submodule update

dotnet build
cmake .
cmake --build .

java Sample1
java -cp sample1.jar Sample1


`You should keep the compiled file in the same fold.`
4 changes: 2 additions & 2 deletions Sample1.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ public class Sample1
public static void main(String[] args)
{
//System.loadLibrary("Sample1");
System.load("./tar.so");
System.load(System.getProperty("user.dir") + java.io.File.separator + "libtar.so");

Sample1 sample = new Sample1();
int square = sample.intMethod(5);
Expand All @@ -17,7 +17,7 @@ public static void main(String[] args)
int sum = sample.intArrayMethod(new int[]{1,1,2,3,5,8,13});

int success=0;
success= sample.coreClrHost("./Managed.dll");
success= sample.coreClrHost("./bin/Debug/netcoreapp2.2/Managed.dll");
System.out.println("intMethod: "+ square);
System.out.println("boolMethod: "+ bool);
System.out.println("stringMethod: "+ text);
Expand Down
18 changes: 18 additions & 0 deletions azure-pipelines.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
jobs:
- template: build.yml
parameters:
name: macOS
pool:
vmImage: 'macOS-10.13'

- template: build.yml
parameters:
name: Linux
pool:
vmImage: 'Ubuntu-16.04'

# - template: build.yml
# parameters:
# name: Windows
# pool:
# vmImage: 'vs2017-win2016'
13 changes: 13 additions & 0 deletions build.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
jobs:

- job: ${{ parameters.name }}
pool: ${{ parameters.pool }}

steps:
- checkout: self
submodules: true
- script: |
cmake .
cmake --build .
java -cp sample1.jar Sample1
displayName: 'Build & run'
1 change: 1 addition & 0 deletions dynamicLinker
Submodule dynamicLinker added at ba6643