java command classpath issue - java

I am having strange issue in this simple scenario.
I am having a jar file which contains following class:
package com.example;
public class Test{
public void perform(){
System.out.println("Performing testing one");
}
}
And I have created a Main class to call the perform method as followed:
import com.example.Test;
public class Main{
public static void main(String[] args) {
Test test=new Test();
test.perform();
}
}
I have put both the jar and Main.java file in same folder and successfully compiled the Main.java file using following command:
javac -cp ".\*" Main.java
But when I am trying to run the Main class using following command:
java -cp ".\*" Main
It gives following error:
Error: Could not find or load main class Main
If I try to run Main class without -cp argument it gives following error:
Exception in thread "main" java.lang.NoClassDefFoundError:
com/example/Test
at Main.main(Main.java:5) Caused by: java.lang.ClassNotFoundException: com.example.Test
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)
... 1 more
The syntax is correct then what I am doing wrong here...?

When using -cp you need to specify jar names one by one. Using * does not usually add anything to the classpath. Good luck!

Everything is wrong.
The source code should be in a directory called com/example.
You should be in the directory containing com.
The compiler command is
javac com/example/Main.java
The execute command is
java com.example.Main
You don't need a JAR file with only one class in it.

After a lot of experiment and searches on net, I have finally found that we need to put ; after classpath to make it work.
So following command does run the Main class:
java -cp ".\*"; Main

Related

Java package run-time error

I am newbie in java. I am running java programs via Ubuntu terminal
I just started java package topic and have been dealing with a problem for several hours.
I tried to create a simple package called pack which has single class Hello.
I created a directory pack. Inside the pack I put Hello.class file intp pack directory via
javac -d ./pack Hello.java
command.
Then I included pack package into a class containing main method. The class's name is test and it is located in test.java file This class is located in another directory. I compile via
javac -cp ./pack test.java
It compiles without any error and everything is ok.
However, when I enter command
java -cp ./pack test
it gives me
Error: Could not find or load main class test
When I tried
java test
command. The following message showed up
Exception in thread "main" java.lang.NoClassDefFoundError: pack/Hello
at test.main(test.java:6)
Caused by: java.lang.ClassNotFoundException: pack.Hello
at java.net.URLClassLoader$1.run(URLClassLoader.java:366)
at java.net.URLClassLoader$1.run(URLClassLoader.java:355)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:354)
at java.lang.ClassLoader.loadClass(ClassLoader.java:425)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
at java.lang.ClassLoader.loadClass(ClassLoader.java:358)
... 1 more
Can anybody explain me what I am doing wrong? Any help is much appreciated.
Sorry, I did bot include my source codes. Here they are.
import pack.Hello;
public class test
{
public static void main(String args[])
{
Hello.HelloMessage();
}
}
This is test.java file which tests if everything is ok. It is located at
/home/uesername/apps
directory.
Then I created "pack" directory. Full path to the pack directory is
home/username/apps/pack
Inside the "pack" I put Hello.java file. The content of Hello.java file is
package pack;
public class Hello
{
public static void HelloMessage()
{
System.out.println("hello, world");
}
}
To begin with I suggest you use an IDE to setup you environment for compiling, running and debugging.
The problem you have is that you have compiled with the wrong path.
javac -cp . pack/Hello.java
javac -cp . pack/test.java
and
java -cp . pack.Hello
or
java -cp . pack.test
The problem is that you compiled a class with package pack in a directory pack and you would end up with
pack/pack/Hello.class
I suggest you check where the Hello.class file has been placed.

Why am I getting a NoClassDefFoundError when I run a Java class file from the command line in Linux?

I am trying to run a test.class file from the linux command line. I compiled the file using javac test.java which produced the test.class file. When I run the command java test it throws a class not found exception. I also tried specifying the package name with the same result. Here is the output, can anybody help? I believe my syntax is correct according to Google searches.
[root#localhost usr]# java test
Exception in thread "main" java.lang.NoClassDefFoundError: test (wrong name: testJava/test)
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)
Could not find the main class: test. Program will exit.
You need to compile your .java file with javac:
javac MyFile.java
Then run the file using java
java MyFile
If you're doing this and it still doesn't work, that means you need to make sure the file and the class name inside the file have the same name... so MyFile.java contains
class MyFile
{
// ...
public static void main(String[] args)
{
// ...
}
// ...
}
Use
java testJava.test from the parent directory of testJava
Assuming your class looks like this:
package javaTest;
public class test{
public static void main(String[] a){
System.out.println("Test");
}
}
Then to compile and run the file, you basically do this:
$ ls
testJava
$ ls testJava
test.java
$ javac testJava/test.java
$ java testJava.test
Test
What this means is that you have to run the class with its fully qualified name, which includes the package name of the class.You also have to do it from the directory that conatins the root of your package. (Unless you specify the "root" directory with the -cp flag.)
I too had problem initially.
java -cp . Test

cannot run a simple java code

I have downloaded a java developers kit for my 64bit windows 7, wrote down my code in the notepad, though the code is compiling from the command prompt and creating a .class file, but its refusing to run showing the error code:
java.lang.NoClassDefFoundError: first Caused by: java.lang.ClassNotFoundException: first
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)
Could not find the main class: first. Program will exit. Exception in thread "main"
I have made sure more than once that the file name and the class name are exactly same(i have kept them smallcase 'a' just to be sure). But still no avail, could you please suggest a few solutions please.. I'm new to java i'm basically a C/C++ programmer.
A java program has this basic structure:
ClassName.java
public class ClassName
{
public static void main(String[] args)
{
}
}
try using this outline to generate your code.
compile and run with:
javac ClassName.java
java ClassName
I used to get this error when I ran a Class file.
Try doing: java NameOfClass
You don't need the .java extension when running, but you need it for compiling. That was always the problem I used to have.
Did you set your classpath?
http://download.oracle.com/javase/1.3/docs/tooldocs/win32/classpath.html
java -classpath <path> <classname>
You must provide with the code.
Anyway, from the Java Docs, class ClassNotFoundException:
Thrown when an application tries to
load in a class through its string
name using:
The forName method in class Class.
The findSystemClass method in class ClassLoader.
The loadClass method in class ClassLoader.
but no definition for the class with the specified name
could be found.
Must read link : Tip: Causes of java.lang.ClassNotFoundException
It is quite possible after compiling your code you would be writing java filename.java.
Because this sought of exception occurs then.
After compiling your program using javac filename.java use the command to start your interpreter java filename.
As you enter this command java interpreter automatically starts interpreting filename.class.
commands :
javac filename.java // to start compiling
java filename // to start interpreting

NoClassDefFoundError found!

import java.io.*;
public class ArrayApp{
public static void main(String[] args){
System.out.println("lllll");
} // end main()
} // end class ArrayApp
i get this error when i run my application after compiling it.
Exception in thread "main" java.lang.NoClassDefFoundError: ArrayApp
Caused by: java.lang.ClassNotFoundException: ArrayApp
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:307)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
at java.lang.ClassLoader.loadClass(ClassLoader.java:248)
Could not find the main class: ArrayApp. Program will exit.
You need to make sure your class file is in the classpath. Assuming you are using the default package (i.e. no package declaration), you need to tell Java where to find your class when you run it. So let's assume your ArrayApp.class file is in the same directory. You will need to run it like this:
java -cp . ArrayApp
The option -cp and the following . tell Java that the classes will be in the current directory. The longer name for -cp is -classpath, so you can use that as well.
Also note the space between the classpath and the class name. The path is the base directory of where your class files are located. If you compiled them into a directory named "bin" then you would change the way you call Java like this:
java -cp bin/ ArrayApp
The "ArrayApp" is the fully qualified class name.
Your classpath is incorrect. Try...java -classpath . ArrayApp

java - missing main class

I'm trying to run one of the very first examples from the book "Head First Java";
public class MyFirstApp {
public static void main (String[] args){
System.out.println("I Rule!");
System.out.println("The Worlds!");
}
}
"javac" created a .class file from the .java file - but "java" complains about a "missing main class" when trying to run the .class file (i also tried java -cp . "..." with the same result):
C:\>java \hfj\MyFirstApp.class
Exception in thread "main" java.lang.NoClassDefFoundError: \hfj\MyFirstApp/class
Caused by: java.lang.ClassNotFoundException: \hfj\MyFirstApp.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)
Could not find the main class: \hfj\MyFirstApp.class. Program will exit.
You need to run it as
javac MyFirstApp.java
java MyFirstApp
From the directory where MyFistApp.java lives.
'javac' calls the compiler - to that you need to pass your .java file.
'java' will run the compiled code - to that you pass the name of your compiled file, but without any extension: "java MyFirstApp"
Specifying the full path of the file should work when you are not in that directory. But are you in the directory that holds the javac and java programs? If not, those may also need absolute paths if you have not put them on your PATH variable.
What ts the package name? Maybe you have something like
package org.test;
in your header?

Categories

Resources