|
| 1 | +[](https://github.com/exceljava/com4j/actions/workflows/build.yml) |
| 2 | +[](https://jitpack.io/#exceljava/com4j) |
| 3 | + |
| 4 | +About |
| 5 | +===== |
| 6 | + |
| 7 | +This is a fork of [kohsuke/com4j](https://github.com/kohsuke/com4j) and contains some changes necessary for calling the Excel COM API from Java. All changes are raised as a PR in the original repo, as the intention is that this repo is only needed temporarily. |
| 8 | + |
| 9 | +Using com4j |
| 10 | +=========== |
| 11 | + |
| 12 | +[Download com4j](https://github.com/exceljava/com4j/releases) or [access it from jitpack.io](https://jitpack.io/#exceljava/com4j) |
| 13 | + |
| 14 | +To add to a Maven project, add the following to your pom.xml (using the latest release tag for the `version`) |
| 15 | + |
| 16 | +```xml |
| 17 | +<repositories> |
| 18 | + <repository> |
| 19 | + <id>jitpack.io</id> |
| 20 | + <url>https://jitpack.io</url> |
| 21 | + </repository> |
| 22 | +</repositories> |
| 23 | + |
| 24 | +<dependency> |
| 25 | + <groupId>com.github.exceljava.com4j</groupId> |
| 26 | + <artifactId>com4j</artifactId> |
| 27 | + <version>release-20190528</version> |
| 28 | +</dependency> |
| 29 | +``` |
| 30 | + |
| 31 | +Building com4j |
| 32 | +============== |
| 33 | + |
| 34 | +com4j is divided into two parts, native code and Java. The native side (`com4j.dll`, x86 and x64) is built with CMake, but the *build* of the Java side does not build the native side - it just packages up the **prebuilt DLLs already committed to this repo**, under `bin/x86/<mode>/com4j.dll` and `bin/x64/<mode>/com4j.dll` (`<mode>` is `Release` or `Debug`; the Java build uses `Release` by default). |
| 35 | + |
| 36 | +This means a plain `mvn package` from the repository root works anywhere with just a JDK and Maven - no C++ toolchain required - which is what lets [jitpack.io](https://jitpack.io/#exceljava/com4j) build this project: JitPack builds from source on its own (Linux) servers, with no way to run a Windows/MSVC build, so it can only ever consume the committed DLLs, never rebuild them itself. |
| 37 | + |
| 38 | +Rebuilding the native DLLs |
| 39 | +-------------------------- |
| 40 | + |
| 41 | +You only need to do this if you've changed code under `native/`. It's opt-in - pass `-DbuildNative=true` - and requires: |
| 42 | + |
| 43 | +- Check out the git submodules that are linked (`git submodule update --init`). |
| 44 | +- [CMake](https://cmake.org/) (3.20+), on your `PATH`. |
| 45 | +- Visual Studio 2022 (or newer) with the "Desktop development with C++" workload, including the optional **C++ ATL** and **C++ MFC** components (both are used by the native project and aren't installed by default). |
| 46 | +- `JAVA_HOME` pointing at a full JDK (not a JRE) - the build needs its `include/`/`include/win32` JNI headers. Any modern JDK works for this. Note that some very old JDKs (e.g. JDK 8) have their own runtime incompatibility loading a DLL built by a modern MSVC toolchain and can crash when actually *running* code that loads it (see below), even though their headers compile fine. |
| 47 | + |
| 48 | +With those in place, rebuild the Release DLLs with: |
| 49 | + |
| 50 | +``` |
| 51 | +mvn generate-resources -DbuildNative=true |
| 52 | +``` |
| 53 | + |
| 54 | +or the Debug DLLs with: |
| 55 | + |
| 56 | +``` |
| 57 | +mvn generate-resources -DbuildNative=true -Dmode=Debug |
| 58 | +``` |
| 59 | + |
| 60 | +Either configures and builds both x86 and x64 with CMake, copying the results into `bin/x86/<mode>` and `bin/x64/<mode>`. **Commit the updated files under `bin/`** afterwards so everyone else - and JitPack - picks up the change; `mvn package`/`mvn install` on their own won't rebuild or re-copy them. |
| 61 | + |
| 62 | +To build the native side by itself, e.g. to iterate faster while working on it without going through the copy step every time, you can also drive CMake directly: |
| 63 | + |
| 64 | +``` |
| 65 | +cmake -G "Visual Studio 17 2022" -A x64 -S native -B native/cmake-build-x64 -DJAVA_HOME="C:/path/to/jdk" |
| 66 | +cmake --build native/cmake-build-x64 --config Release |
| 67 | +``` |
| 68 | + |
| 69 | +(swap `x64`/`-A x64` for `x86`/`-A Win32` for the 32-bit build, and `Release` for `Debug` as needed) |
| 70 | + |
| 71 | +Running the test suite |
| 72 | +----------------------- |
| 73 | + |
| 74 | +The `test` module exercises a small COM test object, `TestObject`, whose implementation only compiles into **Debug** builds of `com4j.dll`. The committed `bin/x64/Debug/com4j.dll` already has it; if you've changed native code, rebuild it first (see above). To run the tests: |
| 75 | + |
| 76 | +1. Register the Debug DLL as a COM server from an **elevated** command prompt (this needs administrator rights - Maven won't do this for you, and shouldn't): `regsvr32 bin\x64\Debug\com4j.dll`. Use `regsvr32 /u ...` to undo this later. |
| 77 | +2. Run the tests using `mvn test` |
| 78 | + |
| 79 | +If you switch which architecture's Debug DLL you use for step 1, re-register the other one and un-register the old one first. |
| 80 | + |
| 81 | +javah |
| 82 | +----- |
| 83 | +If you change the Java classes that define native methods, be sure to execute `native/run_javah.bat` to keep header files in sync |
0 commit comments