以下代码通过Eclipse的项目可以执行,可是手工编译,却执行不了
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.MissingResourceException;
import java.util.ResourceBundle;public class publishKey2 { private static publishKey2 instance = new publishKey2();
private static ResourceBundle bundle;
public publishKey2(){
bundle = ResourceBundle.getBundle("SqlConn");
} public static publishKey2 getInstance() {
return instance;
} /**
* @param args
* @throws Exception
* @throws SQLException
* @throws ClassNotFoundException
*/
public static void main(String[] args) throws Exception{
//1.DB接続情報を取得する
String url = "jdbc:postgresql://"+getValue("PostgreSeverIP")+"/"+getValue("DatabaseName");
Class.forName("org.postgresql.Driver");
String user = getValue("UserName");
String pwd = getValue("Password"); //2.共通キー情報を入力する
String key = getCommonKey();
//3.インサート処理を行う
Connection conn = null;
try {
conn = DriverManager.getConnection(url, user, pwd);
insertKey(conn, key);
System.out.println("共通鍵:["+key+"]を登録しました!");
} catch (Exception e) {
System.out.println("登録処理にエラーが起こったから、もう一度登録ください!");
} finally {
if (conn != null)
conn.close();
} }
public static String getCommonKey(){
String key = "";
BufferedReader buf = null;
System.out.println("システム共通鍵を入力してください!");
key=getString(buf);
while(key.equals("")){
System.out.println("入力不正、もう一度入力してください!");
key=getString(buf);
}
return key;
} public static void insertKey(Connection conn, String key)
throws Exception {
Statement statement = null;
ResultSet rs = null;
try {
//1.排他処理
String sqlLock = "select * from mst_key for update";
System.out.println("DEBUG: SQL=" + sqlLock); statement = conn.createStatement();
rs = statement.executeQuery(sqlLock);
//2.クリア処理
if(rs.next()){
String sqlDel = "delete from mst_key";
System.out.println("DEBUG: SQL=" + sqlDel);
statement = conn.createStatement();
statement.execute(sqlDel);
}
//3.登録処理
String sqlIns = "insert into mst_key values ('"+key+"')";
System.out.println("DEBUG: SQL=" + sqlIns); statement = conn.createStatement();
statement.execute(sqlIns);
} finally {
if (statement != null)
statement.close();
if (rs!=null)
rs.close();
}
}
private static String getValue(String keyWord) {
try {
return bundle.getString(keyWord);
} catch (MissingResourceException mre) {
return null;
}
}
private static String getString(BufferedReader buf){
buf = new BufferedReader(new InputStreamReader(System.in));
String str = "";
try {
str=buf.readLine();
} catch (IOException e) {
e.printStackTrace();
}
return str;
}
}手动编译是命令行提示如下C:\cjar>dir
ドライブ C のボリューム ラベルがありません。
ボリューム シリアル番号は 907B-B5A7 です C:\cjar のディレクトリ2010/11/23 13:49 <DIR> .
2010/11/23 13:49 <DIR> ..
2010/11/23 13:48 <DIR> lib
2010/11/23 13:36 94 manifest.mf
2010/11/23 13:49 3,475 publishKey2.class
2010/11/23 13:35 3,116 publishKey2.java
2010/11/22 18:49 85 SqlConn.properties
4 個のファイル 6,770 バイト
3 個のディレクトリ 5,268,205,568 バイトの空き領域C:\cjar>javac publishKey2.javaC:\cjar>java publishKey2
Exception in thread "main" java.lang.ClassNotFoundException: org.postgresql.Driv
er
at java.net.URLClassLoader$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClassInternal(Unknown Source)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Unknown Source)
at publishKey2.main(publishKey2.java:35)C:\cjar>java -cp lib\postgresql-8.4-701.jdbc3.jar publishKey2
Exception in thread "main" java.lang.NoClassDefFoundError: publishKey2找了一些资料还是没解决,Eclipse没问题,这里为什么不行呢。
在线等,希望大家帮忙。
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.MissingResourceException;
import java.util.ResourceBundle;public class publishKey2 { private static publishKey2 instance = new publishKey2();
private static ResourceBundle bundle;
public publishKey2(){
bundle = ResourceBundle.getBundle("SqlConn");
} public static publishKey2 getInstance() {
return instance;
} /**
* @param args
* @throws Exception
* @throws SQLException
* @throws ClassNotFoundException
*/
public static void main(String[] args) throws Exception{
//1.DB接続情報を取得する
String url = "jdbc:postgresql://"+getValue("PostgreSeverIP")+"/"+getValue("DatabaseName");
Class.forName("org.postgresql.Driver");
String user = getValue("UserName");
String pwd = getValue("Password"); //2.共通キー情報を入力する
String key = getCommonKey();
//3.インサート処理を行う
Connection conn = null;
try {
conn = DriverManager.getConnection(url, user, pwd);
insertKey(conn, key);
System.out.println("共通鍵:["+key+"]を登録しました!");
} catch (Exception e) {
System.out.println("登録処理にエラーが起こったから、もう一度登録ください!");
} finally {
if (conn != null)
conn.close();
} }
public static String getCommonKey(){
String key = "";
BufferedReader buf = null;
System.out.println("システム共通鍵を入力してください!");
key=getString(buf);
while(key.equals("")){
System.out.println("入力不正、もう一度入力してください!");
key=getString(buf);
}
return key;
} public static void insertKey(Connection conn, String key)
throws Exception {
Statement statement = null;
ResultSet rs = null;
try {
//1.排他処理
String sqlLock = "select * from mst_key for update";
System.out.println("DEBUG: SQL=" + sqlLock); statement = conn.createStatement();
rs = statement.executeQuery(sqlLock);
//2.クリア処理
if(rs.next()){
String sqlDel = "delete from mst_key";
System.out.println("DEBUG: SQL=" + sqlDel);
statement = conn.createStatement();
statement.execute(sqlDel);
}
//3.登録処理
String sqlIns = "insert into mst_key values ('"+key+"')";
System.out.println("DEBUG: SQL=" + sqlIns); statement = conn.createStatement();
statement.execute(sqlIns);
} finally {
if (statement != null)
statement.close();
if (rs!=null)
rs.close();
}
}
private static String getValue(String keyWord) {
try {
return bundle.getString(keyWord);
} catch (MissingResourceException mre) {
return null;
}
}
private static String getString(BufferedReader buf){
buf = new BufferedReader(new InputStreamReader(System.in));
String str = "";
try {
str=buf.readLine();
} catch (IOException e) {
e.printStackTrace();
}
return str;
}
}手动编译是命令行提示如下C:\cjar>dir
ドライブ C のボリューム ラベルがありません。
ボリューム シリアル番号は 907B-B5A7 です C:\cjar のディレクトリ2010/11/23 13:49 <DIR> .
2010/11/23 13:49 <DIR> ..
2010/11/23 13:48 <DIR> lib
2010/11/23 13:36 94 manifest.mf
2010/11/23 13:49 3,475 publishKey2.class
2010/11/23 13:35 3,116 publishKey2.java
2010/11/22 18:49 85 SqlConn.properties
4 個のファイル 6,770 バイト
3 個のディレクトリ 5,268,205,568 バイトの空き領域C:\cjar>javac publishKey2.javaC:\cjar>java publishKey2
Exception in thread "main" java.lang.ClassNotFoundException: org.postgresql.Driv
er
at java.net.URLClassLoader$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClassInternal(Unknown Source)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Unknown Source)
at publishKey2.main(publishKey2.java:35)C:\cjar>java -cp lib\postgresql-8.4-701.jdbc3.jar publishKey2
Exception in thread "main" java.lang.NoClassDefFoundError: publishKey2找了一些资料还是没解决,Eclipse没问题,这里为什么不行呢。
在线等,希望大家帮忙。
1.编写一小段没有import语句的程序,然后按你以上的步骤执行一下,就可以排除jdk、jre等环境变量问题。
2.查看文字编码是否配置好
----
Java在编译过程中一般会按照以下过程进行:
1,JDK根据编译参数encoding确定源代码字符集.如果不指定该参数,系统会根据操作系统的file.encoding参数来获取操作系统编码格式,国内的windows通常都是GBK.
2,JDK根据上面的字符集信息,将源文件编译成JAVA内部的unicode模式,并将编译后的内容保存到内存中.
3,JDK将内存保存完好的内存信息写入.class文件,生成最后的二进制文件.很多人会发现自己在IDE中配置了源文件字符集为UTF-8(与操作系统默认字符集不同)后,再直接运行javac就会出现错误,这就是因为不加encoding参数的编译过程中会默认使用系统的字符集的。在windows中默认为GBK。IDE中进行编译时,IDE会在编译参数中增加该参数.如果你使用ant来进行编译活动,那么请你确认你的源代码的字符集,然后再相应的ant编译任务中,增加encoding参数
这就意味着, 如果你之前的项目采用的是系统默认的字符集(GBK)来编辑你的源代码,那么如果你改用UTF-8,这就意味你需要重新修改ant脚本,重新打包你的产品。
----
可能出现的问题就是这两方面了,未正常引入import或字符编码未设置。希望你能细心找到错误原因。
35行是什么东西啊?
是不是这个jar包没有添加到工程啊?
1.编了一个没import的,结果没问题
public class hw{
public static void main(String[] args){
System.out.println("abc");
System.out.println("日本語");
System.out.println("中文");
}
}
输出
C:\>javac hw.javaC:\>java hw
abc
日本語
中文2. 我用了日文系统的编码“SHIFT_JIS”编译,结果跟原来的一样,输出如下C:\cjar>javac -encoding SHIFT_JIS publishKey2.javaC:\cjar>java -Dfile.encoding=SHIFT_JIS publishKey2
Exception in thread "main" java.lang.ClassNotFoundException: org.postgresql.Driv
er
at java.net.URLClassLoader$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClassInternal(Unknown Source)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Unknown Source)
at publishKey2.main(publishKey2.java:35)C:\cjar>java -cp lib\postgresql-8.4-701.jdbc3.jar -Dfile.encoding=SHIFT_JIS publ
ishKey2
Exception in thread "main" java.lang.NoClassDefFoundError: publishKey2疑惑啊,为什么呢?
执行的时候也加了 -cp lib\postgresql-8.4-701.jdbc3.jar
这样可以吗?难道还要解压jar包吗?
我在Eclipse里面的设置引入了这个包,在里面也可以执行。
请教一下:如果我用Eclipse,而用手动编译的方式,怎么设置呢?
在里面启动程序没问题。但是我想把生成的java class 拿出去执行,结果就出错了。
我在网上找了些资料,加了-cp 参数,结果还是不行。
然后
是不是Class.forName("org.postgresql.Driver");我用的不对啊,但是一时也没找到其他办法。错误信息C:\cjar>java publishKey2
Exception in thread "main" java.lang.ClassNotFoundException: org.postgresql.Driv
er
at java.net.URLClassLoader$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClassInternal(Unknown Source)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Unknown Source)
at publishKey2.main(publishKey2.java:35)
这个我可能到目前也无能为力了。
Class.forName("org.postgresql.Driver");用的没有问题,要是不对的话,在eclipse里就会出问题,
而现在eclipse没有问题,说明程序是好的,关键是实施的不对,jar包应该放在哪里,我也不清楚了,不好意思哈
执行命令如下
C:\cjar>javac publishKey2.javaC:\cjar>java -cp .;lib\postgresql-8.4-701.jdbc3.jar publishKey2
システム共通鍵を入力してください!
abcef
DEBUG: SQL=select * from mst_key for update
DEBUG: SQL=delete from mst_key
DEBUG: SQL=insert into mst_key values ('abcef')
共通鍵:[abcef]を登録しました!还是对classpath理解不够,java -cp 时,如果只指定lib\postgresql-8.4-701.jdbc3.jar的话,系统就会报
Exception in thread "main" java.lang.NoClassDefFoundError: publishKey2
,只有同时指定了“.;”的话,才会把系统的也引用进来谢谢大家了
执行命令如下
C:\cjar>javac publishKey2.javaC:\cjar>java -cp .;lib\postgresql-8.4-701.jdbc3.jar publishKey2
システム共通鍵を入力してください!
abcef
DEBUG: SQL=select * from mst_key for update
DEBUG: SQL=delete from mst_key
DEBUG: SQL=insert into mst_key values ('abcef')
共通鍵:[abcef]を登録しました!还是对classpath理解不够,java -cp 时,如果只指定lib\postgresql-8.4-701.jdbc3.jar的话,系统就会报
Exception in thread "main" java.lang.NoClassDefFoundError: publishKey2
,只有同时指定了“.;”的话,才会把系统的也引用进来谢谢大家了