一,JAR基本用法示例
在 C:新建文件夹叫 Test ,然后新建 HelloWorld.java 文件,内容如下:
package powerwind;
import javax.swing.*;
public class HelloWorld
{
public static void main(String[]args)throws Exception
{
JOptionPane.showMessageDialog(null,"HelloWorld");
}
}
接着打开命令提示符窗口,输入 cd c:/Test,此时切换到 c:/test目录下。用
开始编译JAVA源文件。
编译成功后,c:/test目录下有个 powerwind 文件夹,该文件夹下有 HelloWorld.class 文件。
此时在 c:/test 目录下新建文件 helloworld.txt ,在此文件输入
Main-Class: powerwind.HelloWorld
注意:在最后一定要有个回车,Main-Class:后面要有空格。
输入
jar -cvfm HelloWorld.jar helloworld.txt powerwind/
命令完成后,生成 HelloWorld.jar 文件,直接双击此文件就可以运行程序了。如果 .jar 文件已经被 WinRAR关联了,可以再新建一个批处理文件 run.bat
start javaw -jar HelloWorld.jar
这样,双击 run.bat 也可以运行程序。
二,ant基本用法示例
程序如一楼的,在c:/test目录下新建 src文件夹,在此文件夹下新建 powerwind 文件夹并把 HelloWorld.java 这个文件放在里面。helloworld.txt 文件放在src文件夹下。最后在c:/test文件夹下新建 build.xml 文件:
<?xml version="1.0"?>
<project name="A simple test" default="compile" basedir=".">
<property environment="env"/>
<property name="src.dir" value="${basedir}/src"/>
<property name="classes.dir" value="${basedir}/classes"/>
<path id="classpath">
<pathelement location="${classes.dir}"/>
<pathelement location="${basedir}"/>
<pathelement location="${env.CLASSPATH}"/>
</path>
<target name="init" >
<delete dir="${classes.dir}"/>
<mkdir dir="${classes.dir}"/>
</target>
<target name="compile" depends="init">
<javac srcdir="${src.dir}" destdir="${classes.dir}" includes="**/*.java">
<classpath refid="classpath"/>
</javac>
</target>
<target name="jar" depends="compile">
<jar jarfile="${classes.dir}/HelloWorld.jar" manifest="${src.dir}/helloworld.txt">
<fileset dir="${classes.dir}" includes="**/*.class" />
</jar>
</target>
<target name="run">
<java classname="powerwind.HelloWorld" fork="yes" dir=".">
<classpath refid="classpath"/>
</java>
</target>
</project>
此时,输入命令 ant 或 ant compile执行编译,完成后输入 ant run ,用 ant jar 还可以打包文件。
PS:前提条件是下载了 ant 这个小工具。
三,由 Hibernate 配置文件生成JAVA代码
文件结构图如下: [attach]4617[/attach]
其中 build.xml 在 Test 目录下,codegen.cfg.xml 文件在 Test/src 目录下,Student.hbm.xml 文件在 Test/src/powerwind 目录下。
Student.hbm.xml
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 2.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd">
<hibernate-mapping>
<class name="powerwind.Student" table="STUDENTS" >
<meta attribute="class-scope">public</meta>
<id name="id" type="long" column="ID" >
<meta attribute="scope-set">protected</meta>
<generator class="native"/>
</id>
<property name="name" type="string" >
<meta attribute="use-in-tostring">true</meta>
<meta attribute="finder-method">findByName</meta>
<column name="NAME" length="15" not-null="true" unique="true" />
</property>
<property name="age" type="int">
<meta attribute="field-description">How old is the student</meta>
<meta attribute="use-in-tostring">true</meta>
<column name="AGE" check="AGE>18" not-null="true"/>
</property>
<property name="***" type="char" column="***"/>
<property name="info" type="string">
<meta attribute="use-in-tostring">true</meta>
<column name="INFO" sql-type="text"/>
</property>
</class>
</hibernate-mapping>
codegen.cfg.xml
<codegen>
<generate renderer="net.sf.hibernate.tool.hbm2java.BasicRenderer"/>
<generate renderer="net.sf.hibernate.tool.hbm2java.FinderRenderer" suffix="Finder" package="powerwind"/>
</codegen>
build.xml
<?xml version="1.0"?>
<project name="hbm2java" default="hbm2java" basedir=".">
<property name="source.root" value="${basedir}/src"/>
<property name="lib.dir" value="E:/java/Hibernate/lib"/>
<path id="class.path">
<fileset dir="${lib.dir}" includes="*.jar"/>
</path>
<target name="hbm2java">
<taskdef name="hbm2java"
classname="net.sf.hibernate.tool.hbm2java.Hbm2JavaTask"
classpathref="class.path"/>
<hbm2java output="${source.root}" config="${source.root}/codegen.cfg.xml">
<fileset dir="${source.root}">
<include name="**/*.hbm.xml"/>
</fileset>
</hbm2java>
</target>
</project>
<property name="lib.dir" value="E:/java/Hibernate/lib"/> 这个要根据 Hibernate 所在目录而设置。
输入 ant 或 ant hbm2java,就可能生成JAVA源文件了。
三(2),由 Hibernate 配置文件生成DDL
在前面的基础上,加上这个文件.
hibernate.properties
hibernate.dialect=net.sf.hibernate.dialect.MySQLDialect
hibernate.connection.driver_class=com.mysql.jdbc.Driver
hibernate.connection.url=jdbc:mysql://localhost:3306/course
hibernate.connection.username=root
hibernate.connection.password=123456
hibernate.show_sql=true
在 build.xml 里添加两个 target 元素.
<target name="compile">
<javac srcdir="${source.root}"
destdir="${source.root}"
debug="on">
<classpath refid="class.path"/>
</javac>
</target>
<target name="schema">
<taskdef name="schemaexport"
classname="net.sf.hibernate.tool.hbm2ddl.SchemaExportTask"
classpathref="class.path" />
<schemaexport properties="${source.root}/hibernate.properties"
quiet="no" text="no" drop="no" output="${basedir}/student.sql" delimiter=";">
<fileset dir="${source.root}" includes="**/*.hbm.xml"/>
</schemaexport>
</target>
同时还要在原来的 path 元素加上 <pathelement location="${source.root}" />
这样,执行 ant compile后再执行 ant schema 就可能生成DDL文件了:
student.sql
drop table if exists STUDENTS;
create table STUDENTS (
ID bigint not null auto_increment,
NAME varchar(15) not null unique,
AGE integer not null check(AGE>18),
*** char(1),
INFO text,
primary key (ID)
);
有一点很奇怪,没有启动 mysql 数据厍时不能生成DDL。
四,有Spring和没有Spring的 IoC
1、使用 Spring 的简单例子:
IHelloWorldService.java
package powerwind;
public interface IHelloWorldService
{
public String getHello();
public void setHello(String hello);
}
HelloWorldService.java
package powerwind;
public class HelloWorldService implements IHelloWorldService
{
private String hello;
public String getHello()
{
return hello;
}
public void setHello(String hello)
{
this.hello=hello;
}
}
HelloWorld.java
package powerwind;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.core.io.*;
public class HelloWorld
{
private IHelloWorldService iHelloWorld;
public HelloWorld()
{
setService();
}
public void setService()
{
Resource resource=new ClassPathResource("appcontext.xml");
BeanFactory factory=new XmlBeanFactory(resource);
iHelloWorld=(IHelloWorldService)factory.getBean("HelloWorld");
}
public IHelloWorldService getService()
{
return iHelloWorld;
}
public static void main(String[]args)
{
HelloWorld helloworld=new HelloWorld();
System.out.println(helloworld.getService().getHello());
}
}
配置文件 appcontext.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">
<beans>
<bean id="HelloWorld" class="powerwind.HelloWorldService">
<property name="hello">
<value>Hello,World!This is from spring!</value>
</property>
</bean>
</beans>
二,没有Spring的实现
修改 HelloWorld.java 文件
package powerwind;
import java.util.Properties;
import java.io.IOException;
import java.io.FileNotFoundException;
import java.io.InputStream;
public class HelloWorld
{
private IHelloWorldService iHelloWorld;
public HelloWorld()
{
setService();
}
public void setService()
{
try
{
Properties p=new Properties();
InputStream is=getClass().getClassLoader()
.getResourceAsStream("appcontext.properties");
p.load(is);
is.close();
String cls=p.getProperty("HelloWorld");
iHelloWorld=(IHelloWorldService)Class.forName(cls).newInstance();
iHelloWorld.setHello(p.getProperty("hello"));
}catch(Exception e)
{
System.out.println(e);
}
}
public IHelloWorldService getService()
{
return iHelloWorld;
}
public static void main(String[]args)
{
HelloWorld helloworld=new HelloWorld();
System.out.println(helloworld.getService().getHello());
}
}
同时配置文件用 appcontext.properties
HelloWorld=powerwind.HelloWorldService
hello=Hello,World!This is not from spring!
这样实现了同样效果,可是Spring的实现不会是这么简单的。