Export RGB Image by code: jpeg "opaque" output

Hello guys,
we are working on RGB creation from Sentinel Images.
We are able to create the image but the ouput in jpg is different from the output from Java code.

Open Snap
Open One Sentinel 2 Image
Right Click -> Open RGB Image Window
Wait for the image to be shown
Right click on the Image View
Export View As Image
Select full scene
Select JPG format
The output file is correct.

Here our code snipped to create RGB by code and then save in PNG (correct) and JPG (opaque).
It looks like a transparency problem but did not find how to solve it.

import java.awt.Dimension;
import java.awt.Graphics2D;
import java.awt.geom.AffineTransform;
import java.awt.image.BufferedImage;
import java.awt.image.RenderedImage;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;

import org.esa.snap.core.dataio.ProductIO;
import org.esa.snap.core.datamodel.Band;
import org.esa.snap.core.datamodel.ImageInfo;
import org.esa.snap.core.datamodel.Product;
import org.esa.snap.core.datamodel.RasterDataNode;
import org.esa.snap.core.image.ImageManager;
import org.esa.snap.core.util.ProductUtils;

import com.bc.ceres.core.ProgressMonitor;

public class Test_RGB_Only {

public static void main(String[] args) throws Exception {
	// Input file
	String sFilePath = "S2A_MSIL1C_20180102T102421_N0206_R065_T32TMQ_20180102T123237.zip";
	// Band Images
	String sRedBandName = "B4";
	String sGreenBandName = "B3";
	String sBlueBandName = "B2";
	// Output files
	String sOutPath = "RGB.png";
	// Use null output dimension to create a full resolution img
	createRGB(null, sFilePath, sRedBandName, sGreenBandName, sBlueBandName, sOutPath);
	System.out.println("TEST DONE");

private static void createRGB(Dimension oOutputDimension , String sFilePath, String sRedBandName, String sGreenBandName, String sBlueBandName, String sOutPath) throws IOException, InterruptedException {
	File file = new File(sFilePath);
	Product product = ProductIO.readProduct(file);
	Band oRedBand = product.getBand(sRedBandName);
	Band oGreenBand = product.getBand(sGreenBandName);
	Band oBlueBand = product.getBand(sBlueBandName);
	RenderedImage oOutImg;
	System.out.println("createRGB: start buildRGBImage");
	oOutImg = buildRGBImage(oRedBand , oGreenBand, oBlueBand, oOutputDimension);
	long lStartTime = System.currentTimeMillis();
	ImageIO.write(oOutImg, "PNG", new File(sOutPath));
	System.out.println("createRGB: PNG FILE Saved in " + (System.currentTimeMillis() - lStartTime) + " ms");
	lStartTime = System.currentTimeMillis();
	ImageIO.write(oOutImg, "JPG", new File(sOutPath.replace(".png", ".jpg")));
	System.out.println("createRGB: JPG FILE Saved in " + (System.currentTimeMillis() - lStartTime) + " ms");

 * Return a scaled RGB rendered image from the specified Bands. If oOutputImageSize returns default dimension
 * @param oRedBand Red Band
 * @param oGreenBand Green Band
 * @param oBlueBand Blue Band
 * @param oOutputImageSize Ouput Size
 * @return Rendered Image
 * @throws IOException
public static BufferedImage buildRGBImage(RasterDataNode oRedBand, RasterDataNode oGreenBand, RasterDataNode oBlueBand, Dimension oOutputImageSize) throws IOException {
	RenderedImage oOutputImage = null;
	if (oRedBand==null || oBlueBand == null || oGreenBand == null) {
		System.out.println("buildRGBImage: band null");
		return null;
	if (oRedBand.getProduct()==null || oBlueBand.getProduct() == null || oGreenBand.getProduct() == null) {
		System.out.println("buildRGBImage: band product null");
		return null;
	long lStartTime = System.currentTimeMillis();

    RasterDataNode aoRasters [] = { oRedBand, oGreenBand, oBlueBand };
    ImageInfo oInfo = ProductUtils.createImageInfo(aoRasters, true, ProgressMonitor.NULL);
    oOutputImage = ImageManager.getInstance().createColoredBandImage(aoRasters, oInfo, 0);
    System.out.println("BandImageManager.buildImage: render done: " + (System.currentTimeMillis() - lStartTime) + " ms");
    lStartTime = System.currentTimeMillis();
    if (oOutputImageSize == null) {
    	oOutputImageSize = new Dimension(oOutputImage.getWidth(), oOutputImage.getHeight());
    BufferedImage oResizedImage = new BufferedImage((int) oOutputImageSize.getWidth(), (int) oOutputImageSize.getHeight(),BufferedImage.TYPE_INT_ARGB);
	Graphics2D oGraphics = oResizedImage.createGraphics();
	double dX = (oOutputImageSize.getWidth() / (double)oOutputImage.getWidth());
	double dY = (oOutputImageSize.getHeight() / (double)oOutputImage.getHeight());
	oGraphics.drawRenderedImage(oOutputImage, AffineTransform.getScaleInstance(dX, dY));
    System.out.println("BandImageManager.buildImage: scaling done: " + (System.currentTimeMillis() - lStartTime) + " ms");
    return oResizedImage;	        	


JPEG does not support transparency. Why it looks like it does when you export from the SANP Desktop is the background colour of the image view. In your Java code the no-data area is probably filled black and in SNAP desktop the bakground colour is used.
Both scenes shown are exported in the Desktop. On the left is the JPEG and on the right the PNG. You can see the difference:

You can test it by changing the background colour in the settings.

Thank you very much Marco!!
For instance, do you have any suggestion to change the Java code to use the correct background with jpg files or we do not have any chance?

You can have a look at this example:

Instead of adding the Vectorlayer you can use the BackgroundLayer.

Ok thank you again!