最近學習java,想起來沒有在Ubuntu上使用過
因此想安裝下環境,並在Ubuntu上試用MPXJ讀取mpp文件
1.安裝
下載jdk8
https://www.oracle.com/technetwork/java/javase/downloads/jdk8-downloads-2133151.html
新建目錄
mkdir /usr/lib/jvm
解壓
tar -zxvf jdk-8u211-linux-x64.tar.gz -C /usr/lib/jvm
修改環境變量
vim ~/.bashrc
追加
#set oracle jdk environment export JAVA_HOME=/usr/lib/jvm/jdk1.8.0_211 export JRE_HOME=${JAVA_HOME}/jre export CLASSPATH=.:${JAVA_HOME}/lib:${JRE_HOME}/lib export PATH=${JAVA_HOME}/bin:$PATH
使生效
source ~/.bashrc
2.測試
(1)是否安裝成功

(2)Hello.java
public class Hello { private Integer aa = 1; public String ss = "sss"; public static void main(String[] args) { System.out.println("Hello Java"); } }
javac Hello.java

java Hello

javap Hello

3.用MPXJ讀取mpp文件
在http://www.mpxj.org/官網MPXJ in Java,進入下載頁
添加MPXJ的包
在/usr/lib/jvm/jdk1.8.0_211/jre/lib/ext下添加下面的jar包(我的安裝目錄在/usr/lib/jvm/jdk1.8.0_211下)
mpxj.jar commons-collections4-4.1.jar poi-3.17.jar rtfparserkit-1.10.0.jar
在/www/javatest下添加mpxjtest.java
import net.sf.mpxj.mpp.*; import net.sf.mpxj.MPXJException; import net.sf.mpxj.Resource; import net.sf.mpxj.ResourceAssignment; import net.sf.mpxj.Task; import net.sf.mpxj.ProjectFile; import java.util.List; import java.io.*; public class mpxjtest { public static void main(String[] args) throws MPXJException { File file = new File("/www/javatest/aa.mpp"); MPPReader mppRead = new MPPReader(); ProjectFile pf = mppRead.read(file); List tasks = pf.getAllTasks(); mpxjtest mt = new mpxjtest(); System.out.println("總任務數: " + tasks.size()); System.out.println("ID|任務名|資源|開始時間|結束時間|完成百分比"); for (int i = 1; i < tasks.size(); i++) { System.out.println(((Task) tasks.get(i)).getUniqueID() + "|" + ((Task) tasks.get(i)).getName()); } } public String getResource(Task task) { StringBuffer buf = new StringBuffer(); List assignments = task.getResourceAssignments(); for (int i = 0; i < assignments.size(); i++) { ResourceAssignment assignment = (ResourceAssignment) assignments .get(i); Resource resource = assignment.getResource(); if (resource != null) { buf.append(resource.getName()).append(" "); } } return buf.toString(); } }
運行
javac mpxjtest.java
java mpxjtest
結果

說明:
還可以通過ppa(源) 方式安裝
add-apt-repository ppa:webupd8team/java apt-get update apt-get install oracle-java8-installer
