Java command line compile - java

I recently finished a programming assignment using Netbeans and Java. When I run the program in Netbeans everything runs perfect. My issue comes when I try and run it from the terminal. I run javac with all my .java files and it produces a .class file for each one. Then when I try to run the executable it errors out on me.
ls
Board.java KenKenPuzzleSolver.java
Solver.java Cage.java
Size.java Values.java
javac KenKenPuzzleSolver.java Board.java Cage.java Size.java Solver.java Values.java
ls
Board.class Size.class
Board.java Size.java
Cage.class Solver.class
Cage.java Solver.java
KenKenPuzzleSolver.class Values.class
KenKenPuzzleSolver.java Values.java
java KenKenPuzzleSolver
Exception in thread "main" java.lang.NoClassDefFoundError: KenKenPuzzleSolver (wrong name: kenkenpuzzlesolver/KenKenPuzzleSolver)
Also, when the program is run from the terminal it should take in an input file and the name of an output file i.e. "java KenKenPuzzleSolver input.txt output.txt", and I am not quite sure how to do this.

Why don't you generate an executable with netbeans? You don't have to compile your code every time.
input.txt and output.txt are called arguments. They will be stored in the String[] args array in you main function.

You did not provide the classpath parameter.
Use javac -cp YOURCLASSPATH KenKenPuzzleSolver

The class that you are running should be public and should have the class name same as your file name. It seems like your file name is "KenKenPuzzleSolver" but your class name seems to be "KenkenPuzzleSolver" ( Observe the lowercase ken ). correct that and recompile and re run.

You must have messed with cases, from error its obvious instead of KenKenPuzzleSolver you have named your class as kenkenpuzzlesolverin KenKenPuzzleSolver.java.

Related

"java.lang.ClassNotFoundException: oracle.jdbc.driver.OracleDriver" error when running from terminal

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

How to use a jar file in conjunction with another in Java from the command prompt

I am doing an assignment for class and I was given a jar file for the Display board and I have to code some basic functions for it in a separate Java file. Now, I need the Display to be called in the program I'm writing, and so I used:
(I'm using a window PC)
javac -cp lab1.jar;.Wrapping.java
However, every time I try this, it gives me this error:
javac: no source files
usage: javac <options><source files>
use -help...
Now, I've changed my directory to the proper folder and the file is in there, and still, I cannot get it to run. What am I doing wrong? I can get other programs to compile and run from the command prompt, it is just this one that I cannot. Any help?
If your command it posted as you ran it literally, without the space between . and Wrapping.java, you need a space there.
Try this command to compile:
set classpath=lab1.jar
javac Wrapping.java
OR
javac -cp lab1.jar Wrapping.java
Try this command to execute:
set classpath=lab1.jar;.
java Wrapping ::Make sure its class name with main()
OR
java -cp lab1.jar;. Wrapping

Running java from command line with packages: NoClassDefFoundError

I had a fully functional Java program that I am now trying to put into a package instead of having it in the default package. All of the .java files are in the mymap package, and I am trying to run black box tests in my MyMapTest.java file. The problem is that I don't know how to properly run this in the command line now that I have it in a package. I got stuck trying to google for answers.
I can get it to compile:
javac -cp "../junit.jar" *.java;
But when I try to run it as I previously had...
java -cp .:../junit.jar org.junit.runner.JUnitCore MyMapTest
I get the error java.lang.NoClassDefFoundError: mymap/MyMap for all of my tests. I guess the issue is that somehow I am not properly including mymap, perhaps in the classpath?
Similarly, I also have white box tests in MyMapWhiteBoxTest.java, where this file is also in the mymap package. I cannot figure out how to properly run these tests either. Trying to run it the same way, I get:
Exception in thread "main" java.lang.NoClassDefFoundError: MyMapWhiteBoxTest (wrong name: mymap/MyMapWhiteBoxTest)
For both cases, I have tried mymap/FILE or somehow also including the package in the classpath, but I'm definitely missing something.
If the main() is present in MyMapTest class then you can try the below command :
java -cp .:../junit.jar org.junit.runner.JUnitCore myMap.MyMapTest
After compilation: (javac -cp "../junit.jar" *.java;) what do you have in your current dir? You should have the file ./myMap/MyMapTest.class.

Problems running a java program from the command line interface

I created a java program in Eclipse. When I run the program in Eclipse ("run as -> Java Application") the program runs fine and I have the correct output. However, when I try to run the program in the command line interface I got this error:
Exception in thread "main" java.lang.NoClassDefFoundError: HelloWorld (wrong name: helloworld/HelloWorld)
Could not find the main class: HelloWorld. Program will exit.
The class file is in directory bin and I try to run it with the command:
java HelloWorld
Since your class is in the package helloworld you should run it like this:
java helloworld.HelloWorld
Also make sure "." is on your classpath.
I try to compile it with the command:
java HelloWorld
TO compile a java program you should use javac command like
javac Helloworld.java
Are you sure that the directory where your classes are is in the classpath? Typically, in your project directory, the "classes" or "lib" directory.
If you are running from that directory, you could try adding ".".
See the -cp parameter of java runtime executable.

Cannot run simple compiled java program?

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.

Categories

Resources