MATLAB: Do I receive a java.lang.​IllegalArg​umentExcep​tion error when I print in MATLAB 7.0 (R14)

filejava.lang.illegalargumentexceptionMATLAB

When I choose "File->Print" from a desktop tool such as the command window or the Editor/Debugger window, printing fails and I receive the following Java exception:
java.lang.IllegalArgumentException: services must contain defaultService
at javax.print.ServiceUI.printDialog(Unknown Source)
at
com.mathworks.mde.cmdwin.XCmdWndView.print(XCmdWndView.java:1450)
at
com.mathworks.mde.cmdwin.CmdWinEditorKit$PrintAction.actionPerformed(Cmd
WinEditorKit.java:1277)
at javax.swing.AbstractButton.fireActionPerformed(Unknown
Source)
at
javax.swing.AbstractButton$ForwardActionEvents.actionPerformed(Unknown
Source)
at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown
Source)
at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
at javax.swing.AbstractButton.doClick(Unknown Source)
at javax.swing.plaf.basic.BasicMenuItemUI.doClick(Unknown
Source)
at
javax.swing.plaf.basic.BasicMenuItemUI$MouseInputHandler.mouseReleased(U
nknown Source)
at java.awt.Component.processMouseEvent(Unknown Source)
at java.awt.Component.processEvent(Unknown Source)
at java.awt.Container.processEvent(Unknown Source)
at java.awt.Component.dispatchEventImpl(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown
Source)
at java.awt.LightweightDispatcher.processMouseEvent(Unknown
Source)
<snip>

Best Answer

One possible cause of the problem is that no printer is connected.
Another possible cause is that the printer is incorrectly registered. Execute the following script in the Command Window:
defaultSvc = javax.print.PrintServiceLookup.lookupDefaultPrintService()
%get list of all print services that support the "printable" flavor
dflavor = javaObject('javax.print.DocFlavor$SERVICE_FORMATTED', 'java.awt.print.Printable');
services = javax.print.PrintServiceLookup.lookupPrintServices(dflavor,[]);
for i = 1 : size(services,1)
if (~isempty(defaultSvc) & defaultSvc.toString.compareTo(services(i).toString) == 0)
fprintf('DEFAULT: %s\n', services(i).toString.toCharArray());
else
fprintf(' : %s\n', services(i).toString.toCharArray());
end
end
The name of the default MATLAB printer will appear twice in the output of the script. Check to see if both names of the printer are the same, i.e.,
win32 printer : printername
where "printername" is some string. There might be a problem with "printername", which should correspond to the registry change file for the default printer. For example, "printername" might be registered in lowercase, when the printer name is actually uppercase "PRINTERNAME". Update the registry value to exactly match the printer name appearing in the list of all printers. Following article describes the steps to be taken to update the registry with the printername.
<http://support.microsoft.com/kb/156212>
Please restart MATLAB after you have updated the registry value for the default printer.