11

I'm working on a application called LMCT(Let Me Copy That) and it is programed in Java, I just need to know how to burn to a DVD/CD from java.

any example, API or link is welcome.

Liam Haworth
  • 848
  • 1
  • 9
  • 27
  • 3
    Offhand, this doesn't really seem like a Java problem. I mean, each piece of hardware has to interface with the operating system for this, which on the Java side will lead to native code. So it's not that it couldn't be done with Java, but it would likely have to interface with another language's code to get the job done. – corsiKa Dec 19 '11 at 01:39
  • I think my best way of doing this is use the IMAPI(c++) and link that into my Java code – Liam Haworth Dec 19 '11 at 01:43
  • Please flag your question again, choose "it doesn't belong here", and pick a reason why. I'll then close it accordingly. – BoltClock Dec 19 '11 at 09:48

1 Answers1

9

I've done this using COM4J and IMAPI (works on Windows only).

This sample code burns an ISO file using the first CD recording device in the system:

File isoFile = new File("myimage.iso");

IDiscMaster2 dm = ClassFactory.createMsftDiscMaster2();
int count = dm.count();

//Pick the first recorder on the system
String recorderUniqueId = null;
for (int i = 0; i < count; i++)
{
    String cur = dm.item(i);
    recorderUniqueId = cur;
}

IDiscRecorder2 recorder = ClassFactory.createMsftDiscRecorder2();
recorder.initializeDiscRecorder(recorderUniqueId);
System.out.println("Using recorder: " + recorder.vendorId() + " " + recorder.productId());

IIsoImageManager imageManager = StreamClassFactory.createMsftIsoImageManager();
imageManager.setPath(isoFile.getAbsolutePath());
imageManager.validate();
System.out.println("ISO Validation successful: " + isoFile.getAbsolutePath());

IDiscFormat2DataExt discData = ClassFactoryExt.createMsftDiscFormat2Data();
discData.recorder(recorder);
discData.clientName(IsoMan.class.getSimpleName());
int mediaStatus = discData.currentMediaStatusExt();
System.out.println("Media status: " + mediaStatus);
if ((mediaStatus & IMAPI_FORMAT2_DATA_MEDIA_STATE.IMAPI_FORMAT2_DATA_MEDIA_STATE_WRITE_PROTECTED.comEnumValue()) != 0)
    throw new RuntimeException("Media is write protected / not empty.");

//Check if disc is empty
int addr = discData.nextWritableAddress();
if (addr != 0)
    throw new RuntimeException("Disc is not empty, not writing.");

IStream isoStream = imageManager.stream();

System.out.println("Writing CD");
discData.write(isoStream);
System.out.println("Finished writing");

I had to supplement the COM4J generated classes a bit because the generator didn't do a perfect job:

/**
 * Defines methods to create COM objects
 */
public abstract class ClassFactoryExt {
    private ClassFactoryExt() {} // instanciation is not allowed

    /**
     * Microsoft IMAPIv2 Data Writer
     */
    public static com.ms.imapi2.IDiscFormat2DataExt createMsftDiscFormat2Data() {
        return COM4J.createInstance( com.ms.imapi2.IDiscFormat2DataExt.class, "{2735412A-7F64-5B0F-8F00-5D77AFBE261E}" );
    }
}

/**
 * Data Writer
 */
@IID("{27354153-9F64-5B0F-8F00-5D77AFBE261E}")
public interface IDiscFormat2DataExt extends com.ms.imapi2.IDiscFormat2Data {
    /**
     * The state (usability) of the current media
     */
    @VTID(18)
    //com.ms.imapi2.IMAPI_FORMAT2_DATA_MEDIA_STATE currentMediaStatus();
    int currentMediaStatusExt();
}

/**
 * Defines methods to create COM objects
 */
public abstract class StreamClassFactory {
    private StreamClassFactory() {} // instanciation is not allowed


    /**
     * Boot options
     */
    public static com.ms.imapi2.IBootOptions createBootOptions() {
        return COM4J.createInstance( com.ms.imapi2.IBootOptions.class, "{2C941FCE-975B-59BE-A960-9A2A262853A5}" );
    }

    /**
     * File system image
     */
    public static com.ms.imapi2.IFileSystemImage3 createMsftFileSystemImage() {
        return COM4J.createInstance( com.ms.imapi2.IFileSystemImage3.class, "{2C941FC5-975B-59BE-A960-9A2A262853A5}" );
    }

    /**
     * Microsoft IMAPIv2 Iso Image Manager
     */
    public static com.ms.imapi2.IIsoImageManager createMsftIsoImageManager() {
        return COM4J.createInstance( com.ms.imapi2.IIsoImageManager.class, "{CEEE3B62-8F56-4056-869B-EF16917E3EFC}" );
    }
}
prunge
  • 22,460
  • 3
  • 73
  • 80
  • Thanks your answer. How can I find *IMAIP.jar* and *COM4J.jar*? Are there their jar files for java? – Omid Nazifi Apr 10 '13 at 05:06
  • 1
    @omidnazifi COM4J can be downloaded [here](http://com4j.kohsuke.org/). The IMAPI classes were generated by COM4J using a libId of `{2C941FD0-975B-59BE-A960-9A2A262853A5}`. The [tlbimp Ant task](http://com4j.kohsuke.org/ant.html) or the [com4j Maven plugin](http://com4j.java.net/maven-com4j-plugin/) can perform code generation. – prunge Apr 10 '13 at 07:06
  • Is COM4J a cross platform? or it use just in **Windows**? – Omid Nazifi Apr 11 '13 at 04:55
  • @omidnazifi Windows only – prunge Apr 11 '13 at 05:09
  • @prunge can you explain more, on how to Generate IMAPI Classes ???? – Pankaj Jawale Jan 05 '15 at 10:47
  • @prunge is there any chance you know how burn the disk with IMAPI2 with disk title? Usually you you create a CD/DVD title when you choose the recorder in Windows 10 but I wasn't able to find proper method neither in IDiscRecorder2 nor IDiscFormat2Data. Am I missing something? – vzateychuk Aug 04 '22 at 14:59