benanders / rjni

Run Java code from Rust!
MIT License
73 stars 4 forks source link

How to build from source? #4

Open ErichDonGubler opened 6 years ago

ErichDonGubler commented 6 years ago

It's unclear how to build from source at the moment. Attempting to build from a Windows 10 box for the stable MSVC target with no attempt at special configuration yields:

~/…/experiments/rjni master$ cargo build --example instance
    Updating registry `https://github.com/rust-lang/crates.io-index`
   Compiling libc v0.2.40
   Compiling rjni v0.0.1 (file:///…/experiments/rjni)
error: linking with `link.exe` failed: exit code: 1181
  |
  = note: // snip, horribly long linker invocation
  = note: LINK : fatal error LNK1181: cannot open input file 'jvm.lib'

I understand that this project is a work-in-progress, and this may not be an immediate priority for you. However, adding documentation like this will enable people to experiment and contribute, if it's something you think might help this project along. :)

benanders commented 6 years ago

Thanks for taking an interest. You're right, this project isn't a huge priority for me. I unfortunately don't have a Windows box to experiment on at the moment.

For some reason, macOS requires a legacy version of Java (Java 6) in order for the JNI to work. Maybe you could try downloading the equivalent for Windows? Other than that, I'm out of ideas really. Sorry.

joshbooks commented 6 years ago

macOS requires a legacy version of Java (Java 6) in order for the JNI to work.

pretty sure that's not right, I recently (last job) built a java application for mac with jni components using java 8 (built the app using javafx though, which does a bunch of weird stuff)

benanders commented 6 years ago

Yeah all the API calls that RJNI uses are documented in the Java 8 JNI documentation. So I'm sure there's a way to get the project working with Java 8, but for some reason every time I try and use RJNI macOS asks me to download a legacy Java version. Probably something to do with how I'm building the .class files and instantiating the JVM.

joshbooks commented 6 years ago

definitely to do with how you're instantiating the jvm, if you double click a .class file or some equivalent mac pops up that stupid message

joshbooks commented 6 years ago

I think the first way I got it to work outside Intellij was to build an app with platypus since apple doesn't like for any java thing to be the primary target of a .app

joshbooks commented 6 years ago

this stack overflow post gets it to build (if you make sure you have the right target for your machine) and adding the location of jvm.dll to PATH gets it to run (usually jre/bin/server)

joshbooks commented 6 years ago

so I inserted this into my cargo config (%USERPROFILE%/.cargo/config)

[target.x86_64-pc-windows-msvc.jvm]
rustc-link-lib = ["jvm"]
rustc-link-search = ["C:/Program Files/Java/jdk1.8.0_161/lib"]
root = "C:/Program Files/Java/jdk1.8.0_161/lib"

changed the Cargo.toml to this

[package]
name = "rjni"
version = "0.0.1"
authors = ["Ben Anderson <gravityscore@gmail.com>"]
links = "jvm"
build = "src/build.rs"

[dependencies]
libc = "*"

added C:\Program Files\Java\jre1.8.0_161\bin\server to my PATH env variable, and now it runs on my windows machine, you'll probably need to change some of those paths