Compiling java program from command line passing args - java

I wrote a simple java program
package abc.def.ghi
public class Foobar{
public String printS(String s){
System.Out.println(s);
public static void main(String [] args){
String s = args[0];
Foobar foobar = new Foobar();
foobar.printS(s);
}
Now I did javac Foobar.java
It created a class file
and then I did
java Foobar
Didnt worked
java Foobar hi //args
Didnt worked
java -cp . abc.def.ghi.Main
DIdnt worked.
Error I am getting is:
Exception in thread "main" java.lang.NoClassDefFoundError: abc/def/ghi/Main
Caused by: java.lang.ClassNotFoundException: com.intel.hadoop.graphbuilder.conf.Main
at java.net.URLClassLoader$1.run(URLClassLoader.java:202)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:190)
at java.lang.ClassLoader.loadClass(ClassLoader.java:306)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
at java.lang.ClassLoader.loadClass(ClassLoader.java:247)
Could not find the main class: com.intel.hadoop.graphbuilder.conf.Main. Program will exit.

Did you put your .class files in the path (path to project)/abc/def/ghi/Foobar.class?
NoClassDefFoundError: abc/def/ghi/Foobar
Looks like you didn't.

If you compile using
javac -d . Foobar.java
then the compiler will put the .class file in the right directory to match its package name, then
java abc.def.ghi.Foobar
should run it successfully.

Related

Error: Could not find or load main class InputAddress; Caused by: java.lang.NoClassDefFoundError: inputaddress/InputAddress (wrong name: <filename> ) [duplicate]

I wrote a java program to test RESTful web services by using Netbeans7.0.1 and it works fine there. Now I wrote the build.xml file to compile the code and when I try to run the generated .class file I always got this exception:
Exception in thread "main" java.lang.NoClassDefFoundError: ClientREST (wrong name: clientrest/ClientREST)
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClassCond(ClassLoader.java:632)
at java.lang.ClassLoader.defineClass(ClassLoader.java:616)
at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:141)
at java.net.URLClassLoader.defineClass(URLClassLoader.java:283)
at java.net.URLClassLoader.access$000(URLClassLoader.java:58)
at java.net.URLClassLoader$1.run(URLClassLoader.java:197)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:190)
at java.lang.ClassLoader.loadClass(ClassLoader.java:307)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
at java.lang.ClassLoader.loadClass(ClassLoader.java:248)
Could not find the main class: ClientREST. Program will exit.
The name and path are correct, so any thoughts why I'm getting this exception?
Exception in thread "main" java.lang.NoClassDefFoundError: ClientREST
So, you ran it as java ClientREST. It's expecting a ClientREST.class without any package.
(wrong name: clientrest/ClientREST)
Hey, the class is trying to tell you that it has a package clientrest;. You need to run it from the package root on. Go one folder up so that you're in the folder which in turn contains the clientrest folder representing the package and then execute java clientrest.ClientREST.
You should not go inside the clientrest package folder and execute java ClientREST.
I encountered this error using command line java:
java -cp stuff/src/mypackage Test
where Test.java resides in the package mypackage.
Instead, you need to set the classpath -cp to the base folder, in this case, src, then prepend the package to the file name.
So it will end up looking like this:
java -cp stuff/src mypackage.Test
To further note on Garry's reply: The class path is the base directory where the class itself resides. So if the class file is here -
/home/person/javastuff/classes/package1/subpackage/javaThing.class
You would need to reference the class path as follows:
/home/person/javastuff/classes
So to run from the command line, the full command would be -
java -cp /home/person/javastuff/classes package1/subpackage/javaThing
i.e. the template for the above is
java_executable -cp classpath the_class_itself_within_the_class_path
That's how I finally got mine to work without having the class path in the environment
Probably the location you are generating your classes in doesnt exists on the class path. While running use the jvm arg -verbose while running and check the log whether the class is being loaded or not.
The output will also give you clue as to where the clasess are being loaded from, make sure that your class files are present in that location.
Try the below syntax:
Suppose java File resides here: fm/src/com/gsd/FileName.java
So you can run using the below syntax:
(Make current directory to 'fm')
java src.com.gsd.FileName
Suppose you have class A
and a class B
public class A{
public static void main(String[] args){
....
.....
//creating classB object
new classB();
}
}
class B{
}
this issue can be resolved by moving class B inside of class A and using static keyword
public class A{
public static void main(String[] args){
....
.....
//creating class B
new classB();
static class B{
}
}
Here is my class structure
package org.handson.basics;
public class WithoutMain {
public static void main() {
System.out.println("With main()...");
}
}
To compile this program, I had to use absolute path. So from src/main/java I ran:
javac org/handson/basics/WithoutMain.java
Initially I tried with the below command from basics folder and it didn't work
basics % java WithoutMain
Error: Could not find or load main class WithoutMain
Caused by: java.lang.NoClassDefFoundError: org/handson/basics/WithoutMain (wrong name: WithoutMain)
Later I went back to src\main\java folder and ran the class with relevant package structure, which worked as expected.
java % java org.handson.basics.WithoutMain
With main()...
I also have encountered this error on Windows when using Class.forName() where the class name I use is correct except for case.
My guess is that Java is able to find the file at the path (because Windows paths are case-insensitive) but the parsed class's name does not match the name given to Class.forName().
Fixing the case in the class name argument fixed the error.

My Linked List (with generics) program compiles fine, but when I run it I am getting a class not found exception. Can someone help me?

I am trying to run my linked list in a main method, but for some reason I keep getting the following output from my terminal
Exception in thread "main" java.lang.NoClassDefFoundError: PhoneBook/java
Caused by: java.lang.ClassNotFoundException: PhoneBook.java
at java.net.URLClassLoader$1.run(URLClassLoader.java:202)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:190)
at java.lang.ClassLoader.loadClass(ClassLoader.java:306)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
at java.lang.ClassLoader.loadClass(ClassLoader.java:247)
Below I have listed the part of my class where I run the main method. Everything compiles fine, but when I run it I get that exception. Is there away I can try/catch the exception? Or is there something I am doing wrong?
import java.util.LinkedList;
public class PhoneBook<T> extends LinkedList<T>{
/**
* Creates two books, adds a person to each book, then prints out if it
* found the perons in the book.
**/
public static void main(String[] args){
PhoneBook<String> bookOne = new PhoneBook<String>();
PhoneBook<Integer> bookTwo = new PhoneBook<Integer>();
bookOne.addPerson("Obama");
System.out.print(bookOne.findPerson("Obama"));
bookTwo.addPerson(192590594);
System.out.print(bookTwo.findPerson(192590594));
}
If more information from my class is needed, I can provide it. I don't think it should be necessary because it compiled fine.
After compiling with javac PhoneBook.java are you launching it using
java PhoneBook or java PhoneBook.java ? (it should be the former not
the latter). ~Charlie
Thanks Charlie, that was it.

NoClassDefFoundError running java from the console

When I tried to run Java on Linux on the terminal this is what happens:
Exception in thread "main" java.lang.NoClassDefFoundError: HelloWorldApp/class
Caused by: java.lang.ClassNotFoundException: HelloWorldApp.class
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)
Can anyone help me with this?
EDIT: I was in the folder of the bytecode file and ran this on the terminal:
bash-4.1$ java class HelloWorldApp
Source file:
/**
* The HelloWorldApp class implements an application that
* simply prints "Hello World!" to standard output.
*/
class HelloWorldApp {
public static void main(String[] args) {
System.out.println("Hello World!"); // Display the string.
}
}
You are executing
java HelloWorldApp.class
but it must be
java HelloWorldApp
You may not append class to your call but name plain the classname.
Also, as others has remarked, it is better to use packages as classes in the default package do not work fine.
You also should note that if you have the package declaration in the code it will screw it up even if you try all the fancy fixes like setting CLASSPATH. For example if you have:
package blah;
public class HelloWorld
{
public static void main(String[] args)
{
System.out.println("Hello world");
}
}
The line: package blah;
will cause java HelloWorld to fail after compiling. So remove this line and you should be able to run the src via cmd line.

Attempting to compile/run Java code on Ubuntu, Error: java.lang.NoClassDefFoundError: org/apache/pdfbox/pdmodel/PDDocument

I am attempting to run the following Java code on an Ubuntu system. The code should create a blank PDF file using the pdfbox class:
import org.apache.pdfbox.pdmodel.*;
import java.io.*;
public class BlankPDF {
public static void main(String[] args) {
PDDocument doc = null;
try{
doc = new PDDocument();
} catch (IOException ie){
System.out.println(ie);
}
doc.addPage(new PDPage());
try{
doc.save("Empty PDF.pdf");
doc.close();
} catch (Exception io){
System.out.println(io);
}
}
}
I have the following class dependencies in the same directory as the script:
pdfbox-1.7.0.jar
jempbox-1.7.0.jar
fontbox-1.7.0.jar
commons-logging-1.1.1.jar
I used the following command to compile the script:
sudo javac BlankPDF.java -classpath pdfbox-1.7.0.jar:fontbox-1.7.0.jar:jempbox-1.7.0.jar:commons-logging-1.1.1.jar
Which returned no output and created a .class file (indicating that the compilation worked correctly?)
But when I attempt to run the code using the following command:
sudo java BlankPDF -classpath pdfbox-1.7.0.jar:fontbox-1.7.0.jar:jempbox-1.7.0.jar:commons-logging-1.1.1.jar
I get this error:
Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/pdfbox/pdmodel/PDDocument
at BlankPDF.main(BlankPDF.java:15)
Caused by: java.lang.ClassNotFoundException: org.apache.pdfbox.pdmodel.PDDocument
at java.net.URLClassLoader$1.run(URLClassLoader.java:217)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:205)
at java.lang.ClassLoader.loadClass(ClassLoader.java:321)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:294)
at java.lang.ClassLoader.loadClass(ClassLoader.java:266)
... 1 more
What am I missing?
The name of the class must be the last argument to java. The flags must precede it. If you put flags at the end of the command lines, as here, they are ignored. So:
java -classpath .:pdfbox-1.7.0.jar:fontbox-1.7.0.jar:jempbox-1.7.0.jar:commons-logging-1.1.1.jar BlankPDF
With ref to above answer,don't forget to add current directory (with dot sign) as well in command with jars in classpath
java -classpath hello.jar:. SampleProgram

Assistance needed getting basic JUnit 4 setup to work using java files along with the corresponding command line tools

I am trying to get a basic JUnit 4 setup working using java files along with the corresponding command line tools (before I move on to eclipse - which I suspect will be easier to get working but I still want to do it this way first) and what follows is self-explanatory input/output that I think will bring out the issue(s):
deniz#debian:~$ cd /tmp/temp2/src/com/example/example
deniz#debian:/tmp/temp2/src/com/example/example$ echo $CLASSPATH
.:/home/deniz/CLASSPATH_DIR:/usr/share/java/jogl.jar:/usr/share/java/gluegen-rt.jar:/usr/share/java/junit4.jar
deniz#debian:/tmp/temp2/src/com/example/example$ ls -l /usr/share/java/junit4.jar
lrwxrwxrwx 1 root root 16 Feb 8 2011 /usr/share/java/junit4.jar -> junit4-4.8.2.jar
deniz#debian:/tmp/temp2/src/com/example/example$ ls
MathUtils.java MathUtilsTest.java
deniz#debian:/tmp/temp2/src/com/example/example$ javac ./*.java
deniz#debian:/tmp/temp2/src/com/example/example$ ls
MathUtils.class MathUtils.java MathUtilsTest.class MathUtilsTest.java
deniz#debian:/tmp/temp2/src/com/example/example$ java MathUtils
Exception in thread "main" java.lang.NoClassDefFoundError: MathUtils (wrong name: com/example/example/MathUtils)
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClass(ClassLoader.java:634)
at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:142)
at java.net.URLClassLoader.defineClass(URLClassLoader.java:277)
at java.net.URLClassLoader.access$000(URLClassLoader.java:73)
at java.net.URLClassLoader$1.run(URLClassLoader.java:212)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:205)
at java.lang.ClassLoader.loadClass(ClassLoader.java:321)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:294)
at java.lang.ClassLoader.loadClass(ClassLoader.java:266)
at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:334)
Could not find the main class: MathUtils. Program will exit.
deniz#debian:/tmp/temp2/src/com/example/example$ java MathUtilsTest
Exception in thread "main" java.lang.NoClassDefFoundError: MathUtilsTest (wrong name: com/example/example/MathUtilsTest)
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClass(ClassLoader.java:634)
at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:142)
at java.net.URLClassLoader.defineClass(URLClassLoader.java:277)
at java.net.URLClassLoader.access$000(URLClassLoader.java:73)
at java.net.URLClassLoader$1.run(URLClassLoader.java:212)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:205)
at java.lang.ClassLoader.loadClass(ClassLoader.java:321)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:294)
at java.lang.ClassLoader.loadClass(ClassLoader.java:266)
at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:334)
Could not find the main class: MathUtilsTest. Program will exit.
deniz#debian:/tmp/temp2/src/com/example/example$ cat MathUtils.java
package com.example.example;
public class MathUtils
{
public static double multiply(double a, double b)
{
return a * b;
}
public static void main(String[] args)
{
double num = multiply(4.0,-5.0);
System.out.println("The number is: " + num);
}
}
deniz#debian:/tmp/temp2/src/com/example/example$ cat MathUtilsTest.java
package com.example.example;
import org.junit.Test;
import static org.junit.Assert.*;
public class MathUtilsTest
{
#Test
public void testMultiply()
{
double a = 5.0;
double b = -4.0;
double expected = -20.0;
double result = MathUtils.multiply(a, b);
assertEquals(expected, result, 0.00001);
}
}
deniz#debian:/tmp/temp2/src/com/example/example$
Could someone please tell me what's going on and how to solve this? Even the MathUtils class doesn't work and it seems well done to me; it has the package defined and has a main method.
If more information than what I provided is needed, just ask.
I apologize if I made any stupid mistakes and/or assumptions because I am extremely tired as I type this; please correct me nonetheless.
Any help would be greatly appreciated!
Thanks in advance!
Your MathUtilsTest has a package declaration:
package com.example.example;
For various reasons, this means that your physical file must be in com/example/example/MathsUtilsTest. So you need to run java from /tmp/temp2/src/.
java should be able to find the classes correctly then.
It's a good idea to always run javac from that directory as well.
First try: Use the complete class name (with package)
java com.example.example.MathUtils
Second try: Add the classpath
java -cp . com.example.example.MathUtils
And mind the directory structure
To run the tests you need to follow the instructions in Running tests

Categories