Skip to main content

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [List Home]
Re: [platform-swt-dev] Execute swt program success inside Eclipse, but failure execute it as a standalone jar file

We will get more information about this question. In fact, there are two errors displayed.

The first error is that some error message are print in the console while execute the SWT program as a jar file in the console. The messages are showed as blew:

(Test SWT:5234): Gtk-CRITICAL **: gtk_hsv_to_rgb: assertion 's >= 0.0 && s <= 1.0' failed

But the error messages are not printed if I execute the SWT program inside the Eclipse. The error occurs even if the program is a the most simple SWT program, such as:


package testswt;

import org.eclipse.jface.window.*;
import org.eclipse.swt.widgets.*;


public class CTestSWT extends ApplicationWindow
{
	public static void main(String[] args)
	{
		Display.setAppName("Test SWT");
		
		CTestSWT frObj = new CTestSWT();
		frObj.setBlockOnOpen(true);
		frObj.open();
		Display.getCurrent().dispose();
	}
	
	public CTestSWT()
	{
		super(null);
	}
	
	@Override
	protected Control createContents(Composite parent)
	{
		getShell().setText("Test SWT");
		getShell().setSize(300, 300);
		getShell().setLocation(200, 200);
		
		return parent;
	}
}


To the most simple SWT program, this error does not present any wrong with the GUI. But to some dialog, I get the wrong display as fig_error.png (in attachments) while I execute it as standalone jar file. The correct display as fig_correct.png (in attachments) while I execute it inside the Eclipse. There are not any different with the two executions except that one is executed as a standalone jar file in console with the error message printed like above, and other is executed inside the Eclipse without any error message printed.




The second error is about the function "computeSize" of the class Label. The simple code file is CTestSWT.java (in attachments). This code try to find a suitable font size to meet the require of the Label width. So a loop is set up, the font size is changed in the loop, and the prefect Label width with these font size is compute by call "computeSize" of the class Label. After the suitable size is found, it is applied to the Label object.

This code is executed inside the Eclipse, and the execution result of the "computeSize" is as blew.

Point {75, 14}   200
Point {82, 16}   200
Point {86, 17}   200
Point {95, 20}   200
Point {104, 21}   200
Point {110, 23}   200
Point {129, 25}   200
Point {135, 26}   200
Point {139, 28}   200
Point {154, 30}   200
Point {160, 31}   200
Point {168, 33}   200
Point {179, 35}   200
Point {189, 36}   200
Point {195, 38}   200
Point {208, 40}   200

But if I run the program as a standalone jar file in the console, the error occurs. The loop becomes a endless loop because the results of "computeSize" with font size loop become error, like blew.

Point {75, 14}   200
Point {75, 16}   200
Point {75, 17}   200
Point {75, 20}   200
Point {75, 21}   200
Point {75, 23}   200
Point {75, 25}   200
Point {75, 26}   200
Point {75, 28}   200
Point {75, 30}   200
Point {75, 31}   200
Point {75, 33}   200
Point {75, 35}   200
Point {75, 36}   200
Point {75, 38}   200

The x value of the Point value are not changed with the different font size. So a endless loop is done.



The two errors appear after I upgrade the Eclipse to Luna, and the code is not be changed. It runs without any error in older version of Eclipse, not only inside the Eclipse, but also as a standalone jar file in console.

The command line which I run the SWT program as a standalone jar file is like blew.
wgl@athena:~/home/SoftDev/dev/CurveSpirit/project/bin$ java -classpath testswt.jar:swt.jar:org.eclipse.core.commands.jar:org.eclipse.core.runtime.jar:org.eclipse.equinox.common.jar:org.eclipse.jface.jar: testswt/CTestSWT

My operate system is Debian testing, the jdk is OpenJDK 7u65-2.5.2-4 which is install from Debian, and the Eclipse is Luna. The phenomenon is very strange, and it was not occur in older version of Eclipse.

What is wrong? Would you please help me? Thank you very much!


With best regards

Gufstream



Attachment: fig_error.png
Description: PNG image

Attachment: fig_correct.png
Description: PNG image

package testswt;

import org.eclipse.jface.window.*;
import org.eclipse.swt.*;
import org.eclipse.swt.custom.*;
import org.eclipse.swt.graphics.*;
import org.eclipse.swt.widgets.*;


public class CTestSWT extends ApplicationWindow
{
	public static void main(String[] args)
	{
		Display.setAppName("Test SWT");
		
		CTestSWT frObj = new CTestSWT();
		frObj.setBlockOnOpen(true);
		frObj.open();
		Display.getCurrent().dispose();
	}
	
	/**
	 * 根据给定字符串、字体和最大允许宽度确定字号。
	 * @param device 创建字体的设备。
	 * @param comp 父控件。
	 * @param text 字符串。
	 * @param font_data 字体数据。
	 * @param width 字符串显示最大允许宽度。
	 * @return 字号。
	 */
	public static int findFontSizeWithWidth(Device device, Composite comp, String text, FontData font_data, int width)
	{
		Label lbl = new Label(comp, SWT.NONE);
		lbl.setText(text);
		
		int iFontSize = 8;
		boolean blnFound = false;
		while (!blnFound)
		{
			font_data.setHeight(iFontSize);
			Font f = new Font(device, font_data);
			lbl.setFont(f);
			Point perfectSize = lbl.computeSize(SWT.DEFAULT, SWT.DEFAULT, false);
			System.out.print(perfectSize);
			System.out.print("   ");
			System.out.println(width);
			if (perfectSize.x <= width)
				iFontSize++;
			else
			{
				iFontSize--;
				blnFound = true;
			}
			f.dispose();
		}
		
		return iFontSize;
	}
	
	public CTestSWT()
	{
		super(null);
	}
	
	@Override
	protected Control createContents(Composite parent)
	{		
		// 窗口标题、大小
		getShell().setText("Test SWT");
		getShell().setSize(300, 300);
		getShell().setLocation(200, 200);
		
		parent.setLayout(null);
		
		CCombo combo = new CCombo(parent, SWT.NONE);
		combo.setBounds(50, 80, 80, 24);
		String [] s = {"Data in line", "Data in column"};
		combo.setItems(s);
		
		// 名称
		Label lblName = new Label(parent, SWT.NONE);
		lblName.setText("Test Words");
		FontData font_data = new FontData("", 28, SWT.BOLD);
		int iWidth = 200;
		int iFontSize = findFontSizeWithWidth(getShell().getDisplay(), parent, "Curve Spirit", font_data, iWidth);
		font_data.setHeight(iFontSize);
		Font f = new Font(getShell().getDisplay(), font_data);
		lblName.setFont(f);
		f.dispose();
		Point perfectSize = lblName.computeSize(SWT.DEFAULT, SWT.DEFAULT);
		lblName.setBounds(190, 25, perfectSize.x, perfectSize.y);
		
		return parent;
	}
}

Back to the top