I am getting a NullPointerException when trying to open excel from SWT. Sadly I can't post the full stacktrace and some of the code because it has references to the company that I work for. Hopefully someone has run into this issue before and might recognize it.
Here is the part of the stacktrace that I can post
java.lang.NullPointerException
at org.eclipse.swt.ole.win32.OleControlSite.disconnectEventSinks(OleControlSite.java:468)
at org.eclipse.swt.ole.win32.OleControlSite.releaseObjectInterfaces(OleControlSite.java:774)
at org.eclipse.swt.ole.win32.OleClientSite.onDispose(OleClientSite.java:909)
at org.eclipse.swt.ole.win32.OleClientSite.access$1(OleClientSite.java:895)
at org.eclipse.swt.ole.win32.OleClientSite$1.handleEvent(OleClientSite.java:129)
Here is the code. It's on the last line that the exception gets thrown, when a new OleControlSite is instantiated.
OleFrame frame1 = new OleFrame(shell, SWT.NONE);
if (clientSite != null && !clientSite.isDisposed()){
clientSite.dispose();
clientSite = null;
}
OleAutomation doc;
try{
clientSite = new OleControlSite(frame1, SWT.NONE, file);
This code works in windows XP, but not in windows 7 it throws the NullPointerException.
As per cubic suggestion I created a self contained example here is the code.
package com.test;
import java.io.File;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.ole.win32.OLE;
import org.eclipse.swt.ole.win32.OleAutomation;
import org.eclipse.swt.ole.win32.OleControlSite;
import org.eclipse.swt.ole.win32.OleEvent;
import org.eclipse.swt.ole.win32.OleFrame;
import org.eclipse.swt.ole.win32.OleListener;
import org.eclipse.swt.ole.win32.Variant;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Button;
public class OpenExcelExampleWindow extends Shell {
public final static int WorkbookBeforeClose = 0x00000622;
/**
* Launch the application.
* #param args
*/
public static void main(String args[]) {
try {
Display display = Display.getDefault();
OpenExcelExampleWindow shell = new OpenExcelExampleWindow(display);
shell.open();
shell.layout();
while (!shell.isDisposed()) {
if (!display.readAndDispatch()) {
display.sleep();
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* Create the shell.
* #param display
*/
public OpenExcelExampleWindow(Display display) {
super(display, SWT.SHELL_TRIM);
Button btnOpenExcel = new Button(this, SWT.NONE);
btnOpenExcel.setBounds(10, 10, 68, 23);
btnOpenExcel.setText("open excel");
btnOpenExcel.addSelectionListener(new SelectionAdapter() {
#Override
public void widgetSelected(SelectionEvent e) {
onOpenExcelClicked();
}
});
createContents();
}
protected void onOpenExcelClicked() {
openExcel("testfile.xls",this);
}
/**
* Create contents of the shell.
*/
protected void createContents() {
setText("SWT Application");
setSize(450, 300);
}
#Override
protected void checkSubclass() {
// Disable the check that prevents subclassing of SWT components
}
protected void openExcel(String fileName, Shell shell){
OleControlSite clientSite = null;
final File file = new File(fileName);
if (file.exists()) {
OleFrame frame1 = new OleFrame(shell, SWT.NONE);
if (clientSite != null && !clientSite.isDisposed()){
clientSite.dispose();
clientSite = null;
}
clientSite = new OleControlSite(frame1, SWT.NONE, file);
OleAutomation doc = new OleAutomation(clientSite);
int [] dispInfo = doc.getIDsOfNames(new String[] {"Application"});
Variant variant = doc.getProperty(dispInfo[0]);
OleAutomation app = variant.getAutomation();
variant.dispose();
doc.dispose();
doc = null;
int result = clientSite.doVerb(OLE.OLEIVERB_OPEN);
if (result != OLE.S_OK){
OLE.error(OLE.ERROR_CANNOT_OPEN_FILE, result);
}
//When user close workbook, dispose the clientSite.
clientSite.addEventListener(app, "{00024413-0000-0000-C000-000000000046}",
WorkbookBeforeClose,new OleListener() {
public void handleEvent(OleEvent event) {
OleControlSite oldControlSite = (OleControlSite)event.widget;
if ( !oldControlSite.isDisposed()){
//System.out.println("event in WorkbookBeforeClose");
oldControlSite.dispose();
}
}
});
}
}
}
And here is the exception
java.lang.NullPointerException
at org.eclipse.swt.ole.win32.OleControlSite.disconnectEventSinks(OleControlSite.java:468)
at org.eclipse.swt.ole.win32.OleControlSite.releaseObjectInterfaces(OleControlSite.java:774)
at org.eclipse.swt.ole.win32.OleClientSite.onDispose(OleClientSite.java:909)
at org.eclipse.swt.ole.win32.OleClientSite.access$1(OleClientSite.java:895)
at org.eclipse.swt.ole.win32.OleClientSite$1.handleEvent(OleClientSite.java:129)
at org.eclipse.swt.widgets.EventTable.sendEvent(EventTable.java:84)
at org.eclipse.swt.widgets.Widget.sendEvent(Widget.java:1053)
at org.eclipse.swt.widgets.Widget.sendEvent(Widget.java:1077)
at org.eclipse.swt.widgets.Widget.sendEvent(Widget.java:1058)
at org.eclipse.swt.widgets.Widget.release(Widget.java:808)
at org.eclipse.swt.widgets.Widget.dispose(Widget.java:446)
at org.eclipse.swt.ole.win32.OleClientSite.<init>(OleClientSite.java:194)
at org.eclipse.swt.ole.win32.OleControlSite.<init>(OleControlSite.java:96)
at com.test.OpenExcelExampleWindow.openExcel(OpenExcelExampleWindow.java:93)
at com.test.OpenExcelExampleWindow.onOpenExcelClicked(OpenExcelExampleWindow.java:65)
at com.test.OpenExcelExampleWindow$1.widgetSelected(OpenExcelExampleWindow.java:57)
at org.eclipse.swt.widgets.TypedListener.handleEvent(TypedListener.java:248)
at org.eclipse.swt.widgets.EventTable.sendEvent(EventTable.java:84)
at org.eclipse.swt.widgets.Widget.sendEvent(Widget.java:1053)
at org.eclipse.swt.widgets.Display.runDeferredEvents(Display.java:4169)
at org.eclipse.swt.widgets.Display.readAndDispatch(Display.java:3758)
at com.test.OpenExcelExampleWindow.main(OpenExcelExampleWindow.java:33)
Update:
I created an entirely new project with maven and just added one dependency for eclipse sdk 4.2 here is my pom file you can see there is only one dependency. The code in the project is the same as the class above.
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.test</groupId>
<artifactId>excelopen</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>
<name>excelopen</name>
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-eclipse-plugin</artifactId>
<version>2.8</version>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>org.eclipse.swt</groupId>
<artifactId>swt-win32-x86_64</artifactId>
<version>4.2_3.100.0.v4233d</version>
<type>jar</type>
<scope>compile</scope>
</dependency>
</dependencies>
</project>
I noticed that the source code for the OleControlSite class is the same from 3.8 sdk to the 4.2 sdk, which makes it unlikely that it's the SDK version that's causing the issue for me.
Update:
I download the SWT Source code and the exception get's thrown at line 392 of OleClientSite at this line.
if (result != COM.S_OK) OLE.error(OLE.ERROR_CANNOT_CREATE_OBJECT, result)
And the hidden exception is
org.eclipse.swt.SWTException: Failed to create Ole Client. result = -2147221164
at org.eclipse.swt.ole.win32.OLE.error(OLE.java:302)
at org.eclipse.swt.ole.win32.OleClientSite.OleCreate(OleClientSite.java:392)
at org.eclipse.swt.ole.win32.OleClientSite.<init>(OleClientSite.java:192)
at org.eclipse.swt.ole.win32.OleControlSite.<init>(OleControlSite.java:96)
at com.test.OpenExcelExampleWindow.openExcel(OpenExcelExampleWindow.java:93)
at com.test.OpenExcelExampleWindow.onOpenExcelClicked(OpenExcelExampleWindow.java:65)
at com.test.OpenExcelExampleWindow$1.widgetSelected(OpenExcelExampleWindow.java:57)
at org.eclipse.swt.widgets.TypedListener.handleEvent(TypedListener.java:248)
at org.eclipse.swt.widgets.EventTable.sendEvent(EventTable.java:84)
at org.eclipse.swt.widgets.Widget.sendEvent(Widget.java:1053)
at org.eclipse.swt.widgets.Display.runDeferredEvents(Display.java:4169)
at org.eclipse.swt.widgets.Display.readAndDispatch(Display.java:3758)
at com.test.OpenExcelExampleWindow.main(OpenExcelExampleWindow.java:33)
Update 09/05 9:35am
Thanks to favonius's information I was able to find out some more information. I found this discussion about 32bit com's on a 64bit windows OS. This is where the 32bit com on 64bit is discussed.
http://www.eclipse.org/forums/index.php/mv/msg/264018/763345/
This might be my issue as we are running Office 2007 which only comes in 32bit and it's running on a 64bit OS. I would like to check the registry but I don't know what key's I should look up. Does anyone know what the registry key is?
Update 09/05 9:45am
Here are the results of the TestCode class that favonius provided.
Run 1 with line 57 commented
{00020820-0000-0000-C000-000000000046}
Excel.Sheet.8
org.eclipse.swt.SWTException: Failed to create Ole Client. result = -2147221164
at org.eclipse.swt.ole.win32.OLE.error(OLE.java:302)
at com.test.TestCode.check(TestCode.java:62)
at com.test.TestCode.main(TestCode.java:23)
Run 2 line 57 uncommented
{00020820-0000-0000-C000-000000000046}
Excel.Sheet.8
Update 09/05 1:51pm
Our app outputs an xls file for excel to open. I just tried reading in an xlsx file and it works. This is a reasonable change to fix the problem with this specific app. Is it not supposed to open xls files?
Dissecting the Problem
As per your last update, where you mentioned the origin of the exception, the problem lies in the following lines.
int /*long*/[] ppv = new int /*long*/[1];
result = COM.CoCreateInstance(appClsid, 0, COM.CLSCTX_INPROC_HANDLER | COM.CLSCTX_INPROC_SERVER, COM.IIDIUnknown, ppv);
if (result != COM.S_OK) OLE.error(OLE.ERROR_CANNOT_CREATE_OBJECT, result);
Now the second line in the above code is trying create a single uninitialized object of the class associated with a specified CLSID. Now in your case you are directly providing the XLSX file path, and therefore it uses the GetClassFile() which returns the CLSID associated with the specified file name. Now if it would have failed then you should have gotten the exception at line 186 of OleClientSite. Also, you are able to get a proper program id from the file class id otherwise an exception at line 189 would have resulted.
Checking the Error Code
In your stacktrace you got an error code -2147221164. Now to check what it really means. Start up your Visual Studio and select from menu Tools -> Error Lookup. Put the error code there and click lookup. And it gives...
Now the question is if the class is not registered then you should have got this problem while getting the program id from the class id at line 189. And this makes thing weird. Therefore, before proceeding ahead follow the Second Check.
As per MSDN entry of CoCreateInstance, the above error message could also mean thatthe type of server you requested in the CLSCTX enumeration is not registered or the values for the server types in the registry are corrupt.
Second Check
As a second check and to nail the real problem, just try this SWT Snippet and update the result in your question.
Third Check
I have written this simple test class. Try to run it and post the console output. Also, uncomment the line 57 and run again and post its output too.
Note - You have to change the File constructor parameter.
import java.io.File;
import org.eclipse.swt.internal.ole.win32.COM;
import org.eclipse.swt.internal.ole.win32.GUID;
import org.eclipse.swt.internal.win32.OS;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.ole.win32.OLE;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
public class TestCode
{
public static void main(String[] args)
{
Display display = new Display();
Shell shell = new Shell(display);
shell.setText("Excel Example");
shell.setLayout(new FillLayout());
try{
check(new File("output.xlsx"));
}catch(Exception e){
e.printStackTrace();
}
shell.open();
while (!shell.isDisposed()) {
if (!display.readAndDispatch())
display.sleep();
}
display.dispose();
}
static void check(File file)
{
GUID fileClsid = new GUID();
char[] fileName = (file.getAbsolutePath()+"\0").toCharArray();
int result = COM.GetClassFile(fileName, fileClsid);
if (result != COM.S_OK)
OLE.error(OLE.ERROR_INVALID_CLASSID, result);
System.out.println(fileClsid);
String progID = getProgID(fileClsid);
if (progID == null)
OLE.error(OLE.ERROR_INVALID_CLASSID, result);
System.out.println(progID);
int [] ppv = new int[1];
int server_type = COM.CLSCTX_INPROC_HANDLER | COM.CLSCTX_INPROC_SERVER;
//server_type |= COM.CLSCTX_LOCAL_SERVER;
result = COM.CoCreateInstance(fileClsid, 0, server_type, COM.IIDIUnknown, ppv);
if (result != COM.S_OK)
OLE.error(OLE.ERROR_CANNOT_CREATE_OBJECT, result);
}
static String getProgID(GUID clsid)
{
if (clsid != null)
{
int [] lplpszProgID = new int [1];
if (COM.ProgIDFromCLSID(clsid, lplpszProgID) == COM.S_OK)
{
int hMem = lplpszProgID[0];
int length = OS.GlobalSize(hMem);
int ptr = OS.GlobalLock(hMem);
char[] buffer = new char[length];
COM.MoveMemory(buffer, ptr, length);
OS.GlobalUnlock(hMem);
OS.GlobalFree(hMem);
String result = new String(buffer);
int index = result.indexOf("\0");
return result.substring(0, index);
}
}
return null;
}
}
Related
I'm trying to make a simple graph using java but keep getting error
Code:
public class PlantUMLDemoMain {
public static void main(String[] args) throws Exception {
generateFromStringSource(new File("from-string.png"));
generateFromApi(new File("from-api.png"));
}
private static void generateFromApi(File file) throws IOException {
// 1. setup:
SequenceDiagramFactory f = new SequenceDiagramFactory();
SequenceDiagram diagram = f.createEmptyDiagram();
// 2. Build the diagram:
// "Bob -> Alice : hello"
// See net.sourceforge.plantuml.sequencediagram.command.CommandArrow#executeArg
Display bobD = Display.getWithNewlines("Bob");
Participant bobP = diagram.getOrCreateParticipant("Bob", bobD);
Display aliceD = Display.getWithNewlines("Alice");
Participant aliceP = diagram.getOrCreateParticipant("Alice", aliceD);
Display label = Display.getWithNewlines("hello");
ArrowConfiguration config = ArrowConfiguration.withDirectionNormal();
Message msg = new Message(bobP, aliceP, label, config, diagram.getNextMessageNumber());
checkState(null == diagram.addMessage(msg));
// 3. Output the diagram
// See net.sourceforge.plantuml.SourceStringReader#generateImage
diagram.makeDiagramReady();
checkState(1 == diagram.getNbImages());
try (OutputStream os = new FileOutputStream(file)) {
ImageData imageData = diagram.exportDiagram(os, 0, new FileFormatOption(FileFormat.PNG));
System.out.println("generateFromApi: " + diagram.getDescription().getDescription());
}
}
private static void generateFromStringSource(File file) throws IOException {
String source = "#startuml\n";
source += "Bob -> Alice : hello\n";
source += "#enduml\n";
StringBuffer stringBuffer = new StringBuffer();
SourceStringReader reader = new SourceStringReader(source);
// Write the first image to "png"
String desc = reader.generateImage(file);
// Return a null string if no generation
System.out.println("generateFromStringSource: " + desc);
}
}
Error: Exception in thread "main" java.lang.IllegalAccessError: class net.sourceforge.plantuml.png.PngIOMetadata (in unnamed module #0x9597028) cannot access class com.sun.imageio.plugins.png.PNGMetadata (in module java.desktop) because module java.desktop does not export com.sun.imageio.plugins.png to unnamed module #0x9597028
at net.sourceforge.plantuml.png.PngIOMetadata.writeWithMetadata(PngIOMetadata.java:60)
at net.sourceforge.plantuml.png.PngIO.write(PngIO.java:86)
at net.sourceforge.plantuml.png.PngIO.write(PngIO.java:80)
at net.sourceforge.plantuml.ugraphic.g2d.UGraphicG2d.writeImageTOBEMOVED(UGraphicG2d.java:219)
at net.sourceforge.plantuml.ugraphic.ImageBuilder.writeImageInternal(ImageBuilder.java:249)
at net.sourceforge.plantuml.ugraphic.ImageBuilder.writeImageTOBEMOVED(ImageBuilder.java:171)
at net.sourceforge.plantuml.sequencediagram.graphic.SequenceDiagramFileMakerPuma2.createOne(SequenceDiagramFileMakerPuma2.java:234)
at net.sourceforge.plantuml.sequencediagram.SequenceDiagram.exportDiagramInternal(SequenceDiagram.java:222)
at net.sourceforge.plantuml.UmlDiagram.exportDiagramNow(UmlDiagram.java:236)
at net.sourceforge.plantuml.AbstractPSystem.exportDiagram(AbstractPSystem.java:127)
at net.sourceforge.plantuml.SourceStringReader.generateImage(SourceStringReader.java:124)
at net.sourceforge.plantuml.SourceStringReader.generateImage(SourceStringReader.java:111)
at net.sourceforge.plantuml.SourceStringReader.generateImage(SourceStringReader.java:101)
at scr.graphviz.sk.PlantUMLDemoMain.generateFromStringSource(PlantUMLDemoMain.java:66)
at scr.graphviz.sk.PlantUMLDemoMain.main(PlantUMLDemoMain.java:23)
I found someone with similar problem and older version of plantuml worked for him. I have jar file of the older version but I'm not sure how to apply it. I tried inspecting the file and find out versions of libraries used and added maven dependencies for them but it didnt seem to work.
This is similar problem i mentioned https://github.com/plantuml/plantuml/issues/69
There are three different tables (OPTIONS, FIELDS and DATA) in import parameter "QUERY_TABLE" = "LTAP".
I created a java program to display column FIELDNAME from the table FIELDS with helping function RFC_READ_TABLE.
It always appears Error com.sap.conn.jco.AbapException: (126) TABLE_NOT_AVAILABLE: TABLE_NOT_AVAILABLE Message 300 of class DA type E, when I call the method step2WorkWithTable().
Can anybody explain the error? And how to fix it?
My codes:
import java.util.Properties;
import com.sap.conn.jco.AbapException;
import com.sap.conn.jco.JCoDestination;
import com.sap.conn.jco.JCoDestinationManager;
import com.sap.conn.jco.JCoException;
import com.sap.conn.jco.JCoFunction;
import com.sap.conn.jco.ext.DestinationDataProvider;
import com.sap.conn.jco.JCoStructure;
import com.sap.conn.jco.JCoTable;
public class RFC_Read_Table {
public static void main(String[] args) throws JCoException
{
System.out.println("Step1: connect SAP without Pool");
step1Connect();
System.out.println("");
System.out.println("Step2: call RFC_Read_Table ");
step2WorkWithTable();
System.out.println("--------------------------------");
System.out.println("finished");
}
static {
String DESTINATION_NAME1 = "mySAPSystem";
Properties connectProperties = new Properties();
connectProperties.setProperty(DestinationDataProvider.JCO_ASHOST, "ABC");
connectProperties.setProperty(DestinationDataProvider.JCO_SYSNR, "33");
connectProperties.setProperty(DestinationDataProvider.JCO_SAPROUTER, "/A/123/");
connectProperties.setProperty(DestinationDataProvider.JCO_CLIENT, "100");
connectProperties.setProperty(DestinationDataProvider.JCO_USER, "UserID");
connectProperties.setProperty(DestinationDataProvider.JCO_PASSWD, "Passwort");
connectProperties.setProperty(DestinationDataProvider.JCO_LANG, "de");
createDestinationDataFile(DESTINATION_NAME1, connectProperties);
}
private static void createDestinationDataFile(String destinationName, Properties connectProperties) {
File destCfg = new File(destinationName+".jcoDestination");
try
{
FileOutputStream fos = new FileOutputStream(destCfg, false);
connectProperties.store(fos, "for tests only !");
fos.close();
}
catch (Exception e)
{
throw new RuntimeException("Unable to create the destination files", e);
}
}
public static void step1Connect() throws JCoException
{
try {
JCoDestination destination = JCoDestinationManager.getDestination("mySAPSystem");
System.out.println("connected");
destination.ping();
} catch (JCoException e) {
e.printStackTrace();
System.out.println("not connected");
}
}
public static void step2WorkWithTable() throws JCoException
{
JCoDestination destination = JCoDestinationManager.getDestination("mySAPSystem");
JCoFunction function = destination.getRepository().getFunction("RFC_READ_TABLE");
if (function == null)
throw new RuntimeException("RFC_Read_Table not found in SAP.");
try
{
function.execute(destination);
}
catch(AbapException e)
{
System.out.println(e.toString());
return;
}
function.getImportParameterList().setValue("QUERY_TABLE","LTAP");
JCoTable codes = function.getTableParameterList().getTable("FIELDS");
codes.appendRow();
for (int i = 0; i < codes.getNumRows(); i++)
{
codes.setRow(i);
System.out.println(codes.getString("FIELDNAME"));
}
codes.firstRow();
for (int i = 0; i < codes.getNumRows(); i++, codes.nextRow())
{
function = destination.getRepository().getFunction("RFC_READ_TABLE");
if (function == null)
throw new RuntimeException("RFC_READ_TABLE not found in SAP.");
function.getImportParameterList().setValue("FIELDNAMEID", codes.getString("FIELDNAME"));
try
{
function.execute(destination);
}
catch (AbapException e)
{
System.out.println(e.toString());
return;
}
JCoStructure detail = function.getExportParameterList().getStructure("FIELDS");
System.out.println(detail.getString("FIELDNAME"));
}
}
}
There is nothing wrong with your JCo code. The error message comes from the SAP system. So you need to check in the SAP system, what that error code means. This can be done in transaction SE91. You enter message class = "DA" and message number = "300" and click display.
I did this for you, and the result is:
"No active nametab exists for &"
where '&' needs to be replaced by the input, which is "LTAP" in this case. So we have "No active nametab exists for LTAP".
This error basically means: the database table "LTAP" exists on the database, but has not yet been activated in the ABAP DDIC. (Perhaps because it still contains a syntax error, or a required data element/domain is missing, etc.)
Solution: go to transaction SE11 and try to activate the table. This will probably give you an error message about what is wrong with this table. Fix all the syntax errors, activate it, and then you can use it.
Note: if LTAP is a standard table delivered by SAP, this error probably means that something went wrong when installing a transport/hotpackage from SAP that contained modifications to this table. In this case you should better contact SAP support to get the table back into a "consistent" state again.
I see you are connecting to an ABAP system using JCoDestinationManager. It means your are using the properties from mySAPSystem destination. Please check if the mySAPSystem connects to a proper ABAP system.
What are these lines are needed for
Properties connectProperties = new Properties();
connectProperties.setProperty(DestinationDataProvider.JCO_ASHOST, "ABC");
connectProperties.setProperty(DestinationDataProvider.JCO_SYSNR, "33");
connectProperties.setProperty(DestinationDataProvider.JCO_CLIENT, "100");
connectProperties.setProperty(DestinationDataProvider.JCO_USER, "UserID");
connectProperties.setProperty(DestinationDataProvider.JCO_PASSWD, "Passwort");
connectProperties.setProperty(DestinationDataProvider.JCO_LANG, "de");
I don't see them to be used anywhere in your program. It seems their are not applied to your connection...
I've just been playing around with the Java 7 WatchService for monitoring a file for change.
Here's a little bit of code I knocked up:
WatchService watcher = FileSystems.getDefault().newWatchService();
Path path = Paths.get("c:\\testing");
path.register(watcher, StandardWatchEventKinds.ENTRY_MODIFY);
while (true) {
WatchKey key = watcher.take();
for (WatchEvent event : key.pollEvents()) {
System.out.println(event.kind() + ":" + event.context());
}
boolean valid = key.reset();
if (!valid) {
break;
}
}
This seems to be working, and I get notifications as to when a file 'changethis.txt' gets modified.
However, in addition to being able to notify when a file changes, is there anyway of being notified as to the location within the file that the modification occurred?
I've had a look through the Java docs but I can't seem to find anything.
Is this possible using the WatchService, or would something custom have to be implemented?
Thanks
For what it is worth, I have hacked a little proof of concept which is able to
detect added, modified and deleted files in a watched directory,
displaying unified diffs for each change (also full diffs when files were added/deleted),
keeping track of successive changes by keeping a shadow copy of the source directory,
work in a user-defined rhythm (default is 5 seconds) so as not to print too many small diffs in a short period of time, but rather somewhat bigger ones once in a while.
There are several limitations which would be impediments in production environments:
In order to not complicate the sample code more than necessary, subdirectories are copied at the beginning when the shadow directory is created (because I have recycled an existing method to create a deep directory copy), but ignored during runtime. Only files right below the watched directory are being monitored so as to avoid recursion.
Your requirement not to use external libraries is not met because I really wanted to avoid re-inventing the wheel for unified diff creation.
This solution's biggest advantage - it is able to detect changes anywhere in a text file, not only at the end of file like tail -f - is also its biggest disadvantage: Whenever a file changes it must be fully shadow-copied because otherwise the program cannot detect the subsequent change. So I would not recommend this solution for very big files.
How to build:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>de.scrum-master.tools</groupId>
<artifactId>SO_WatchServiceChangeLocationInFile</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<build>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.1</version>
<configuration>
<source>1.7</source>
<target>1.7</target>
</configuration>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>com.googlecode.java-diff-utils</groupId>
<artifactId>diffutils</artifactId>
<version>1.3.0</version>
</dependency>
</dependencies>
</project>
Source code (sorry, a bit lengthy):
package de.scrum_master.app;
import difflib.DiffUtils;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.nio.file.*;
import java.nio.file.attribute.BasicFileAttributes;
import java.util.LinkedList;
import java.util.List;
import static java.nio.file.StandardWatchEventKinds.*;
public class FileChangeWatcher {
public static final String DEFAULT_WATCH_DIR = "watch-dir";
public static final String DEFAULT_SHADOW_DIR = "shadow-dir";
public static final int DEFAULT_WATCH_INTERVAL = 5;
private Path watchDir;
private Path shadowDir;
private int watchInterval;
private WatchService watchService;
public FileChangeWatcher(Path watchDir, Path shadowDir, int watchInterval) throws IOException {
this.watchDir = watchDir;
this.shadowDir = shadowDir;
this.watchInterval = watchInterval;
watchService = FileSystems.getDefault().newWatchService();
}
public void run() throws InterruptedException, IOException {
prepareShadowDir();
watchDir.register(watchService, ENTRY_CREATE, ENTRY_MODIFY, ENTRY_DELETE);
while (true) {
WatchKey watchKey = watchService.take();
for (WatchEvent<?> event : watchKey.pollEvents()) {
Path oldFile = shadowDir.resolve((Path) event.context());
Path newFile = watchDir.resolve((Path) event.context());
List<String> oldContent;
List<String> newContent;
WatchEvent.Kind<?> eventType = event.kind();
if (!(Files.isDirectory(newFile) || Files.isDirectory(oldFile))) {
if (eventType == ENTRY_CREATE) {
if (!Files.isDirectory(newFile))
Files.createFile(oldFile);
} else if (eventType == ENTRY_MODIFY) {
Thread.sleep(200);
oldContent = fileToLines(oldFile);
newContent = fileToLines(newFile);
printUnifiedDiff(newFile, oldFile, oldContent, newContent);
try {
Files.copy(newFile, oldFile, StandardCopyOption.REPLACE_EXISTING);
} catch (Exception e) {
e.printStackTrace();
}
} else if (eventType == ENTRY_DELETE) {
try {
oldContent = fileToLines(oldFile);
newContent = new LinkedList<>();
printUnifiedDiff(newFile, oldFile, oldContent, newContent);
Files.deleteIfExists(oldFile);
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
watchKey.reset();
Thread.sleep(1000 * watchInterval);
}
}
private void prepareShadowDir() throws IOException {
recursiveDeleteDir(shadowDir);
Runtime.getRuntime().addShutdownHook(
new Thread() {
#Override
public void run() {
try {
System.out.println("Cleaning up shadow directory " + shadowDir);
recursiveDeleteDir(shadowDir);
} catch (IOException e) {
e.printStackTrace();
}
}
}
);
recursiveCopyDir(watchDir, shadowDir);
}
public static void recursiveDeleteDir(Path directory) throws IOException {
if (!directory.toFile().exists())
return;
Files.walkFileTree(directory, new SimpleFileVisitor<Path>() {
#Override
public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
Files.delete(file);
return FileVisitResult.CONTINUE;
}
#Override
public FileVisitResult postVisitDirectory(Path dir, IOException exc) throws IOException {
Files.delete(dir);
return FileVisitResult.CONTINUE;
}
});
}
public static void recursiveCopyDir(final Path sourceDir, final Path targetDir) throws IOException {
Files.walkFileTree(sourceDir, new SimpleFileVisitor<Path>() {
#Override
public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
Files.copy(file, Paths.get(file.toString().replace(sourceDir.toString(), targetDir.toString())));
return FileVisitResult.CONTINUE;
}
#Override
public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs) throws IOException {
Files.createDirectories(Paths.get(dir.toString().replace(sourceDir.toString(), targetDir.toString())));
return FileVisitResult.CONTINUE;
}
});
}
private static List<String> fileToLines(Path path) throws IOException {
List<String> lines = new LinkedList<>();
String line;
try (BufferedReader reader = new BufferedReader(new FileReader(path.toFile()))) {
while ((line = reader.readLine()) != null)
lines.add(line);
}
catch (Exception e) {}
return lines;
}
private static void printUnifiedDiff(Path oldPath, Path newPath, List<String> oldContent, List<String> newContent) {
List<String> diffLines = DiffUtils.generateUnifiedDiff(
newPath.toString(),
oldPath.toString(),
oldContent,
DiffUtils.diff(oldContent, newContent),
3
);
System.out.println();
for (String diffLine : diffLines)
System.out.println(diffLine);
}
public static void main(String[] args) throws IOException, InterruptedException {
String watchDirName = args.length > 0 ? args[0] : DEFAULT_WATCH_DIR;
String shadowDirName = args.length > 1 ? args[1] : DEFAULT_SHADOW_DIR;
int watchInterval = args.length > 2 ? Integer.getInteger(args[2]) : DEFAULT_WATCH_INTERVAL;
new FileChangeWatcher(Paths.get(watchDirName), Paths.get(shadowDirName), watchInterval).run();
}
}
I recommend to use the default settings (e.g. use a source directory named "watch-dir") and play around with it for a while, watching the console output as you create and edit some text files in an editor. It helps understand the software's inner mechanics. If something goes wrong, e.g. within one 5 second rhythm a file is created but also quickly deleted again, there is nothing to copy or diff, so the program will just print a stack trace to System.err.
Okay, here is another answer as a variation of my previous one for changes at any file position (diff). Now the somewhat simpler case is files only being appended (tail).
How to build:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>de.scrum-master.tools</groupId>
<artifactId>SO_WatchServiceChangeLocationInFile</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<build>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.1</version>
<configuration>
<source>1.7</source>
<target>1.7</target>
</configuration>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<!-- Use snapshot because of the UTF-8 problem in https://issues.apache.org/jira/browse/IO-354 -->
<version>2.5-SNAPSHOT</version>
</dependency>
</dependencies>
<repositories>
<repository>
<id>apache.snapshots</id>
<url>http://repository.apache.org/snapshots/</url>
</repository>
</repositories>
</project>
As you can see, we use Apache Commons IO here. (Why a snapshot version? Follow the link in the XML comment if you are interested.)
Source code:
package de.scrum_master.app;
import org.apache.commons.io.input.Tailer;
import org.apache.commons.io.input.TailerListenerAdapter;
import java.io.IOException;
import java.nio.charset.Charset;
import java.nio.file.*;
import static java.nio.file.StandardWatchEventKinds.ENTRY_CREATE;
public class FileTailWatcher {
public static final String DEFAULT_WATCH_DIR = "watch-dir";
public static final int DEFAULT_WATCH_INTERVAL = 5;
private Path watchDir;
private int watchInterval;
private WatchService watchService;
public FileTailWatcher(Path watchDir, int watchInterval) throws IOException {
if (!Files.isDirectory(watchDir))
throw new IllegalArgumentException("Path '" + watchDir + "' is not a directory");
this.watchDir = watchDir;
this.watchInterval = watchInterval;
watchService = FileSystems.getDefault().newWatchService();
}
public static class MyTailerListener extends TailerListenerAdapter {
public void handle(String line) {
System.out.println(line);
}
}
public void run() throws InterruptedException, IOException {
try (DirectoryStream<Path> dirEntries = Files.newDirectoryStream(watchDir)) {
for (Path file : dirEntries)
createTailer(file);
}
watchDir.register(watchService, ENTRY_CREATE);
while (true) {
WatchKey watchKey = watchService.take();
for (WatchEvent<?> event : watchKey.pollEvents())
createTailer(watchDir.resolve((Path) event.context()));
watchKey.reset();
Thread.sleep(1000 * watchInterval);
}
}
private Tailer createTailer(Path path) {
if (Files.isDirectory(path))
return null;
System.out.println("Creating tailer: " + path);
return Tailer.create(
path.toFile(), // File to be monitored
Charset.defaultCharset(), // Character set (available since Commons IO 2.5)
new MyTailerListener(), // What should happen for new tail events?
1000, // Delay between checks in ms
true, // Tail from end of file, not from beginning
true, // Close & reopen files in between reads,
// otherwise file is locked on Windows and cannot be deleted
4096 // Read buffer size
);
}
public static void main(String[] args) throws IOException, InterruptedException {
String watchDirName = args.length > 0 ? args[0] : DEFAULT_WATCH_DIR;
int watchInterval = args.length > 2 ? Integer.getInteger(args[2]) : DEFAULT_WATCH_INTERVAL;
new FileTailWatcher(Paths.get(watchDirName), watchInterval).run();
}
}
Now try appending to existing files and/or creating new ones. Everything will be printed to standard output. In a production environment you would maybe display multiple windows or tabs, one for each log file. Whatever...
#Simon: I hope this one suits your situation better than the more general case and is worth a bounty. :-)
I want to open New MS Word Document to open on button click in java
can you suggest me the code , I am doing this by following code but i think its to open the existing document not to create the new document
class OpenWordFile {
public static void main(String args[]) {
try {
Runtime rt = Runtime.getRuntime();
rt.exec("cmd.exe /C start Employee.doc");
} catch (Exception ex) {
JOptionPane.showMessageDialog(null, "Exception occured" + ex);
}
}
}
You can not do this using Java alone, at least if you need to generate DOC Files you need a 3rd tool library Aspose for example. Take a look at this thread, otherwise you can open existing files using the runtime.
only the comment with out any words
Runtime run = Runtime.getRuntime();
String lcOSName = System.getProperty("os.name").toLowerCase();
boolean MAC_OS_X = lcOSName.startsWith("mac os x");
if (MAC_OS_X) {
run.exec("open " + file);
} else {
//run.exec("cmd.exe /c start " + file); //win NT, win2000
run.exec("rundll32 url.dll, FileProtocolHandler " + path);
}
In the recent release (Java 6.0), Java provides Desktop class. The purpose of the class is to open the application in your system that is associated with the given file. So, if you invoke open() method with a Word document (.doc) then it automatically invokes MS Word as that is the application associated with .doc files.
I have developed a small Swing program (though you can develop it as a console application) to take document number from user and invoke document into MSWord. The assumption is; documents are stored with filename consisting of <document number>>.doc.
Given below is the Java program that you can compile and run it as-it-is. Make sure you change DIR variable to the folder where .doc files are stored.
Here is the code to open Word Doc in Java... its an extract from net....
import java.io.File;
import java.awt.event.*;
import javax.swing.*;
import java.awt.*;
public class WordDocument extends JFrame {
private JButton btnOpen;
private JLabel jLabel1;
private JTextField txtDocNumber;
private static String DIR ="c:\\worddocuments\\"; // folder where word documents are present.
public WordDocument() {
super("Open Word Document");
initComponents();
}
private void initComponents() {
jLabel1 = new JLabel();
txtDocNumber = new JTextField();
btnOpen = new JButton();
Container c = getContentPane();
c.setLayout(new java.awt.FlowLayout());
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
jLabel1.setText("Enter Document Number : ");
c.add(jLabel1);
txtDocNumber.setColumns(5);
c.add(txtDocNumber);
btnOpen.setText("Open Document");
btnOpen.addActionListener(new ActionListener() { // anonymous inner class
public void actionPerformed(ActionEvent evt) {
Desktop desktop = Desktop.getDesktop();
try {
File f = new File( DIR + txtDocNumber.getText() + ".doc");
desktop.open(f); // opens application (MSWord) associated with .doc file
}
catch(Exception ex) {
// WordDocument.this is to refer to outer class's instance from inner class
JOptionPane.showMessageDialog(WordDocument.this,ex.getMessage(),"Error", JOptionPane.ERROR_MESSAGE);
}
}
});
c.add(btnOpen);
} // initCompnents()
public static void main(String args[]) {
WordDocument wd = new WordDocument();
wd.setSize(300,100);
wd.setVisible(true);
}
}
Maybe using java.awt.Desktop can help?
File f = new File("<some temp path>\\file.docx");
f.createNewFile();
Desktop.getDesktop().open(f);
Creates a new empty document and opens it with the systsem specifik program for the extension. The strength of this solution is that it works for all OS... As long as the OS has a program to view the file that is.
Although I suspect that you are looking for semthing with abit more control over the creation of the file...
Maybe this is going to be a larger task than I had originally thought, but regardless, I'm trying to load a MavenProject from a file and then resolve its dependencies. I've got the code for both bits but I'm missing some object references that I need; specifically I need to get instances of RepositorySystemSession and RepositorySystem. Any tips?
Note: I have tagged this question with maven-plugin, but this is not a Maven plugin. I am happy to mandate Maven 3 (think I already have anyway..)
Here's the code I have so far:
Constructing the MavenProject:
public static MavenProject loadProject(File pomFile) throws Exception
{
MavenProject ret = null;
MavenXpp3Reader mavenReader = new MavenXpp3Reader();
if (pomFile != null && pomFile.exists())
{
FileReader reader = null;
try
{
reader = new FileReader(pomFile);
Model model = mavenReader.read(reader);
model.setPomFile(pomFile);
ret = new MavenProject(model);
}
finally
{
// Close reader
}
}
return ret;
}
Resolving dependencies:
public static List<Dependency> getArtifactsDependencies(MavenProject project, String dependencyType, String scope) throws Exception
{
DefaultArtifact pomArtifact = new DefaultArtifact(project.getId());
RepositorySystemSession repoSession = null; // TODO
RepositorySystem repoSystem = null; // TODO
List<RemoteRepository> remoteRepos = project.getRemoteProjectRepositories();
List<Dependency> ret = new ArrayList<Dependency>();
Dependency dependency = new Dependency(pomArtifact, scope);
CollectRequest collectRequest = new CollectRequest();
collectRequest.setRoot(dependency);
collectRequest.setRepositories(remoteRepos);
DependencyNode node = repoSystem.collectDependencies(repoSession, collectRequest).getRoot();
DependencyRequest projectDependencyRequest = new DependencyRequest(node, null);
repoSystem.resolveDependencies(repoSession, projectDependencyRequest);
PreorderNodeListGenerator nlg = new PreorderNodeListGenerator();
node.accept(nlg);
ret.addAll(nlg.getDependencies(true));
return ret;
}
I realise this might be an unusual request, maybe I should just scrap what I was trying to do and wrap it as a plugin...but I kind of just want to finish what I started now! Thanks in advance.
Try jcabi-aether, which is a wrapper around Apache Aether from Sonatype:
final File repo = this.session.getLocalRepository().getBasedir();
final Collection<Artifact> deps = new Aether(this.getProject(), repo).resolve(
new DefaultArtifact("junit", "junit-dep", "", "jar", "4.10"),
JavaScopes.RUNTIME
);
If you are outside of Maven plugin:
final File repo = new File(System.getProperty("java.io.tmpdir"), "my-repo");
final MavenProject project = new MavenProject();
project.setRemoteArtifactRepositories(
Arrays.asList(
new RemoteRepository(
"maven-central",
"default",
"http://repo1.maven.org/maven2/"
)
)
);
final Collection<Artifact> deps = new Aether(project, repo).resolve(
new DefaultArtifact("junit", "junit-dep", "", "jar", "4.10"),
JavaScopes.RUNTIME
);
I would recommend to read the information about Aether lib which is exactly is for such kind of purposes.
Note: Aether was previousely developed at Sonatype, but has since moved to Eclipse.
I just whipped up a solution to both your and my problem:
/*******************************************************************************
* Copyright (c) 2013 TerraFrame, Inc. All rights reserved.
*
* This file is part of Runway SDK(tm).
*
* Runway SDK(tm) is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* Runway SDK(tm) is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with Runway SDK(tm). If not, see <http://www.gnu.org/licenses/>.
******************************************************************************/
package com.test.mavenaether;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.Arrays;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;
import org.apache.maven.artifact.repository.ArtifactRepository;
import org.apache.maven.artifact.repository.ArtifactRepositoryPolicy;
import org.apache.maven.artifact.repository.MavenArtifactRepository;
import org.apache.maven.artifact.repository.layout.DefaultRepositoryLayout;
import org.apache.maven.model.Model;
import org.apache.maven.model.io.xpp3.MavenXpp3Reader;
import org.apache.maven.project.MavenProject;
import org.codehaus.plexus.util.xml.pull.XmlPullParserException;
import org.sonatype.aether.artifact.Artifact;
import org.sonatype.aether.resolution.DependencyResolutionException;
import org.sonatype.aether.util.artifact.DefaultArtifact;
import org.sonatype.aether.util.artifact.JavaScopes;
import com.jcabi.aether.Aether;
public class MavenAether
{
public static void main(String[] args) throws Exception
{
String classpath = getClasspathFromMavenProject(new File("/users/terraframe/documents/workspace/MavenSandbox/pom.xml"), new File("/users/terraframe/.m2/repository"));
System.out.println("classpath = " + classpath);
}
public static String getClasspathFromMavenProject(File projectPom, File localRepoFolder) throws DependencyResolutionException, IOException, XmlPullParserException
{
MavenProject proj = loadProject(projectPom);
proj.setRemoteArtifactRepositories(
Arrays.asList(
(ArtifactRepository) new MavenArtifactRepository(
"maven-central", "http://repo1.maven.org/maven2/", new DefaultRepositoryLayout(),
new ArtifactRepositoryPolicy(), new ArtifactRepositoryPolicy()
)
)
);
String classpath = "";
Aether aether = new Aether(proj, localRepoFolder);
List<org.apache.maven.model.Dependency> dependencies = proj.getDependencies();
Iterator<org.apache.maven.model.Dependency> it = dependencies.iterator();
while (it.hasNext()) {
org.apache.maven.model.Dependency depend = it.next();
final Collection<Artifact> deps = aether.resolve(
new DefaultArtifact(depend.getGroupId(), depend.getArtifactId(), depend.getClassifier(), depend.getType(), depend.getVersion()),
JavaScopes.RUNTIME
);
Iterator<Artifact> artIt = deps.iterator();
while (artIt.hasNext()) {
Artifact art = artIt.next();
classpath = classpath + " " + art.getFile().getAbsolutePath();
}
}
return classpath;
}
public static MavenProject loadProject(File pomFile) throws IOException, XmlPullParserException
{
MavenProject ret = null;
MavenXpp3Reader mavenReader = new MavenXpp3Reader();
if (pomFile != null && pomFile.exists())
{
FileReader reader = null;
try
{
reader = new FileReader(pomFile);
Model model = mavenReader.read(reader);
model.setPomFile(pomFile);
ret = new MavenProject(model);
}
finally
{
reader.close();
}
}
return ret;
}
}
pom.xml:
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.test</groupId>
<artifactId>MavenSandbox</artifactId>
<version>0.0.1-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>com.jcabi</groupId>
<artifactId>jcabi-aether</artifactId>
<version>0.7.19</version>
</dependency>
<dependency>
<groupId>org.apache.maven</groupId>
<artifactId>maven-core</artifactId>
<version>3.0.3</version>
</dependency>
</dependencies>
</project>
The code first loads the Maven project (using your function provided in the original question), then uses jcabi-aether to find the artifact in your local repository. You will need to change the two parameters in the main function: the location for the pom.xml of your project, and the location of your local repository.
Enjoy! :)
Try this (as can be seen from the ather-demo):
...
LocalRepository localRepository = new LocalRepository("/path/to/local-repo");
RepositorySystem system = getRepositorySystemInstance();
RepositorySystemSession session = getRepositorySystemSessionInstance(system, localRepository);
....
public static RepositorySystem getRepositorySystemInstance()
{
/**
* Aether's components implement org.sonatype.aether.spi.locator.Service to ease manual wiring and using the
* prepopulated DefaultServiceLocator, we only need to register the repository connector factories.
*/
MavenServiceLocator locator = new MavenServiceLocator();
locator.addService(RepositoryConnectorFactory.class, FileRepositoryConnectorFactory.class);
locator.addService(RepositoryConnectorFactory.class, WagonRepositoryConnectorFactory.class);
locator.setServices(WagonProvider.class, new ManualWagonProvider());
return locator.getService(RepositorySystem.class);
}
private static RepositorySystemSession getRepositorySystemSessionInstance(RepositorySystem system,
LocalRepository localRepo)
{
MavenRepositorySystemSession session = new MavenRepositorySystemSession();
session.setLocalRepositoryManager(system.newLocalRepositoryManager(localRepo));
session.setTransferListener(new ConsoleTransferListener());
session.setRepositoryListener(new ConsoleRepositoryListener());
// Set this in order to generate dirty trees
session.setDependencyGraphTransformer(null);
return session;
}
There is a nice set of standalone examples for Eclipses Aether API which is used in the latest Maven (3.1.1) and it can be found here.
Note: Maven 3.1.X still uses Aether 0.9.0.M2 (and the latest version which us used in the examples is 0.9.0.M3). So to run these examples inside a Maven plugin, version M2 is required, and a standalone application can use the latest M3 version.
This is covered in "Aether/Setting Aether Up" for the RepositorySystem and in "Aether/Creating a Repository System Session" in the eclipse wiki.
There is not a ton of documentation, but you can create a RepositorySystem as follows:
// verbatim copy from the Setting Aether Up link
private static RepositorySystem newRepositorySystem()
{
DefaultServiceLocator locator = MavenRepositorySystemUtils.newServiceLocator();
locator.addService( RepositoryConnectorFactory.class, BasicRepositoryConnectorFactory.class );
locator.addService( TransporterFactory.class, FileTransporterFactory.class );
locator.addService( TransporterFactory.class, HttpTransporterFactory.class );
return locator.getService( RepositorySystem.class );
}
Do note that this requires the dependencies detailed in "Getting Aether", most notably maven-aether-provider.
When you have your repository system the tutorial goes on to create a RepositorySystemSession with the following factory method:
// copied verbatim from "Creating a Repository System Session"
private static RepositorySystemSession newSession( RepositorySystem system )
{
DefaultRepositorySystemSession session = MavenRepositorySystemUtils.newSession();
LocalRepository localRepo = new LocalRepository( "target/local-repo" );
session.setLocalRepositoryManager( system.newLocalRepositoryManager( session, localRepo ) );
return session;
}