Skip to content

Commit 6d66d5d

Browse files
committed
[Java.Runtime.Environment] Move java-interop.jar provision responsibility
java-interop.jar MUST be within $CLASSPATH, or all manner of things break (e.g. com.xamarin.android.ManagedPeer is required by Java.Interop.ManagedPeer). Previously the responsibility for providing java-interop.jar was done whoever created JreVMBuilder, but this makes things annoying when writing new utility programs (yet one more thing that needs to be specified). Change the Java.Runtime.Environment.csproj so it's a "normal" Library project (PCL is getting annoying now), add a JreVMBuilder.ClassPath collection property, and auto-add "java-interop.jar" to the JreVMBuilder.ClassPath property. For good measure, provide the *full path* to java-interop.jar as well, so that the current working directory when running apps doesn't need to be the bin/$(CONFIGURATION) directory (otherwise the JDK can't find java-interop.jar).
1 parent 2298181 commit 6d66d5d

File tree

3 files changed

+25
-11
lines changed

3 files changed

+25
-11
lines changed

src/Java.Runtime.Environment/Java.Interop/JreVM.cs

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
using System;
22
using System.Collections.Generic;
3+
using System.Collections.ObjectModel;
4+
using System.IO;
35
using System.Linq;
46
using System.Runtime.InteropServices;
57

@@ -25,9 +27,16 @@ public class JreVMBuilder : JavaVMOptions {
2527
public JniVersion JniVersion {get; set;}
2628
public bool IgnoreUnrecognizedOptions {get; set;}
2729

30+
public Collection<string> ClassPath {get; private set;}
31+
2832
public JreVMBuilder ()
2933
{
3034
JniVersion = JniVersion.v1_2;
35+
ClassPath = new Collection<string> () {
36+
Path.Combine (
37+
Path.GetDirectoryName (typeof (JreVMBuilder).Assembly.Location),
38+
"java-interop.jar"),
39+
};
3140
}
3241

3342
public JreVMBuilder AddOption (string option)
@@ -42,6 +51,8 @@ public JreVMBuilder AddSystemProperty (string name, string value)
4251
throw new ArgumentNullException ("name");
4352
if (value == null)
4453
throw new ArgumentNullException ("value");
54+
if (name == "java.class.path")
55+
throw new ArgumentException ("Do not use AddSystemProperty() for the 'java.class.path' property. Add to the ClassPath collection instead.", "name");
4556
Options.Add (string.Format ("-D{0}={1}", name, value));
4657
return this;
4758
}
@@ -108,13 +119,15 @@ static unsafe JreVMBuilder CreateJreVM (JreVMBuilder builder)
108119

109120
var args = new JavaVMInitArgs () {
110121
version = builder.JniVersion,
111-
nOptions = builder.Options.Count,
122+
nOptions = builder.Options.Count + 1,
112123
ignoreUnrecognized = builder.IgnoreUnrecognizedOptions ? (byte) 1 : (byte) 0,
113124
};
114-
var options = new JavaVMOption [builder.Options.Count];
125+
var options = new JavaVMOption [builder.Options.Count + 1];
115126
try {
116-
for (int i = 0; i < options.Length; ++i)
127+
for (int i = 0; i < builder.Options.Count; ++i)
117128
options [i].optionString = Marshal.StringToHGlobalAnsi (builder.Options [i]);
129+
var classPath = Marshal.StringToHGlobalAnsi (string.Format ("-Djava.class.path={0}", string.Join (Path.PathSeparator.ToString (), builder.ClassPath)));
130+
options [builder.Options.Count].optionString = classPath;
118131
fixed (JavaVMOption* popts = options) {
119132
args.options = (IntPtr) popts;
120133
JavaVMSafeHandle javavm;

src/Java.Runtime.Environment/Java.Runtime.Environment.csproj

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,10 @@
66
<ProductVersion>10.0.0</ProductVersion>
77
<SchemaVersion>2.0</SchemaVersion>
88
<ProjectGuid>{5887B410-D448-4257-A46B-EAC03C80BE93}</ProjectGuid>
9-
<ProjectTypeGuids>{786C830F-07A1-408B-BD7F-6EE04809D6DB};{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}</ProjectTypeGuids>
109
<OutputType>Library</OutputType>
1110
<RootNamespace>Java.Interop</RootNamespace>
1211
<AssemblyName>Java.Runtime.Environment</AssemblyName>
13-
<TargetFrameworkProfile>Profile6</TargetFrameworkProfile>
14-
<TargetFrameworkVersion>v4.0</TargetFrameworkVersion>
12+
<TargetFrameworkVersion>v4.5</TargetFrameworkVersion>
1513
</PropertyGroup>
1614
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
1715
<DebugSymbols>true</DebugSymbols>
@@ -39,7 +37,10 @@
3937
<Compile Include="Java.Interop\JniRuntime.cs" />
4038
<Compile Include="Java.Interop\JreVM.cs" />
4139
</ItemGroup>
42-
<Import Project="$(MSBuildExtensionsPath32)\Microsoft\Portable\$(TargetFrameworkVersion)\Microsoft.Portable.CSharp.targets" />
40+
<ItemGroup>
41+
<Reference Include="System" />
42+
</ItemGroup>
43+
<Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" />
4344
<ItemGroup>
4445
<ProjectReference Include="..\Java.Interop\Java.Interop.csproj">
4546
<Project>{94BD81F7-B06F-4295-9636-F8A3B6BDC762}</Project>

tests/TestJVM/TestJVM.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -221,10 +221,10 @@ public class TestJVM : JreVM {
221221
static JreVMBuilder CreateBuilder (string[] jars)
222222
{
223223
var builder = new JreVMBuilder ();
224-
var _jars = new List<string> (jars ?? new string [0]) {
225-
"java-interop.jar",
226-
};
227-
builder.AddSystemProperty ("java.class.path", string.Join (":", _jars));
224+
if (jars != null) {
225+
foreach (var jar in jars)
226+
builder.ClassPath.Add (jar);
227+
}
228228

229229
TextWriter grefLog = null;
230230
TextWriter lrefLog = null;;

0 commit comments

Comments
 (0)