• 0

[java] command line compile and run


Question

I am having a bit of trouble with a class project. The code works fine in NetBeans but I cant get it working if i compile and run it from the command window. I have two files: hw3.java and ojdbc14.jar (oracle JDBC driver).

The first thing I did to compile was:

javac -classpath ojdbc14.jar hw3.java

and to run:

java -classpath ojdbc.jar hw3

this is the error i get:

Caused by: java.lang.ClassNotFoundException: hw3

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)

Could not find the main class: hw3. Program will exit.

I think its looking in the ojdbc14.jar file for the 'hw3' class. Netbeans wraps it inot a nice jar file (im looking to into doing that) which i can execute just fine with "java -jar Homework3.jar". I wish netbeans would show the compile instructions in the output window. I just need help on getting the "javac" compile and "java" to work.

Link to comment
Share on other sites

5 answers to this question

Recommended Posts

  • 0

It can't find your hw3.class file. Is it in the jar file? If it is, you would use "java -jarfile ojdbc.jar", adding the path to the jar file if necessary.

If hw3.class is in your current working directory, then I don't know why it can't find it... Actually, try that!

java -classpath ojdbc.jar:. hw3

IIRC, the class path never seems to include the current working directory, so you need to add it manually. The ':' (colon) is the separator. You can use ZIP files, JAR files and directories, according to the output of "java -h" on my machine.

Link to comment
Share on other sites

  • 0

java is case sensitive... file name has to be the same case and name of the class.

in your source code do you have the following?

public class hw3 {
....
}

or is it

public class Hw3 {
....
}

if it is, then you need to rename the file to Hw3.java - compile it and the run it.

Link to comment
Share on other sites

  • 0

Nevermind I solved the problem. It had to do with my path and classpath system variables on my PC. I connected to the school server via ssh and everything compiles and runs fine. I just had to knock out -classpath in running the program to get "java hw3".

Link to comment
Share on other sites

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.