Java虚拟机(JamVM)源码剖析-(1)环境搭建(mac)
1. 环境搭建(Mac)
1.1 准备工作
mac在第一次安装时,会提示安装失败,是因为权限问题,打开设置 ->Security -> 会提示你一个oracle啥啥啥的,alow一下就行,然后重新安装即可。
准备jdk 我用的jdk是 jdk-8u11-linux-x64.tar.gz
下载classpath 我用的是classpath-0.99.tar.gz
1.2 安装、启动虚拟机
mkdir centos-vm # 创建目录
cd centos-vm # 进入目录
# 将下载好的classpath压缩包、antlr的jar包、jamvm源码都放在当前目录,可以映射到虚拟机的/vagrant目录
ls
antlr-4.7.1-complete.jar centos-7.0-x86_64.box classpath-0.99.tar.gz jamvm-code jdk-8u11-linux-x64.tar.gz Vagrantfile
# 注意了,创建的目录 centos-vm 在虚拟机里面被映射成/vagrant
vagrant init centos7.0 ./centos-7.0-x86_64.box # 创建一个虚拟机
A `Vagrantfile` has been placed in this directory. You are now
ready to `vagrant up` your first virtual environment! Please read
the comments in the Vagrantfile as well as documentation on
`vagrantup.com` for more information on using Vagrant.
# 输出上面表明创建成功
vagrant up # 启动虚拟机
Bringing machine 'default' up with 'virtualbox' provider...
==> default: Box 'centos7.0' could not be found. Attempting to find and install...
default: Box Provider: virtualbox
default: Box Version: >= 0
==> default: Box file was not detected as metadata. Adding it directly...
==> default: Adding box 'centos7.0' (v0) for provider: virtualbox
default: Unpacking necessary files from: file:///Users/gonzo/projects/study/jdk/jamvm/centos-vm/centos-7.0-x86_64.box
==> default: Successfully added box 'centos7.0' (v0) for 'virtualbox'!
==> default: Importing base box 'centos7.0'...
==> default: Matching MAC address for NAT networking...
==> default: Setting the name of the VM: centos-vm_default_1615645582766_29724
==> default: Clearing any previously set forwarded ports...
==> default: Clearing any previously set network interfaces...
==> default: Preparing network interfaces based on configuration...
default: Adapter 1: nat
==> default: Forwarding ports...
default: 22 (guest) => 2222 (host) (adapter 1)
==> default: Booting VM...
==> default: Waiting for machine to boot. This may take a few minutes...
default: SSH address: 127.0.0.1:2222
default: SSH username: vagrant
default: SSH auth method: private key
default:
default: Vagrant insecure key detected. Vagrant will automatically replace
default: this with a newly generated keypair for better security.
default:
default: Inserting generated public key within guest...
default: Removing insecure key from the guest if it's present...
default: Key inserted! Disconnecting and reconnecting using new SSH key...
==> default: Machine booted and ready!
==> default: Checking for guest additions in VM...
default: The guest additions on this VM do not match the installed version of
default: VirtualBox! In most cases this is fine, but in rare cases it can
default: prevent things such as shared folders from working properly. If you see
default: shared folder errors, please make sure the guest additions within the
default: virtual machine match the version of VirtualBox you have installed on
default: your host and reload your VM.
default:
default: Guest Additions Version: 4.3.28
default: VirtualBox Version: 6.1
==> default: Mounting shared folders...
default: /vagrant => /Users/gonzo/projects/study/jdk/jamvm/centos-vm
# 输出上面这些表明创建成功
vagrant ssh # 进入虚拟机
注:其中 ./centos-7.0-x86_64.box镜像,是我下载好的
下载地址 http://www.vagrantbox.es/ 选择仔细熟悉的系统
启动完成之后,设置jdk环境
这一步就跳过了 网上很多
我用的jdk是
jdk-8u11-linux-x64.tar.gz
好了,环境准备好了,接下来开始编译。
1.3 编译
解压、安装classpath
tar -zxvf classpath-0.99.tar.gz # 解压classpath
export ANTLR_JAR=/vagrant/antlr-4.7.1-complete.jar # 设置antlrjar包的环境变量 编译时候需要的 不加这个会报错
cd classpath-0.99 # 进入解压好的classpath
./configure --disable-gtk-peer --disable-gconf-peer --disable-plugin # 开始构建 会检测环境啊啥的 linux根据实际情况需要加--build=x86_64 来标注系统类型
# 如果没有出现报错 继续执行 有啥错误的话,就安装对应的依赖即可 如果使用的是和我一样的镜像这一步应该没问题
make -i # make
# 这一步会输出很多日志,稍微要点时间
sudo make -i install # 安装
开始编译jamvm源码
./autogen.sh # 运行源码下的脚本
# 如果用的是和我一样的镜像,会提示下面这些依赖错误
**Error**: You must have `libtool' installed to compile JamVM.
Install the appropriate package for your distribution,
or get the source tarball at http://ftp.gnu.org/gnu/libtool
**Error**: You must have `autoconf' installed to compile JamVM.
Install the appropriate package for your distribution,
or get the source tarball at http://ftp.gnu.org/gnu/autoconf
**Error**: You must have `automake' installed to compile JamVM.
Install the appropriate package for your distribution,
or get the source tarball at http://ftp.gnu.org/gnu/automake
sudo yum install libtool # 安装 这一步安装会把三个依赖全安装了...
# 继续执行 ./autogen.sh 如果还有其他依赖,则继续安装即可
./configure CFLAGS=-g # 配置 --with-java-runtime-library=openjdk8 使用jdk8的runtime包 可以支持lambda表达式
make # make
sudo yum install zip # 如果用的是和我一样的镜像 会提示zip命令不存在,安装即可
# 继续执行 make 输出很多日志巴拉巴拉的,缺啥依赖就安装 如果用我的镜像,应该不会有缺的了
sudo make install # 安装
在这一步,可以通过 ./configure --help 来获取所有可选的配置
根据实际情况,这里可以设置要支持的jdk版本我记得
接下来写个java代码开始验证
echo "public class HelloWorld { public static void main(String[] args) { System.out.println(\"Hello World\"); } }" > HelloWorld.java
javac HelloWorld.java
历史性的一刻
/usr/local/jamvm/bin/jamvm HelloWorld
# 输出 hello world 即正常编译
至此,编译结束。