(Java)正确使用命令行执行 [英] (Java) Proper use of command line execution

查看:101
本文介绍了(Java)正确使用命令行执行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在学习有关将Java类连接到MySQL的教程( http://zetcode.com/db/mysqljava/).我可以使代码在Eclipse中编译和运行,但是尝试在命令行上运行时我做错了.我正在使用OSX的终端btw.

I'm following a tutorial on connecting a java class to MySQL (http://zetcode.com/db/mysqljava/). I can get the code to compile and run in Eclipse, but I'm doing something wrong when trying to run it at the command line. I'm using OSX's terminal btw.

我执行以下编译

cd dropbox/workspace/mysqltut/src/zetcode
javac Version.java

这将成功创建Version.class文件.

This successfully creates the Version.class file.

我的问题的简单版本是,如何从命令行运行它?以下详细说明了我使用本教程显示的代码行的尝试以及为使类文件正确运行所做的努力.

The simple version of my question is, how do I run this from the command line? The following details my attempts using the line that the tutorial shows as well as my variations in an effort to get the class file to run properly.

如果我尝试使用教程命令行:java -cp .:lib/mysql-connector-java-5.1.30-bin.jar zetcode/Version.java来运行它,我会得到Error: Could not find or load main class zetcode.Version

If I try to run it using the tutorials command line: java -cp .:lib/mysql-connector-java-5.1.30-bin.jar zetcode/Version.java, I get Error: Could not find or load main class zetcode.Version

所以我想,哦,我已经在zetcode中了,所以我将其更改并使用java -cp .:lib/mysql-connector-java-5.1.30-bin.jar Version.java,这次我得到以下信息:

So I figure, oh I'm already in zetcode so I alter it and use java -cp .:lib/mysql-connector-java-5.1.30-bin.jar Version.java, and this time I get the following:

Exception in thread "main" java.lang.NoClassDefFoundError: Version (wrong name: zetcode/Version)
    at java.lang.ClassLoader.defineClass1(Native Method)
    at java.lang.ClassLoader.defineClass(ClassLoader.java:800)
    at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:142)
    at java.net.URLClassLoader.defineClass(URLClassLoader.java:449)
    at java.net.URLClassLoader.access$100(URLClassLoader.java:71)
    at java.net.URLClassLoader$1.run(URLClassLoader.java:361)
    at java.net.URLClassLoader$1.run(URLClassLoader.java:355)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(URLClassLoader.java:354)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:425)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:358)
    at sun.launcher.LauncherHelper.checkAndLoadMain(LauncherHelper.java:482)

不确定wrong name: zetcode/Version是什么意思.我已经在zetcode中了.请记住,在lib之前我找不到开关.:的含义.我刚刚结束本教程.所以我尝试将开关更改为../,因为libzetcode高了一个级别(并且与src处于同一级别).然后我又得到了Error: Could not find or load main class Version.

Not sure what wrong name: zetcode/Version means. I'm already in zetcode. Bear in mind I can't find what the switch .: means beforelib. I'm just going off the tutorial. So I tried changing that switch to ../ instead since lib is one level up from zetcode (and on the same level as src). And I get the Error: Could not find or load main class Version again.

我显然是jsut错误地处理了执行语法,因为它在IDE中运行良好.提前致谢.

I'm obviously jsut handling the syntax for execution incorectly since it runs fine inside the IDE. Thnks in advance.

哦,如果您需要源代码,我正在使用:

oh in case you need the source code, I'm using:

package zetcode;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.logging.Level;
import java.util.logging.Logger;

public class Version {

    public static void main(String[] args) {

        Connection con = null;
        Statement st = null;
        ResultSet rs = null;

        String url = "jdbc:mysql://localhost:3306/";
        String user = "root";
        String password = "";

        try {
            con = DriverManager.getConnection(url, user, password);
            st = con.createStatement();
            rs = st.executeQuery("SELECT VERSION()");

            if (rs.next()) {
                System.out.println(rs.getString(1));
            }

        } catch (SQLException ex) {
            Logger lgr = Logger.getLogger(Version.class.getName());
            lgr.log(Level.SEVERE, ex.getMessage(), ex);

        } finally {
            try {
                if (rs != null) {
                    rs.close();
                }
                if (st != null) {
                    st.close();
                }
                if (con != null) {
                    con.close();
                }

            } catch (SQLException ex) {
                Logger lgr = Logger.getLogger(Version.class.getName());
                lgr.log(Level.WARNING, ex.getMessage(), ex);
            }
        }
    }
}

推荐答案

您不应从src目录编译源代码.编译中 应该从项目目录中完成.项目目录包含 bin,src和lib目录.

You should not compile your sources from the src directory. Compiling should be done from the project directory. The project directory contains the bin, src, and lib directories.

Linux tree命令以树状格式列出我们的项目目录.

The Linux tree command list us our project directory in a tree-like format.

$ tree
.
├── bin
│   └── zetcode
│       └── Version.class
├── lib
│   └── mysql-connector-java-5.1.31-bin.jar
└── src
    └── zetcode
        └── Version.java

二进制文件进入bin目录,库文件进入lib库,源文件进入src目录.请注意,目录必须与软件包名称匹配.

Binaries go into the bin directory, libraries into the lib, and sources into the src directory. Note that directories must match the package names.

要编译源代码,我们使用以下命令:

To compile the sources, we use the following command:

$ javac -d bin src/zetcode/Version.java

使用-d选项,我们为创建的类文件设置目录.

With the -d option, we set the directory for the created class files.

以下命令执行程序:

$ java -cp bin:lib/mysql-connector-java-5.1.31-bin.jar zetcode.Version 
5.5.37-0ubuntu0.14.04.1

这篇关于(Java)正确使用命令行执行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆