My source files are in this folder: c:\data\mycompany.
All of my source files contain the following as the first line: package mycompany; Now from the c:\data folder, I compiled everything using this command: javac mycompany/*.java -extdirs c:\some\other\folder\with\libs. This compiles fine. Now when I try to execute it (again from c:\data) using this command: java mycompany/test then i get this error:
Exception in thread "main"
java.lang.NoClassDefFoundError:
mycompany/test Caused by:
java.lang.ClassNotFoundException:
mycompany.test at java.net.URLClassLoader$1.run(Unknown Source)
I also tried the below command but it reproduces the same error:
java mycompany/test -extdirs c:\some\other\folder\with\libs
Is this the proper way to compile/run?
Here is my source-code:
package MyCompany;
public class Test
{
public static void main(String[] args)
{
System.out.println("test");
}
}
You need to set the classpath. See for example this SO question.
You shouldn't be using extdirs when invoking java, you should be setting your classpath with -cp
By the way, when invoking a main java class, you should provide the class name, not the path to the class, hence, it's likely mycompany.test (if your class that contains main is named test), not mycompany/test. It's not an error as Java fixes it for you.
that is saying that the .class files are not on the classpath how you are compiling should be fine, you need to add the directory with the resulting .class files to your classpath when you try and run your test code.
java -cp <path to classes> classtorun
so your example might look like
java -cp <path to classes>;<path to libs> mycompany.Test
you should really look at ANT to automate your compile and build to an executable .jar file. Nobody does this fiddly stuff by hand because of all the repetitive typing and chances for errors. ANT was created to avoid all this minutia and let you concentrate on solving coding problems and not struggling with the command line tools. Read this.
Try this to compile:
mkdir classes
javac -d classes *.java
Only create the /classes directory the first time. The -d directory tells javac to put your .class file under /classes.
To run, do this:
java -cp .;classes MyCompany.Test
This should work fine.
Related
Probably this is a repeated question, but I just couldn't find an answer to what I am looking for! I am trying to compile and run a java class in a Unix box.
I have the class as:
package tmp.test;
import org.jasypt.registry.AlgorithmRegistry;
class Algo {
public static void main(String[] args) {
System.out.println(AlgorithmRegistry.getAllPBEAlgorithms());
}
}
The files are in the path /tmp/test/. Now I compile the class with the command:
javac -cp jasypt-1.9.3.jar Algo.java
The JAR file is in the same directory. It compiles just fine. But when I run the class file with the command:
java -cp jasypt-1.9.3.jar Algo
I get the error:
Error: Could not find or load main class Algo
I am executing all the commands from the path /tmp/test/.
I tried:
java -cp jasypt-1.9.3.jar tmp.test.Algo
and
java -cp jasypt-1.9.3.jar tmp/test/Algo
Both throw the same error.
I am not sure what I am doing wrong. At first I thought it was the problem of the access thing. So I changed everything using chmod to 777. Everything seems to be fine. Can you please let me know what I am missing here?
I am executing all the commands from the path /tmp/test/
That is the problem. You need to be one level above tmp, not somewhere inside. Then your command line
java -cp /path/to/jasypt-1.9.3.jar tmp.test.Algo
should work. If you insist in starting Java from the subdirectory inside your classpath, you can do this quite contrived thing:
java -cp /path/to/jasypt-1.9.3.jar:../.. tmp.test.Algo
tl;dr
Use the switch -d to compile and then use the fully qualified name of the class to run it.
Compile the class as follows:
javac -d . -cp jasypt-1.9.3.jar Algo.java
The switch, -d specifies where to place generated class files and . stands for the current directory.
Run the class as follows:
java -cp jasypt-1.9.3.jar tmp.test.Algo
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 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.
I know this question has been asked and answered a number of times. But I somehow am not able to get this right. I have a package having the following structure
model/
InputDetails.java
RelationDetails.java
Now the file RelationDetails has the following structure:
package model;
public class RelationDetails {
....
}
And the file InputDetails has the following structure
package model;
public class InputDetails {
.....
}
Now I have compiled the RelationDetails.java file that creates a RelationDetails.class file in the same directory.
But when I try to compile the InputDetails.java file, It shows the error
Symbol not found
wherever RelationDetails has been used. Where am I going wrong??
I'd recommend using an IDE like Eclipse or IntelliJ IDEA. They will do the compiling for you. Or use Ant, Gradle or Maven to compile. I am a professional Java developer and I cannot remember the last time I used javac from the command line. There's no need for it.
If you insist on using javac directly, either compile both files together from the appropriate source folder (the directory above "model").:
javac "model/InputDetails.java" "model/RelationDetails.java"
Or, if you want to compile them separately:
javac -classpath . "model/InputDetails.java"
javac -classpath . "model/RelationDetails.java"
The -classpath . bit adds the current folder to the classpath for the javac executable, so it can find the previously compiled class and you won't get the 'Symbol not found' errors.
$ pwd
/tmp/model
$ ls
InputDetails.java RelationDetails.java
$ javac InputDetails.java RelationDetails.java
$ ls *.class
InputDetails.class RelationDetails.class
I am just tried in my eclipse nothing will be showing errors, better to user Eclipse or STS they will help you like this problems easily I think so..
compile with fully qualifier name.
javac model\YourClass.java
There are some encoding character in my code so I compile the code as follow:
javac -cp cayenne-2.0.4.jar Twokenize.java
It's ok. But when I run the program, it return an error:
java -cp cayenne-2.0.4.jar Twokenize test.txt
Here is an error:
Error: Could not find or load main class Twokenize
But when I just run:
java Twokenize test.txt
The program (main method) still runs smoothly util it meets the code that require external library.
Please help me. Thank you very much.
when you use -cp it looses current directory from classpath so you need to specify explicitly
Use
java -cp .:cayenne-2.0.4.jar Twokenize test.txt