2009/11/18

Dynsolve starts


I finished the check-MCR-on-start in Sparc and hand it in to Denis today, finally.

Now I think I could begin with the Dynsolve now coz I don't want my boss to be so disappointed.  It all starts with the short instructions on the paper, and of course the fancy Matlab code from Raids as well. That's an old code from the year 1997.  I hope I could bring him some good news soon, and myself some SEK for living ;-)


Meanwhile I have the vehicle acoustics HA1 Revision waiting for me, as well as HA3 and, the most annoying one, the nonlinear... 

exe4j

exe4j can make .exe file correctly than jsmooth if there is a calling to Runtime.exec() in the java code.
download exe4j form it's official website and get a crack file from
http://www.slzz.net/Soft/bygj/13126.html

exe4j strictly requires .ico format file, which could be converted from a png file. Here's the website for doing so:

http://www.bitbug.net/

2009/11/04

Failed to find the runtime libraries, required by MATLAB Builder JA, onsystem path

Since Tom has deleted everything form the server by mistake, I have to install Weblcc again...TT. Everything is OK except for the old annoying error:
Failed to find the runtime libraries, required by MATLAB Builder JA, on system path

I could not solve it this time!!

---------------------------------------

01:28, 11.7, 2009

On last Thursday Danis let me compile a Matlab2009a version of Sparc, so I deleted Matlab2008a from my laptop, installing 09a, compiling the matlab2sparc in it. Then I could not work with sparc in netbeans anymore. The same error as that in weblcc:

Failed to find the runtime libraries, required by MATLAB Builder JA, on system path

Thousands of methods had been tried but none helps. 


Yesterday I restored my computer and installed Matlab2008a again, and created a new project the same as matlab2sparc, and compiled it in 08a. Everything worked again!!


Yesterday Tom also helped me to restore the virtual server and installed 2008b on it which is the one I had used initially in the virtual server. Now everything works again!


Maybe the real problem is caused by Matlab version issues?


Anyway, I could make Raids happy again.  



2009/09/29

[转]JSmooth打包Class为exe(Jar转为EXE)

原理大概看了一下,基本上是将其做成一个自解压文件运行,一个770字节的HelloWorld程序打包完成会成为380多k的程序,如果特别在意程序大小我觉得这个不是个好办法,但是为了发布方便,通过双击运行程序可以解决很多普通用户对Java的反感,这还是不错的选择。
这个程序可以在下面的网址找到:
http://jsmooth.sourceforge.net/index.php
安装之后运行会在开始菜单找到快捷方式,运行JSmoothGen
启动之后,界面类似Nokia的开发环境,左边是竖着一溜内容,很直观的告诉我们要从上到下一个一个设置。
需要说明一点的是在转EXE之前,你成Java程序应该已经打包成了Jar,这用Eclipse导出即可完成(其中要生成manifest文件到目录中,除非你自己已经写了)。
逐个将左边的内容说一下
Skeleton是基本设置,需要选择是包装成包含窗体的还是控制台的程序
选择之后,后面有个属性窗口,里面包含三个内容
Message:没有安装JRE的时候弹出的提示内容
PressKey:是否需要按键结束
Debug traces:是否调试跟踪
Executable是设置生成的EXE文件的属性
Executable Binary是生成的Exe文件路径及文件名,如:C:\HelloWorld.exe
Executable Icon是生成Exe文件的图标,内容如:C:\Dev-Cpp\Icons\Communication.ico
这个选项选择完了之后会在下面出现一个预览,有的图标文件没有出现预览图象而是出现“(no image)”的话,最好换一个,否则转换不过去
Current Directory是当前目录,一般不改,如果不仔细看的话看不到那个表示当前目录的点,小心删除了之后转换不过去
Application是有关Java程序的设置
Main Class这里输入程序的入口类,这个类应该包含一个main函数。因为我们经常会用到域名建立我们的命名空间(忘了Java叫什么,.net里面这么称呼的),这个时候可以用到打包时候生成的那个manifest文件了,如果你在打包的时候设置了Main Class,现在就可以把文件中的内容直接复制出来了。格式如下:org.onlymap.rodger.HelloWorld
Appliaction Arguments是执行程序的参数,如果你的程序运行的时候需要有一些参数可以放进去,如果在执行程序的时候又给了新的参数,这里给的参数就没有作用了,比如程序没有带参数启动的时候需要显示帮助,加入-h的时候也是显示帮助,那么就不需要在代码中针对无参数和-h分别判断,而是在这里输入一个-h即可达到这种效果。
Embedded Jar是你已经打包好的Java 程序,输入全路径,或者单击后面按钮启动对话框进行选择。
Classpath就不用说了吧,就是相关的其他的Jar包

JVM Selection提供了对Java 虚拟机的相关限制,其中包括了最高版本和最低版本限制,允许绑定一个虚拟机还有搜索Java虚拟机的环境变量顺序,这个基本我都没有动,不详细说了
JVM Configration是对虚拟机的配置,包含Maximum Memory(最大内存)、Initial Memory Allocation(初始内存分配)和Java Properties(Java属性),也都很好理解。只是最后一个,这些信息除了查看属性的时候可能可以看到之外,是否在程序中也可以调用呢?
如果有错请大家指正。

(原文:http://www.3snews.net/html/95/895-6944.html)

2009/08/24

weblcc发布了

上周五Tom把virtual server帮我搞定. 可是把该装的软件都在新的Windows server 2008上装好后, 却在Netbeans中运行时发生Java虚拟机的错误. 周末想了两天, 发现还是MCR的问题: 在数据库操作之后实例化matlab接口会出现问题. 看来还是要在server 2008上装matlab. 那个stand along的MCR还是不太完善. 

今天上午装上了matlab. 虽然可以在一个普通的Java类中实例化matlab接口,可是还是运行不了web程序. 比较一下, 看到web程序中servlet实例化matlab接口的方法是:

weblcc.WeblccMat weblccMat = null;

...

synchronized (application) {
weblccMat = (weblcc.WeblccMat) pageContext.getAttribute("weblccMat", PageContext.APPLICATION_SCOPE);
if (weblccMat == null) {
weblccMat = new weblcc.WeblccMat();
pageContext.setAttribute("weblccMat", weblccMat, PageContext.APPLICATION_SCOPE);
}
}

于是尝试把程序改为

if(session.getAttribute("weblccMat")==null){

  weblccMat =  new weblcc.WeblccMat();

 session.setAttribute("weblccMat",weblccMat);

}else{

  weblccMat = (weblcc.WeblccMat)session.getAttribute("weblccMat");

}

运行,失败。不是代码的错误,而是glassfish在经历了Java虚拟机的错误之后就启动不了了。当初之所以用glassfish,是因为netbeans的doc是用glassfish做实例。可是后来看网上很多资料都是tomcat相关的。而且,我也发现tomcat的启动速度要比glassfish快很多, 这对于调试当然很重要了。于是我决定用tomcat做我的服务器程序。

在netbeans中设置tomcat为运行时服务器后,weblcc终于可以顺利运行。

之后我把weblcc的web程序在tomcat中deploy,运行,前面所有页面都顺利,就是最后的results picture出不来。有了上次的经验,我很快找到了问题的原因:还是路径问题。 matlab程序将图片按绝对路径存到了我的netbeans的project下build/web/resultpics/文件夹下,可是tomcat会按相对路径在tomcat目录下的webapps/LCC/resultpics/文件夹下找图片。 这样当然不会显示图片。

于是我把原servlet程序中的

private static final String figureDir = System.getProperty("user.dir") + "/domains/LCC/build/web/resultpics/";

改为

private static final String figureDir = "D:/program/Apache Tomcat 6.0.18/webapps/LCC/resultpics/". 注意最后那个backslash一定要加,不然图片全存到/LCC下了!

运行bin/startup.bat,成功!


weblcc 顺利发布。 


2009/08/15

[转]mysql自动断开连接的问题及解决方法

工作的两年多时间里,接触过几种数据库,在实际项目开发中使用过Oracle 10g和MySql,其中遇到过一件比较有意思的问题:以前公司开发的一个项目使用的MySql数据库,用的是连接池的方式连接数据库,然后发现一个问题,如果一段时间不登陆该系统 ,下一次登陆的时候就会报“用户名或密码错误”,接着再次登陆就不会有问题了,而第一次登陆的用户名和密码肯定是正确的,之前的技术人员也一直没有找到出现这个问题的原因。我到这个公司之后,经理让我看看代码能不能解决这个问题,我当时怀疑是不是登陆跳转页面中有什么问题呢?可是看了几天也没有什么问题啊,最后我想到是不是数据库的原因呢?可是我对数据库也不是很熟悉,然后就问了一个赛迪的网友,经过他的提示和在网上查的资料,知道了原因和解决方法,原来是当MySql数据库连接空闲一定时间后,服务器就会断开等待超时的连接,默认时间是8小时,当我修改了连接池的连接方式,使用了DBCP连接方式,设置几个参数,这样,每次访问数据库的时候都会先检查连接是否有效,如果无效则建立有效连接,这样就不会出现我之前说的那个问题了,啰啰嗦嗦的说了一大堆,其实是个很简单的问题,只是当时我对数据库不了解才一直都找不到原因。 

  下面是我当时参考的一篇文章和自己总结的一些需要注意的问题: 
数据库连接池的好处是不言而喻的,现在大部分的application server都提供自己的数据库连接池方案,此时,只要按照application server的文档说明,正确配置,即可在应用中享受到数据库连接池的好处。 
但是,有些时候,我们的应用是个独立的java application,并不是普通的WEB/J2EE应用,而且是单独运行的,不要什么application server的配合,这种情况下,我们就需要建立自己的数据库连接池方案了。这里,介绍如何利用apache的dbcp来建立为民自己的数据库连接池。 

1、首先,下载必须的jar包 
dbcp包,目前版本是1.2.1:http://jakarta.apache.org/commons/dbcp/ 
pool包,目前版本是1.3:http://jakarta.apache.org/commons/pool/, 
如果下载的pool包是1.2的版本,还要下载common-collections包:http://jakarta.apache.org/commons/collections/ 
在建立我们自己的数据库连接池时,可以使用xml文件来传入需要的参数,这里只使用hard code的方式来简单介绍,所有需要我们自己写的代码很少,只要建立一个文件如下: 
import org.apache.commons.dbcp.BasicDataSource; 
import org.apache.commons.dbcp.BasicDataSourceFactory; 

import java.sql.SQLException; 
import java.sql.Connection; 
import java.util.Properties; 

public class ConnectionSource { 
private static BasicDataSource dataSource = null; 

public ConnectionSource() { 


public static void init() { 

if (dataSource != null) { 
try { 
dataSource.close(); 
} catch (Exception e) { 
// 

dataSource = null; 


try { 
Properties p = new Properties(); 
p.setProperty("driverClassName", "oracle.jdbc.driver.OracleDriver"); 
p.setProperty("url", "jdbc:oracle:thin:@192.168.0.1:1521:testDB"); 
p.setProperty("password", "scott"); 
p.setProperty("username", "tiger"); 
p.setProperty("maxActive", "30"); 
p.setProperty("maxIdle", "10"); 
p.setProperty("maxWait", "1000"); 
p.setProperty("removeAbandoned", "false"); 
p.setProperty("removeAbandonedTimeout", "120"); 
p.setProperty("testOnBorrow", "true"); 
p.setProperty("logAbandoned", "true"); 

dataSource = (BasicDataSource) BasicDataSourceFactory.createDataSource(p); 

} catch (Exception e) { 
// 




public static synchronized Connection getConnection() throws SQLException { 
if (dataSource == null) { 
init(); 

Connection conn = null; 
if (dataSource != null) { 
conn = dataSource.getConnection(); 

return conn; 



  接下来,在我们的应用中,只要简单地使用ConnectionSource.getConnection()就可以取得连接池中的数据库连接,享受数据库连接带给我们的好处了。当我们使用完取得的数据库连接后,只要简单地使用connection.close()就可把此连接返回到连接池中,至于为什么不是直接关闭此连接,而是返回给连接池,这是因为dbcp使用委派模型来实现Connection接口了。 

  在使用Properties来创建BasicDataSource时,有很多参数可以设置,比较重要的还有: 

  testOnBorrow、testOnReturn、testWhileIdle,他们的意思是当是取得连接、返回连接或连接空闲时是否进行有效性验证(即是否还和数据库连通的),默认都为false。所以当数据库连接因为某种原因断掉后,再从连接池中取得的连接,实际上可能是无效的连接了,所以,为了确保取得的连接是有效的,可以把把这些属性设为true。当进行校验时,需要另一个参数:validationQuery,对oracle来说,可以是:SELECT COUNT(*) FROM DUAL,实际上就是个简单的SQL语句,验证时,就是把这个SQL语句在数据库上跑一下而已,如果连接正常的,当然就有结果返回了。 

  还有2个参数:timeBetweenEvictionRunsMillis 和 minEvictableIdleTimeMillis,他们两个配合,可以持续更新连接池中的连接对象,当timeBetweenEvictionRunsMillis 大于0时,每过timeBetweenEvictionRunsMillis 时间,就会启动一个线程,校验连接池中闲置时间超过minEvictableIdleTimeMillis的连接对象。 
还有其他的一些参数,可以参考源代码。 
部分参数简要说明: 
removeAbandoned :是否自动回收超时连接 
removeAbandonedTimeout:超时时间(以秒数为单位) 
rmoveAbandoned=true那么在getNumActive()快要到getMaxActive()的时候,系统会进行无效的Connection的回收,回收的Connection为removeAbandonedTimeout(默认300秒)中设置的秒数后没有使用的Connection 
logAbandoned:logAbandoned=true的话,将会在回收事件后,在log中打印出回收Connection的错误信息,包括在哪个地方用了Connection却忘记关闭了,在调试的时候很有用。 
maxWait:超时等待时间以毫秒为单位 
maxIdle:最大空闲连接 
minIdle:最小空闲连接 
maxActive:最大连接数 
testOnBorrow、testOnReturn、testWhileIdle、validationQuery:上面有介绍 
另外很重要的一点是每次连接使用完了不要忘了调用connection.close()使连接返回到连接池


(原文出处: http://hi.baidu.com/freeafon/blog/item/3775ff8a70843d779e2fb4b5.html)

2009/08/12

path problem

I have been confused by the Matlab-generated figures' problem for more than 3 weeks! It is the path that cause this problem.

The pictures should be saved under the directory 'build' in my Netbeans web project, hence they could be referred and displayed in jsp.

MySQL connetion problem

The problem pops up when I was trying to connect to the MySQL database:

'The last packet successfully received from the server was41645 seconds ago.The last packet sent successfully to the server was 41645 seconds ago, which is longer than the server configured value of 'wait_timeout'. You should consider either expiring and/or testing connection validity before use in your application, increasing the server configured values for client timeouts, or using the Connector/J connection property 'autoReconnect=true' to avoid this problem.com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: The last packet successfully received from the server was41645 seconds ago.The last packet sent successfully to the server was 41645 seconds ago, which is longer than the server configured value of 'wait_timeout'. You should consider either expiring and/or testing connection validity before use in your application, increasing the server configured values for client timeouts, or using the Connector/J connection property 'autoReconnect=true' to avoid this problem.'

I checked online (http://bbs.cnw.com.cn/thread-192473-1-1.html) and it's said that it's better to write the URL of the database in the following way:

jdbc:mysql://localhost:3306/test?autoReconnect=true&failOverReadOnly=false 

I've tried this trick but it's not working. Alternatively I checked the validity of the database connection, via 'conn.isValidate()' (this conn is the class 'JDCConnection' . Plz see my previous blog 'Using connection pool in java bean'). If it's not a validate connection any longer, I get a new connection from DriverManager again.

2009/07/21

save pictures in matlab using MATLAB ja builder

Saving a picture in Matlab is just a piece of cake~we could use both relative and absolute path to point the directory where we want to put the pictures to be saved. 

However, when I compiled my matlab files today  into Java .jar file via Matlab JA builder and tried to run it in Java, it said that 'can not save files to dir'.

Those matlab files would save some pictures to local directory after some calculation, and the path names I used in it is absolute path. I've tested it for many many times under Matlab environment and it worked well, but the problem popped up when it comes to the Java environment. 

I was about to give up when I finally tried to use absolute path instead of relative one. Thanks god it works! I don't know the trick in it but anyway I could continue my work. 

2009/07/15

Summary of SEA (Copied from my original sina blog)

(The source is in Svante's lecture notes and presentations)

(lecNotes 1.1)


SEA formulation:
1. Subdivide the investigated structure into SUBSYSTEMS
2. The power balance equation(conservation of energy, and steady state is considered)


Coupling power proportionallity
'(1.3) states that the net energy flow is proportional to the diffence in energy per mode in connected subsystems'

page 2-4: C is a property of only the considered connections and the elements in this connection. any third element elsewhere in the structure won't change C, (but of course the energy balance.)

SEA parameters, using them flexibly!
P_dis = eta*omega*E = eta*omega*n*e_hat = M*e_hat
P_coup = eta_coup * omega *(E1 - E2) = eta_coup * omega * n *(ehat1 - ehat2) = C (ehat1-ehat2)


ROOM

1, Check the coincidence frequencies!
2, If necessary, reduce the unknowns by assuming that Pin = Ehat*M in the first room.


2009/06/29

关于 Error while evaluating uitoggletool ClickedCallback

环境:

JDK 1.6

Netbeans 6.5

MATLAB Version 7.6 (R2008a)

MATLAB Builder JA Version 2.0.1 (R2008a)

问题:

在一个Java GUI程序中第一次使用由MATLAB Builder JA编译的matlab画图函数时,可以方便的使用zoom in,zoom out 等matlab的figure工具栏。但是当重新计算数据并用相同函数画图时,zoom in,zoom out 等figure工具栏都不可用,并在Netbeans的output中显示:

Error while evaluating uitoggletool ClickedCallback

我首先修改了原matlab程序,在每一个画图命令前都加上‘figure()’命令,但是没有成功:-(

之后我修改了JAVA代码。原先的代码是每按一次画图按钮,便分出一个线程,实例化一个由MATLAB Builder JA编译的Java类,其中就包括那个画图函数,这个函数被Java调用,进行绘图;修改之后,我把实例化的过程放到了GUI程序的构造方法中,也就是每次运行这个Java GUI程序时,由MATLAB Builder JA编译的Java类只会被实例化一次,在GUI程序关闭时才会调用其dispose()方法释放其资源。

修改之后,figure工具栏可以任意使用。 可是此问题的机理还是不太明白。我已发信至其他程序员,希望能得到满意答复。

2009/06/23

"Failed to find the runtime libraries"

I receive a "Failed to find the runtime libraries" error in Netbeans when using the .jar component that was compiled by MATLAB Builder JA 2.0.1 (R2008a) on windows. I searched the solution online, the answer is on

http://www.mathworks.com/support/solutions/en/data/1-78FIKN/index.html?product=MJ

Just add the matlab bin directory to the system PATH, as it instructed. But it didn't work until I restarted the Netbeans IDE.

2009/06/16

"Adding Tab Panels to jTabbedPane using Netbeans"

I am so lucky that I found the solution to the questions stated in the title so quickly!

Thanks to the original blogger:

http://dotnettojava.blogspot.com/2008/07/adding-tab-panels-to-jtabbedpane-using.html

To easily access to it I pasted here, thank you Dot Net To Java:

Adding panels to a tabbed pane was not straight forward until I found the correct procedure. To add panels, select a jpanel from the palette and pull it over to the tabbed pane window. After the first pane, you must place the panel you have grabbed with your mouse over the tabs at the top. Move the panel around until an orange box appears on the tabbed pane. Then you can release the mouse button and the panel will be added as a new tabbed pane panel. If you don't get the orange box, then when you drop the panel, it will become a sub panel on the tabbed pane panel that is currently active. This is not what you want if you are trying to add an additional panel to the tabbed pane. 
Posted by DotNetToJava at 4:04 PM

2009/06/10

Copy the remote database to my local machine

Finnally I succeeded to copy the remote database to my local machine!

1. Log in the remote machine(redhat) via Putty, and cd to the database directory, which consists of the '.frm' '.myd' and 'myi' files.

2. Type command 'zip myDatabase *', which put all the files in the current directory into myDatabase.zip

3. cd the directory where myDatabase.zip locates via psftp, and type the command
'get myDatabase.zip'. Now the myDatabase.zip is on my machine, under the same directory as psfyp.

4. Unzip the file, create a database on local machine, and copy all the file from the zip file to the local database file directory. The database file directory could be found via MySQLAdministrator: Click 'Startup Variables', and then the 'Data Directory' under the 'Directories' is the place where the database table hosts.

Using connection pool in java bean

Now I am using Netbean 6.5 as an IDE, and Glassfish 3 Prelude as a server app.

I tried to use the connection pool provided by Netbean in my java beans, following the help document "Accessing a Connection Pool from a Java Class " in Netbean, but it says no to me ...

I have n0 time to figure it out since my boss is pushing me, so I borrowed the connection pool code from http://java.sun.com/developer/onlineTraining/Programming/JDCBook/conpool.html . There are totally 3 java files that is needed to download:


The interface you need is the first class: JDCConnectionDriver

Here's a demonstration code fragment for connecting to the database:

-------------------------------------------------------------------------

new JDCConnectionDriver("com.mysql.jdbc.Driver", "jdbc:mysql://localhost:3306/weblcc", "root", "password");

Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/weblcc");

-------------------------------------------------------------------------

now the conn could be used for database operation.

Notice: There might be some problems when you run your code unless you uncomment the 3 lines of code:

// if (!url.startsWith(URL_PREFIX)) {

// return null;

// }

in JDCConnectionDriver.connect(String url, Properties props) method, or change the JDCConnectionDriver.URL_PREFIX from "jdbc:jdc:" to "jdbc:". It depends on your URL.

I have tested the code, and plug it in my web apps. Hope it works well  :-D

2009/06/09

[转]解压版MySQL配置全解

来源:http://hi.baidu.com/lupeng0527/blog/item/7a05931b649637d2ad6e75e0.html

一 下载MySQL

http://dev.mysql.com/downloads/mysql/5.0.html

解压MySQL(F:\Program Files\MySQL\)MySQL安装目录

二 新建my.ini

在F:\Program Files\MySQL\下建立my.ini文件,内容为

my.ini

[mysqld] 

# 设置mysql的安装目录 

basedir=F:\\Program Files\\MySQL

# 设置mysql数据库的数据的存放目录,必须是data,或者是\\xxx-data 

datadir=F:\\Program Files\\MySQL\\data

# 设置mysql服务器的字符集,默认编码

default-character-set=latin1 


[client]

# 设置mysql客户端的字符集

default-character-set=latin1


[WinMySQLAdmin] 

# 指定mysql服务启动启动的文件

Server=F:\\Program Files\\MySQL\\bin\\mysqld.exe



三 注册MySQL服务,使MySQL可以在服务项目中启动或关闭

1)进入安装目录\bin,执行:

  F:\Program Files\MySQL\bin>mysqld -install

  显示:Service successfully installed. 则注册服务成功。

如果要删除服务:

F:\Program Files\MySQL\bin>mysqld -remove

2)修改注册表:

HKEY_LOCAL_MACHINE\System\CurrentControlSet\Services\MySQL\ImagePath

修改为:

"F:\Program Files\MySQL\bin\mysqld" --defaults-file=

"F:\Program Files\MySQL\my.ini" MySQL

四 修改root密码

MySQL配置好后,启动成功,默认密码是空,但是为了安全,设置密码(一般用户名为root,密码为root)。

1)登录MySQL root用户:

  进入MySQL安装目录\bin,执行:

  F:\Program Files\MySQL\MySQL-6.0.7\bin>mysql -u root;

2)修改root密码:

  mysql> update mysql.user set password=PASSWORD(''新密码'') where User=''root''; 

  mysql> flush privileges; 

这样配置就可以使用net start mysql命令正常启动MySQL。