I am working on getting the tool JNI4NET working so that I can use some Java code I have within my C# application. As a simple initial test I have created a simple Java class library with a single class Person
with one method public String GetName() { return "NoBody"; }
. From here I have been following along with the samples given in the JNI download to edit the generateProxies.cmd
to create the DLL wrapper of the jar.
我正在努力使JNI4NET工具正常工作,这样我就可以使用我在C#应用程序中使用的一些Java代码。作为一个简单的初始测试,我创建了一个简单的Java类库,其中包含一个Person类,其中一个方法是public String GetName(){return“NoBody”; }。从这里开始,我一直在跟随JNI下载中给出的示例来编辑generateProxies.cmd以创建jar的DLL包装器。
I didn't have much luck with this so I decided to try to perform the same action but with the sample, specifically the sample entitled myJavaDemoCalc
. When executed generateProxies.cmd
in the sample folder an error is thrown.
我没有太多运气,所以我决定尝试执行相同的操作但是使用样本,特别是名为myJavaDemoCalc的样本。在示例文件夹中执行generateProxies.cmd时,将引发错误。
(I will transcribe this picture if need be)
(如果需要,我会转录这张照片)
I have followed the link in the exception though while I somewhat understand what it means I am not sure if it is necessarily safe to enable loading from remote sources as it suggests at the end of the linked article.
我已经遵循异常中的链接,虽然我有点理解这意味着我不确定是否必须安全地启用从远程源加载,因为它在链接文章的末尾建议。
I am also confused why the exception is being thrown seeing that the generateProxies.cmd
and thus ProxyGen.exe is being run from my C:
drive.
我也很困惑为什么抛出异常会看到generateProxies.cmd以及因此从我的C:驱动器运行ProxyGen.exe。
Anyone have an idea of what I could try next or know the issue here?
任何人都知道我可以尝试下一步或知道这里的问题?
For reference here is the generateProxies.cmd
source from myJavaDemoCalc
这里的参考是来自myJavaDemoCalc的generateProxies.cmd源代码
@echo off
copy ..\..\lib\*.* work
..\..\bin\proxygen.exe work\myJavaDemoCalc.jar -wd work
cd work
call build.cmd
cd ..
echo compiling usage
csc.exe /nologo /warn:0 /reference:work\jni4net.n-0.8.8.0.dll /reference:work\myJavaDemoCalc.j4n.dll /out:work\demo.exe /target:exe MyCalcUsageInDotnet.cs
1 个解决方案
#1
2
I assume you downloaded that zip file and then immediately Extracted all files.
我假设您下载了该zip文件,然后立即提取所有文件。
However, because that zipfile did originate from an untrusted zone, being the internet, the files in it will also remain untrusted. It contains an alternate data stream with a zone identifier.
但是,因为该zipfile确实来自不受信任的区域,即互联网,其中的文件也将保持不受信任。它包含带有区域标识符的备用数据流。
When those assemblies get loaded by the framework, it checks if they can be trusted. Assemblies with that zone identfier still present don't get loaded. That is the exception you get:
当这些程序集被框架加载时,它会检查它们是否可以信任。具有该区域标识符的程序集仍然存在,不会加载。这是你获得的例外:
System.IO.FileLoadException: Could not load file or assembly 'file:///jni4net.n-0.8.8.0.dll' or one of its dependencies. Operation is not supported. (Exception from HRESULT: 0x80131515) --->
System.NotSupportedException: An attempt was made to load an assembly from a network location which would have caused the assembly to be sandboxed in previous versions of the .NET Framework. This release of the .NET Framework does not enable CAS policy by default, so this load may be dangerous. If this load is not intended to sandbox the assembly, please enable the loadFromRemoteSources switch.System.IO.FileLoadException:无法加载文件或程序集'file:///jni4net.n-0.8.8.0.dll'或其依赖项之一。不支持操作。 (来自HRESULT的异常:0x80131515)---> System.NotSupportedException:尝试从网络位置加载程序集,这会导致程序集在以前版本的.NET Framework中被沙箱化。此版本的.NET Framework默认情况下不启用CAS策略,因此此负载可能很危险。如果此负载不是用于沙盒装配,请启用loadFromRemoteSources开关。
The quickest solution to resolve this is to open the properties window of the downloaded zip file and tick unblock
before you extract all files:
解决此问题的最快解决方案是打开下载的zip文件的属性窗口,并在提取所有文件之前勾选解除阻止:
If you already extracted all the files to a folder you can use the powershell command unblock-file
如果您已将所有文件解压缩到一个文件夹,则可以使用powershell命令unblock-file
Get-ChildItem -Path 'c:\path\to\files' -Recurse | Unblock-File
But if you're sure that you will always run proxygen.exe with trusted assemblies, you can add the suggestion offered in the MSDN article by adding the loadFromRemoteSources
element in the existing proxygen.exe.config:
但是,如果您确定将始终使用受信任程序集运行proxygen.exe,则可以通过在现有proxygen.exe.config中添加loadFromRemoteSources元素来添加MSDN文章中提供的建议:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.0"/>
<supportedRuntime version="v2.0.50727"/>
</startup>
<!-- trust all the thingz -->
<runtime>
<loadFromRemoteSources enabled="true"/>
</runtime>
</configuration>
#1
2
I assume you downloaded that zip file and then immediately Extracted all files.
我假设您下载了该zip文件,然后立即提取所有文件。
However, because that zipfile did originate from an untrusted zone, being the internet, the files in it will also remain untrusted. It contains an alternate data stream with a zone identifier.
但是,因为该zipfile确实来自不受信任的区域,即互联网,其中的文件也将保持不受信任。它包含带有区域标识符的备用数据流。
When those assemblies get loaded by the framework, it checks if they can be trusted. Assemblies with that zone identfier still present don't get loaded. That is the exception you get:
当这些程序集被框架加载时,它会检查它们是否可以信任。具有该区域标识符的程序集仍然存在,不会加载。这是你获得的例外:
System.IO.FileLoadException: Could not load file or assembly 'file:///jni4net.n-0.8.8.0.dll' or one of its dependencies. Operation is not supported. (Exception from HRESULT: 0x80131515) --->
System.NotSupportedException: An attempt was made to load an assembly from a network location which would have caused the assembly to be sandboxed in previous versions of the .NET Framework. This release of the .NET Framework does not enable CAS policy by default, so this load may be dangerous. If this load is not intended to sandbox the assembly, please enable the loadFromRemoteSources switch.System.IO.FileLoadException:无法加载文件或程序集'file:///jni4net.n-0.8.8.0.dll'或其依赖项之一。不支持操作。 (来自HRESULT的异常:0x80131515)---> System.NotSupportedException:尝试从网络位置加载程序集,这会导致程序集在以前版本的.NET Framework中被沙箱化。此版本的.NET Framework默认情况下不启用CAS策略,因此此负载可能很危险。如果此负载不是用于沙盒装配,请启用loadFromRemoteSources开关。
The quickest solution to resolve this is to open the properties window of the downloaded zip file and tick unblock
before you extract all files:
解决此问题的最快解决方案是打开下载的zip文件的属性窗口,并在提取所有文件之前勾选解除阻止:
If you already extracted all the files to a folder you can use the powershell command unblock-file
如果您已将所有文件解压缩到一个文件夹,则可以使用powershell命令unblock-file
Get-ChildItem -Path 'c:\path\to\files' -Recurse | Unblock-File
But if you're sure that you will always run proxygen.exe with trusted assemblies, you can add the suggestion offered in the MSDN article by adding the loadFromRemoteSources
element in the existing proxygen.exe.config:
但是,如果您确定将始终使用受信任程序集运行proxygen.exe,则可以通过在现有proxygen.exe.config中添加loadFromRemoteSources元素来添加MSDN文章中提供的建议:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.0"/>
<supportedRuntime version="v2.0.50727"/>
</startup>
<!-- trust all the thingz -->
<runtime>
<loadFromRemoteSources enabled="true"/>
</runtime>
</configuration>