win8.1 cygwin compiles java lightweight virtual machine avian

Keywords: Java Windows Cygwin git

Original Link: http://www.cnblogs.com/Mr-Nobody/p/3855914.html

1. Background

Yesterday it was nice to see people writing local applets in aauto on the internet. I think if java's jre is smaller, it would be nice to write gadgets with java's huge third-party class libraries.I often write widget software with eclipse+some commons packages.Unfortunately, they can only be used by themselves, which can be a problem for people without a Java environment.So go online and find a lightweight Java virtual machine avian.However, many difficulties were encountered during the compilation and installation process, so we record them here.

 

2. Start compiling

Say less nonsense, but focus first.In fact, the gihub front page of this software and a clear description of the installation tutorial, but this is in English, and mainly because I have no experience in compiling open source programs, nor have I played with any msys,cygwin's linux compilation environment.This is also considered an exercise, since before I only had to go away with open source projects that I needed to compile myself.

There are actually two difficulties in installation, the first one being http://kingj.iteye.com/blog/1614892 This blog is broken.The win32/64 folder should be side by side with avian's decompressed folder.I always thought it was going to be merged, there were no special instructions on the official website, and I didn't play the linux command line, so...Say nothing more than tears.

The second is the installation of cygwin.The installation of this is doomed to be frustrating. Although there are tutorials all over the Internet, the problem is that there are many details of these tutorials that have not been explained and that have spared a lot of distance.Except what the general tutorial says

In addition to binutils, gcc, gcc-mingw, gdb, make, zlib should also be installed (the first time I installed it, I forgot to install avian's make according to the tutorial setup, vomiting blood.)).These are all 32-bit. If you want to install 64-bit, make, gcc-mingw-g++, mingw64-i686-gcc-g++, mingw64-x86_64-gcc-g++, mingw64-x86_64-zlib (these are actually described on avian's website, but zlib is not introduced.)Because it was written in the middle, I also saw it later. This layout is really unscientific.)Anyway, my computer is 64-bit, so I have these installed to avoid any problems.

 

The environment is ready and all you need is to follow the instructions on the website

 

Take 32-bit for example.Since 32-bit can be compiled under 64-bit, 64-bit compiles cannot run on 32-bit.

$ git clone https://The official website github.com/ReadyTalk/win32.git. /win32 #is downloading win64, we changed all to win32.But there's a problem with the GIT address on that official website, so it's okay to go to GitHub and download the zip copy yourself
$ export JAVA_HOME="/cygdrive/c/Program Files/Java/jdk1.7.0_45" #Introducing JAVA_HOME is equivalent to setpath of cmd under windows
$ make platform=windows arch=i386   #Note here that if you do not specify a compilation environment, the default is the native environment. If the computer is 64-bit, the win32 header files you downloaded before will conflict, so you must specify i386. If you want to compile 64-bit, the previous git clone should be 64-bit
$ build/windows-i386/avian -cp build/windows-i386/test Hello  #The successful running of this test program means that the compilation was successful.

  

 

You then embed and package the virtual machine, which has a detailed tutorial on github.Wait for me to tidy up later.

 

 

3. Package and embed virtual machines

# building
platform=windows
#make platform=windows arch=i386/x86_64
arch=i386
project=Hello #project name
mainClass=Hello #The class where the main function is located

#i686-w64-mingw32- x86_64-w64-mingw32- 
#This is a dumb place. It's not detailed on the official website.cygwin needs to install mingw-gcc... equivalent to a series of files to support 32-bit compilation
#If 32-bit compilation, ar,gcc,g++,dlltool,strip is prefixed with i686-w64-mingw32-
#If 64-bit compilation, prefix x86_64-w64-mingw32- 

cd /cygdrive/d/java/avian/avian #Go to avian's installation directory
export JAVA_HOME=/cygdrive/d/Java/jdk1.7.0_17

# The first time you need to compile avian through make directives, if you don't specify a platform, the default is the local platform.Note: In shell \ means line break
#make platform=${platform} arch=${arch}


#Embedding
#Before embedding, you need to write one through eclipse
mkdir ../projects/${project}
cd ../projects/${project}
i686-w64-mingw32-ar x ../../avian/build/${platform}-${arch}/libavian.a
#cp ../../avian/build/${platform}-${arch}/classpath.jar boot.jar

cat >Hello.java <<EOF
public class Hello {
  public static void main(String[] args) {
    System.out.println("hello, world!");
  }
}
EOF
javac -bootclasspath boot.jar Hello.java
#jar u0f boot.jar Hello.class
 ../../avian/build/${platform}-${arch}/binaryToObject/binaryToObject boot.jar boot-jar.o _binary_boot_jar_start _binary_boot_jar_end ${platform} ${arch}
cat >embedded-jar-main.cpp <<EOF
#include "stdint.h"
#include "jni.h"
#include "stdlib.h" 

#if (defined __MINGW32__) || (defined _MSC_VER)
#  define EXPORT __declspec(dllexport)
#else
#  define EXPORT __attribute__ ((visibility("default"))) \
  __attribute__ ((used))
#endif

#if (! defined __x86_64__) && ((defined __MINGW32__) || (defined _MSC_VER))
#  define SYMBOL(x) binary_boot_jar_##x
#else
#  define SYMBOL(x) _binary_boot_jar_##x
#endif

extern "C" {

  extern const uint8_t SYMBOL(start)[];
  extern const uint8_t SYMBOL(end)[];

  EXPORT const uint8_t*
  bootJar(unsigned* size)
  {
    *size = SYMBOL(end) - SYMBOL(start);
    return SYMBOL(start);
  }

} // extern "C"

extern "C" void __cxa_pure_virtual(void) { abort(); }

int
main(int ac, const char** av)
{
  JavaVMInitArgs vmArgs;
  vmArgs.version = JNI_VERSION_1_2;
  vmArgs.nOptions = 1;
  vmArgs.ignoreUnrecognized = JNI_TRUE;

  JavaVMOption options[vmArgs.nOptions];
  vmArgs.options = options;

  options[0].optionString = const_cast<char*>("-Xbootclasspath:[bootJar]");

  JavaVM* vm;
  void* env;
  JNI_CreateJavaVM(&vm, &env, &vmArgs);
  JNIEnv* e = static_cast<JNIEnv*>(env);

  jclass c = e->FindClass("${mainClass}");
  if (not e->ExceptionCheck()) {
    jmethodID m = e->GetStaticMethodID(c, "main", "([Ljava/lang/String;)V");
    if (not e->ExceptionCheck()) {
      jclass stringClass = e->FindClass("java/lang/String");
      if (not e->ExceptionCheck()) {
        jobjectArray a = e->NewObjectArray(ac-1, stringClass, 0);
        if (not e->ExceptionCheck()) {
          for (int i = 1; i < ac; ++i) {
            e->SetObjectArrayElement(a, i-1, e->NewStringUTF(av[i]));
          }

          e->CallStaticVoidMethod(c, m, a);
        }
      }
    }
  }

  int exitCode = 0;
  if (e->ExceptionCheck()) {
    exitCode = -1;
    e->ExceptionDescribe();
  }

  vm->DestroyJavaVM();

  return exitCode;
}
EOF


i686-w64-mingw32-g++ -fno-exceptions -fno-rtti -I"$JAVA_HOME/include" -I"$JAVA_HOME/include/win32" -D_JNI_IMPLEMENTATION_ -c embedded-jar-main.cpp -o main.o
	 
i686-w64-mingw32-dlltool -z ${project}.def *.o
i686-w64-mingw32-dlltool -d ${project}.def -e ${project}.exp
i686-w64-mingw32-gcc ${project}.exp *.o -L../../win32/lib -lmingwthrd -lz -lws2_32 \
    -lIphlpapi -mwindows -mconsole -o ${project}.exe
i686-w64-mingw32-strip --strip-all ${project}.exe

The eclipse project needs to remove the jre, then import avian/build/windows-xxx/classpath.jar to export the runnable jar package.

Copy to the project directory after exporting, rename it boot.jar. Then copy the list of commands to the command line window of cygwin for execution

 

4.swt configuration

There is a zip package for swt on avian's official website and it's also described on the web, but it's important to note that there can only be one dll file in the jar package when you install it, which may appear to be conflicting.

 

Reprinted at: https://www.cnblogs.com/Mr-Nobody/p/3855914.html

Posted by rubio on Wed, 24 Jul 2019 12:46:16 -0700