(Java) Proper use of command line execution

淺唱寂寞╮ 提交于 2019-12-13 05:14:00

问题


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.

I perform the following to compile

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

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.

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

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)

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.

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);
            }
        }
    }
}

回答1:


When your running from command prompt after compiling your class use this

java -classpath .;mysql-connector-java-5.0.8-bin.jar Version

Keep both jar and java file in same directory and try. use ";" instead of ":" as mentioned above.include package name if any.I have tested its working.




回答2:


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.

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

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

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

The following command executes the program:

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


来源:https://stackoverflow.com/questions/23330160/java-proper-use-of-command-line-execution

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!