I've been trying to compile java programs, and every single time these errors show up:
I'm compiling using cmd, and javac test.java works fine, returning no errors, but java test is resulting in the string of errors. What is the problem?
EDIT:
Here is the result of javac -version and java -version.
wrong name: Test
Your Test class is defined in a package but you didn't provide its package name on the command line.
To run a java program from the command-line, you need to consider the following:
Specify the fully qualified name of the main class you want to run, i.e.:
java com.somepackage.MainClass
Add the main class plus any dependent classes/jars to the classpath.
See: Setting the Class Path.
Try running these commands with cmd:
javac -version
java -version
If one of them is not set properly you won't see your JDK version so you have to set right your environment variables.
Related
I have a program that I run from Eclipse successfully.
However, when I want to run it from terminal, I encounter the famous error:
"java.lang.ClassNotFoundException: oracle.jdbc.driver.OracleDriver"
on this line:
Class drvClass = Class.forName("oracle.jdbc.driver.OracleDriver");
PS:
I have the following in CLASSPATH:
/oracle/jdbc/lib/ojdbc6.jar
Also note that I compile it successfully (javac Test2.java). Then when I run it (java Test2), I get the following error:
Error: Could not find or load main class Test2
So I run:
java -classpath ~/Desktop/JDBC2/src Test2
It runs, but I get the above "ClassNotFoundException" though.
I found this question tricky: the reason is related to semicolon after jar file address.
At first I changed the directory of MySample.java to another directory (you can don't do that) like C:\
then I removed package address from the source code, at the end I run this command in cmd
java -cp path_to_oracle_driver.jar; MySample
P.S. If you want run it from terminal you have to remove package PackageAddress from the source code and compile it again.
As #yngwietiger mentioned above in the comments, using -classpath parameter when running the .class file, overrides the original CLASSPATH and the predefined ojdbc6.jar file. So we need to mention both when running:
java -classpath ~/Desktop/JDBC2/src:/oracle/jdbc/lib/ojdbc6.jar Test2
Or, as a better solution, we can add the current path to CLASSPATH (note the colon and dot at the end):
export CLASSPATH=$CLASSPATH:.
And, in order to run, we just need to type:
Java Test2
I cant help myself but to finally ask here... I am using Windows 8.1 and I always need to copy the JUnit.jar files to my active projects, because whatever I try its not working. At the moment my classpath is set to:
C:\Program Files\junit\junit.jar;C:\Program Files\junit\junit-4.4.jar;C:\Program Files\junit\junit-4.11.jar
JUNIT_HOME is set to C:\Program Files\junit
Whenever I try to run a test using eg.: java junit-4.4.jar org.junit.runner.JUnitCore MyMatrixTest
I get the following error saying main class junit-4.4.jar not found. I also tried using only one version of junit in my classpath (even if it would be good to have all of them included, since I often need to run tests from different versions), but it didnt make a change.. I just dont know what to try to make it working..
Thanks for your help!
If you have a CLASSPATH environment variable set correctly, then you need to execute
java org.junit.runner.JUnitCore
without the reference to the .jar file.
The java application launcher has the following usage
java [ options ] class [ arguments ]
If you aren't going to specify any options, then the first argument needs to be the class name. In this case, that is org.junit.runner.JUnitCore.
All you have to do is:
java -cp "C:\Program Files\junit\junit-4.4.jar";. org.junit.runner.JUnitCore MyMatrixTest
I am very new to groovy and I am trying out this example from the Groovy in Action book. I have this fibonacci.groovy program and when trying to run the program with java command, I am getting the NoClassDefFound error.
The command I am using in the console is:
java -cp %GROOVY_HOME%/embeddable/groovy-all-2.2.0.jar;classes fibonacci
As you can see, I have mentioned the groovy-all jar in the classpath and I set the GROOVY_HOME variable. The classpath variable is not set, so I am assuming it has the default '.' value to find in the current folder itself. What am I doing wrong?
Aren't you missing the current folder in the classpath?
I'm on Linux, but if i compile a Groovy class with groovyc and then try to run it with java, i need to tell java where is my groovy-all.jar and also add the current dir to the classpath
So, this compilation works:
$ groovyc Fib.groovy
But this run doesn't runs:
$ java -cp $GROOVY_HOME/embeddable/groovy-all-2.2.0.jar:classes Fib
As it's missing the current dir in the path:
$ java -cp $GROOVY_HOME/embeddable/groovy-all-2.2.0.jar:. Fib
test for fib
Also note that if fibonacci is in a package, you need to type the full path to the class. So for this groovy source:
package up.foo
println "test for fib"
Compile:
$ groovyc Fib.groovy
We write the full package path to execute:
$ java -cp $GROOVY_HOME/embeddable/groovy-all-2.2.0.jar:. up.foo.Fib
There it is:
test for fib
I am on Arch Linux, I just installed JRE and JDK and all the proper bin files (javac and java) are in /opt/java/bin/
I simply compiled a standard hello world, and compiled it with javac running javac ./hello.java and that made a class.
Now my problem is running it. I run java ./helloworld.class and it gives me an error, even if the file I point java to is non-existant:
Exception in thread "main" java.lang.NoClassDefFoundError: //helloworld/class
Caused by: java.lang.ClassNotFoundException: ..helloworld.class
(..omitted for clarity..)
Could not find the main class: ./helloworld.class. Program will exit.
You will notice the first line of the error, it munges the path //helloworld/class
When I feed java an absolute path, i.e java /home/foo/helloworld.class it gives the same error, but replaces the path's / with . in the first line, again munged.
What do you think is wrong? I really don't know why it is doing this..
When you run java, you just pass it the fully qualified class name (including package), not the file name.
java helloworld will look for helloworld.class.
java helloworld.class will look for helloworld/class.class
You do not run a file as
# java file.class
you run it as
# javac PATH/file.java
# java PATH/file
Do not add .class while using JAVA command.
Actually you should compile it like this
javac helloword.java
run the program
java helloword
And yet another thing: add command line option "-classpath ." or it short version "-cp .", i.e. your command line should look like:
java -cp . helloworld
this is if your class is in your current directory. Otherwise "." should be replaced by path where the class(es) may be found.
Ok, I am beginner in JAVA. I have just started. I downloaded Java SE Development Kit 6u21 and wrote a program, saved it in .java and try to run it, but I can not do it. What's wrong? Thank you.
You will need to compile it first using javac:
javac YourClass.java
And then run:
java YourClass
If you really want to do it manually, you have to use the javac compiler in command line like this :
javac package/of/your/project/YourClass.java
and then
java package.of.your.project.YourClass
Your class YourClass must have a public static void main(String... args) method.
If your class isn't in a package, then javac YourClass.java and java YourClass are sufficient.
You should really consider to use an IDE which will handle this for you.
Resources :
javac documentation
java documentation
On the same topic :
Compiling multiple packages using the command line in Java
Compiling/running a java program in a different directory.
I suggest you read and follow this tutorial by Oracle: "Hello World!" for Microsoft Windows. Once you have successfully done so, you should have JDK installed and know how to run your program.
If you are still getting "'javac' is not recognized as an internal or external command, operable program or batch file", try reading these: How do I set or change the PATH system variable? and PATH and CLASSPATH.