Many a time, I wished I had a utility that would read my projects mpp File and automatically alerted me about my tasks. Had created such a utilitly and this post desribes the process of reading MS Project Files in Java using MPXJ and Apache POI library.
MPXJ - MPXJ is the java library used for parsing MS Project files. The lib depends upon Apache POI package for providing the functionality. MPXJ supports all the three formats for MS Project. The details can be found at the Home Page for MPXJ provided in references section
Let's start implementing. The code to read the File is fairly simple.
Β To read an MS Project file, we use projectReaderUtility class to get the instance of a ProjectReader, based on the extension. ProjectReader is the interface implemented by the classes that can read an MS Project file in mpxj library. Once we get the instance of ProjectReader, we need to invoke read API, passing the file name to be read. The API returns an instance of ProjectFile, containing all the details in the MS Project File.Β
More about ProjectFile - ProjectFile is the class in mpxj, which contains all the information about an MS Project file. It has API's to access all Task details, as well other API's to extract and manipulate other metadata.
Is that it? The basic objective of reading is done. Now we can use the provided API to iterate and create applications to suite our needs. For example, to list all the tasks, use getAllTasks(), it returns a List of Tasks. Custom logic can be applied to manipulate this list as desired, like filtering the tasks based on resources or date.
What's next?
I am working on the prototype to read the MS Project File and update the Google Calendar with Task details, based on Start Date.
References
Hai,
It was very usefull post..
And i’ve a problem. π
Where we have to put the mpp file? Shall we give a file path there?
Please reply..
Thanks..
Regards,
Purathani
You can specify the full path of the file. Alternatively, you provide an instance of java.io.File or InputStream class as well. Please refer to API doc for exact syntax.
Please revert back if need more inputs
Hi ashish,
I have a simillar prob hope u can help me out in reading the mpp file using java. The prob which im facing is tht its saying the ProjectReader is not present in the MPXJ library. Can u please provide me the solution for reading the mpp file and resolve this error.
Please help me out. Thanks in advance.
Regards,
Basu
Hi,
This is the exception im getting when i read the file.
Exception in thread “main” java.lang.NoSuchMethodError: org.apache.poi.poifs.filesystem.POIFSFileSystem.getRoot()Lorg/apache/poi/poifs/filesystem/DirectoryEntry;
at com.tapsterrock.mpp.MPPFile.read(MPPFile.java:129)
at com.tapsterrock.mpx.MPXFile.read(MPXFile.java:363)
at com.tapsterrock.mpx.MPXFile.(MPXFile.java:144)
at com.tapsterrock.mpp.MPPFile.(MPPFile.java:94)
at POIFSExample1.run(POIFSExample1.java:31)
at POIFSExample1.main(POIFSExample1.java:140)
Just i have done nothing only wrote the following code for which im getting the above error.
MPPFile mpp = new MPPFile(“D:\MMPT_Project\Project1.mpp”);
ArrayList arLst = mpp.getTables();
for(int i=0;i<arLst.size();i++){
System.out.println(“mpp file : “+ arLst.get(i));
}
Please help me as soon as possible
Thanks in advance
Regards,
Basu
Thanks alot for your reply..
I’ve tried with the java.io.file also..
But i’ve got some exception errors.
Can you please do me a favour?
If you have any sample codes to parse the MS project file, can you post me please?
Thanks in advance
Regards,
Purathani
Basu! please check the version of the apache poi you are using. Match the one as expected by MPXJ version and this problem shall be resolved.
ashish
Purathani, the sample code is also available with the mpxj library. Try the samples provided with the mpxj library, source is available. You can easily see them in Action. Let me know if it still doesn’t work.
ashish
Hi,
POI version is 3.2 , but i don’t know which version the mpxj.jar belongs to. If possible can u pls let me know which of the versions of POI and MPXJ are compantible with each other. So tht it would be helpful for me.
Thanks
Basu
Download mpxj again, all the required jars are present in its lib folder π Keep life simple
Ok.. Thanks.. π
I’ll try and will see them.
Purathani
This is the code to access the mpp file
File file = new File(“D:\MMPT_Project\Project1.mpp”);
MPPReader mppRead = new MPPReader();
System.out.println(“mpp reader : “+ mppRead.toString());
ProjectFile pf = mppRead.read(file);
/*
The above statement can also be write as
ProjectFile pf = mppRead.read(“path of the mpp file”);
*/
System.out.println(“proj file : “+ pf.getMppFileType());
List lstTask = pf.getAllTasks();
System.out.println(“tasks count:: “+ lstTask.size());
for(int i=1;i<lstTask.size();i++){
System.out.println(“tasks one:: “+ lstTask.get(i).getName() + ” —- “+ lstTask.get(i).getUniqueID() +” —– “+lstTask.get(i).getStart() + ” —– “+ lstTask.get(i).getID());
System.out.println(“tasks second :: “+ lstTask.get(i).getText2() + ” —- “+ lstTask.get(i).getFinish() +” —- “+lstTask.get(i).getPercentageComplete());
}
Hope this will solve the problem
set the classpath of mpxj.jar(2.0) and poi-3.0.2-FINAL-20080204.jar
which comes with the mpxj folder when downloaded.
don’t forget the classpath setting
π Basu
Basu,
Kindly send me the mpxj.jar(2.0)
Thanks
Rajpal
Hai,
Thanks for your help..
But i’m still getting this exception error.
Exception in thread “main” java.lang.NoSuchMethodError: org.apache.poi.poifs.filesystem.POIFSFileSystem.getRoot()Lorg/apache/poi/poifs/filesystem/DirectoryEntry;
at net.sf.mpxj.mpp.MPPReader.read(MPPReader.java:61)
at net.sf.mpxj.reader.AbstractProjectReader.read(AbstractProjectReader.java:69)
at Mppparser.MppParser.main(MppParser.java:55)
I’m trying to solve this.
If you have any idea, please help me π
Purathani.
Hi
Please download mpxj-2.1.0 after which u need to use the following import statements
import net.sf.mpxj.ProjectFile;
import net.sf.mpxj.Task;
import net.sf.mpxj.mpp.*;
I think u are getting the following import statement
import com.tapsterrock.mpp.*;
import com.tapsterrock.mpx.*;
which are not correct as i was also getting the same error before but bu using the first import statement as specified above solved the issue.
Please see that once again
I hope u will get through tht easily
Regards
Basu
Hi
Please download mpxj-2.1.0 after which u need to use the following import statements
import net.sf.mpxj.ProjectFile;
import net.sf.mpxj.Task;
import net.sf.mpxj.mpp.*;
I think u are getting the following import statement
import com.tapsterrock.mpp.*;
import com.tapsterrock.mpx.*;
which are not correct as i was also getting the same error before but bu using the first import statement as specified above solved the issue.
Please see that once again
I hope u will get through tht easily
Also add all the jar files given in tht mpxj-2.1.0.zip file present in the lib directory along with the mpxj.jar and
poi-3.0.2-FINAL-20080204.jar
Regards
Basu
I’ve tried before with the other version of poi jar file. This poi-3.0.2-FINAL-20080204.jar version is okey to parse the file now.
Thanks alot for your help, Basu.
π
Regards,
Purathani.
ashish,
very good post.
I’am starting using this api and I’ve a question about if the Task classes are live or not. By live I mean that if you remove a subtask from a task, the “father task” gets all his information like cost and duration updated “on line”. Is that what’s happend?, Or you have to update “manually” suchs information about the “father task”?
Regards.
Thanks! I don’t have the answer right now, but working on my next post on Task class details. So will take care of your comment. Will try to post it in 2-3 days
Ashish,
after I’ve been working with this api a couple days, I found that in fact the properties of the “father task” doesn’t get updated, i’ve to update them manually using for example father.setDuration(), father.setWork() , father.setStart() or father.setFinish(). It would be very good that the father task get his properties updated after a call to remove() or add() method. It seems that the api gives a few classes in net.sf.mpxj.listener package that able to trigger actions when change are performed in some task, resource or project file. Maybe the solution goes in that direction.
Regards.
Does anybody have an example code for updating an mpp file using task.set ?
I update the task successfully, I can read it using task,get and the update is correct, however when I open the mpp file the updates are not there.
Am I missing something such as commit or close?
Thanks
Nicolas
hi ,
i m using mpxj and poi library and i want to display the ms project on webpage can anyone help me out, i tried it by using jsp but it is giving error as “ProjectReader cannot be resolved to a type” please help me out.
hello
i saw the code written above to upload the MPP file and also to download jar file mpxj.jar(2.0) but how to set the classpath and in which folder shoudh we keep this downloaded file please inform me soon its urgent plss
thanks
Hi .. when I run this application I get these errors, pls help thanks
Exception in thread “main” java.lang.Error: Unresolved compilation problems:
The method getUniqueID() is undefined for the type File
The method getStart() is undefined for the type Object
The method getID() is undefined for the type Object
The method getText2() is undefined for the type Object
The method getFinish() is undefined for the type Object
The method getPercentageComplete() is undefined for the type Object
The method getUniqueID() is undefined for the type Object
The method getStart() is undefined for the type Object
The method getID() is undefined for the type Object
Some jar mismatch is happening. Please check the versions.