Where to install library for Java on Ubuntu command line - java

I'm trying to get the Charva tutorial to work, but I don't think I'm installing it correctly. I added the .jar files to /jre/lib/ext, but I get this error when I try to run the tutorial file:
Exception in thread "main" java.lang.NoClassDefFoundError: Tutorial (wrong name: tutorial/java/Tutorial)
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClass(ClassLoader.java:800)
at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:142)
at java.net.URLClassLoader.defineClass(URLClassLoader.java:449)
at java.net.URLClassLoader.access$100(URLClassLoader.java:71)
at java.net.URLClassLoader$1.run(URLClassLoader.java:361)
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)
at sun.launcher.LauncherHelper.checkAndLoadMain(LauncherHelper.java:482)
Am I supposed to be running something else when I compile or am I installing the library the wrong way?

You can add the .jar files to the classpath using:
java -cp "Charva.jar" main.Class
If you need to add multiple jars, or you want it to be better structured in folders, put the jars under lib which is the common thing to do, and use:
java -cp "lib/*.jar" main.Class
If you're using and IDE, tag the question with the specific IDE to get specific instructions for your IDE.

I suggest you build and run your program in your IDE.
Once you have a working program you can build a JAR and when you specify it on the command line with
java -cp {path-to-my-jar}.jar mypackage.MyClass
you can put the JAR where ever you like.

Related

Java NoClassDefFound Error when trying to run

I'm fairly new to working with packages in java but I've got a file structure thats:
project
classes
src
com
main
Billing.class
Password.class
src
com
main
Billing.java
Password.java
Each java file has package package src.com.main; at the top.
I'm compiling from the project folder using javac -d ./classes/ ./src/com/main/*.java and it compiles fine. When I go to run it using java classes/src/com/main/Billing is gives me a NoClassDefFound exception. What am I doing wrong?
Edit: Entire Error:
java classes/src/com/main/BillingException in thread "main" java.lang.NoClassDefFoundError: classes/src/com/main/Billing (wrong name: src/com/main/Billing)
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClass(ClassLoader.java:800)
at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:142)
at java.net.URLClassLoader.defineClass(URLClassLoader.java:449)
at java.net.URLClassLoader.access$100(URLClassLoader.java:71)
at java.net.URLClassLoader$1.run(URLClassLoader.java:361)
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)
at sun.launcher.LauncherHelper.checkAndLoadMain(LauncherHelper.java:482)
Try
cd project/classes/
java -cp . src.com.main.Billing
Your class files are not in the classpath that may be the case.
If you are using maven and eclipse try the command mvn eclipse:eclipse
or
you can set the classpath using command prompt also where your .class files are located.
For example:
C:> set CLASSPATH=classpath1;classpath2..
i think you use other class in your class that is on other package or jar file if so! you need to define Class Path to enable java find your dependencies.

NoClassDefFoundError in a java program

I compiled the program given below and it was fine. Both class files and the java file are on my Desktop folder. But when I run it using the command "java B" on my terminal from the Desktop, I get the exception as given below. Can anyone please tell me what is wrong with it?
import java.util.*;
public class a<X> {
//code
}
class b{
//code
}
Exception in thread "main" java.lang.NoClassDefFoundError: a
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClass(ClassLoader.java:800)
at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:142)
at java.net.URLClassLoader.defineClass(URLClassLoader.java:449)
at java.net.URLClassLoader.access$100(URLClassLoader.java:71)
at java.net.URLClassLoader$1.run(URLClassLoader.java:361)
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)
at sun.launcher.LauncherHelper.checkAndLoadMain(LauncherHelper.java:482)
After you compile your code, you end up with .class files for each class in your program. These binary files are the bytecode that Java interprets to execute your program. The NoClassDefFoundError indicates that the classloader, which is responsible for dynamically loading classes, cannot find the .class file for the class that you're trying to use. It probably indicates that you haven't set the classpath option when executing your code
for more help see this link

Exception in thread "main" java.lang.NoClassDefFoundError: Huffman (wrong name: p3/Huffman)

I am not sure how to fix this but I think it's rather a classpath error. I need to know where to run the following command if this is my file structure:
monas-mbp:p3 mona$ java Huffman c p3code.txt p3input.txt p3output.tx
Exception in thread "main" java.lang.NoClassDefFoundError: Huffman (wrong name: p3/Huffman)
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClass(ClassLoader.java:800)
at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:142)
at java.net.URLClassLoader.defineClass(URLClassLoader.java:449)
at java.net.URLClassLoader.access$100(URLClassLoader.java:71)
at java.net.URLClassLoader$1.run(URLClassLoader.java:361)
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)
at sun.launcher.LauncherHelper.checkAndLoadMain(LauncherHelper.java:482)
Well, I already copied the Huffman.class from the following path actually to the current directory which is /Users/mona/java/p3/src/p3 using
cp /Users/mona/java/p3/bin/p3/Huffman.class Huffman.class
so here's what I have in Eclipse. I created project p3 and then it automatically created package p3 while previously it used to create default package.
Any idea what's the fix? Also why it didn't create the default package automatically like always?
[UPDATE]Well, I didn't expect to receive such confusing answers specially because both project name as well as the automatically package name is p3. Can you please be more elaborate and detailed about your answers? Here's the structure created by Eclipse in my computer:
Also when I run the command in bin/p3 I get this result:
monas-mbp:p3 mona$ pwd
/Users/mona/java/p3/bin/p3
monas-mbp:p3 mona$ java Huffman c p3code.txt p3input.txt p3output.tx
Exception in thread "main" java.lang.NoClassDefFoundError: Huffman (wrong name: p3/Huffman)
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClass(ClassLoader.java:800)
at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:142)
at java.net.URLClassLoader.defineClass(URLClassLoader.java:449)
at java.net.URLClassLoader.access$100(URLClassLoader.java:71)
at java.net.URLClassLoader$1.run(URLClassLoader.java:361)
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)
at sun.launcher.LauncherHelper.checkAndLoadMain(LauncherHelper.java:482)
I already copied the Huffman.class from the following path actually to the current directory which is /Users/mona/java/p3/src/p3 using
cp /Users/mona/java/p3/bin/p3/Huffman.class Huffman.class
now execute java from parent directory of p3 instead from p3, if you have copied it to current directory then execute following set of commands
mkdir p3
mv Huffman.class p3/
java p3.Huffman c p3code.txt p3input.txt p3output.tx
so structure should be relative to current directory
p3
\
|
|
------Huffman.class
note: you are using eclipse to write code why not launch from it as well
it automatically created package p3 while previously it used to
create default package.
Huffman class must have a package declaration as p3. So when you have copied the file in eclipse project, eclipse either had asked you to move the file to p3 package or remove the package declaration from file.
Depending whether other files(Heap,HeapSandBox etc) in your project need to be p3 project or not, you may have to move the files.
To run your Huffman class, you need to use package.classname.
java p3.Huffman -cp p3code.txt p3input.txt p3output.tx

Running Junit test from command line

So I wrote a class called Item and using Junit wrote a test called ItemTest and I want to execute my test from a command line. This is steps that I followed:
set the path, to run java.exe and set the classpath to libraries
...> set path="C:\Program Files (x86)\Java\jdk1.7.0_40\bin"
...> set classpath=C:\Program Files (x86)\Java\hamcrest-core-1.3.jar; C:\Program Files (x86)\Java\junit-4.11.jar
then i simply run org.junit.runner.JUnitCore to see if added libs were in classpath
...>java org.junit.runner.JUnitCore
the output was
JUnit version 4.11
Time: 0,001
OK (0 tests)
Then I compiled without an error Item and ItemTest
C:\Users\User\IdeaProjects\My_project\src>javac .\main\java\Item.java .\test\java\ItemTest.java
Then try ro run this classes
C:\Users\User\IdeaProjects\My_project\src>java org.junit.runner.JUnitCore main.java.Item test.java.ItemTest
In the end I got this
JUnit version 4.11
Exception in thread "main" java.lang.NoClassDefFoundError: main/java/Item (wrong name: Item)
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClass(ClassLoader.java:792)
at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:14 2)
at java.net.URLClassLoader.defineClass(URLClassLoader.java:449)
at java.net.URLClassLoader.access$100(URLClassLoader.java:71)
at java.net.URLClassLoader$1.run(URLClassLoader.java:361)
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:424)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Class.java:190)
at org.junit.runner.JUnitCore.runMain(JUnitCore.java:86)
at org.junit.runner.JUnitCore.runMainAndExit(JUnitCore.java:47)
at org.junit.runner.JUnitCore.main(JUnitCore.java:40)
How to fix this ? Should I put Itemin classpath ?
UPD#1
Put this path in my classpath doesn't work (with and without src):
C:\Users\User\IdeaProjects\My_project\src>
UPD#2
When I want to run the class I adress to him as he stores in file system or in what packages he lays ? Because I don't have packages and so My_project apart from being project name is a module name.
SOLVED
This is the way - I add to classpath next paths:
The path to class Item:
C:\Users\User\IdeaProjects\My_project\src\main\java\;
The path to test class ItemTest:
C:\Users\User\IdeaProjects\My_project\src\test\java\;
Then simply run:
...>java org.junit.runner.JUnitCore ItemTest
You have to put the location of the .class files in the classpath. Even if you are in the same folder it won't find them if they are not in the classpath. That comes from DOS x .... anyone else.
In DOS the OS looks first in the current folder. Everywhere else the OS looks first in the path (or classpath for Java).
If Item is in the package com.example.Item and the class is in c:\users\test\myproj\target\com\example\Item.class then you must add c:\users\test\myproj\target\ to the classpath

Class file not being read

When I compile a java file, it gets compiled and I am able to see the class file in the same location. But when I try to run it using the java command, I get the following error:
Exception in thread "main" java.lang.NoClassDefFoundError: UDPServer (wrong name: com/prasanna/udp/UDPServer)
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClass(ClassLoader.java:787)
at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:142)
at java.net.URLClassLoader.defineClass(URLClassLoader.java:447)
at java.net.URLClassLoader.access$100(URLClassLoader.java:71)
at java.net.URLClassLoader$1.run(URLClassLoader.java:361)
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:423)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
at java.lang.ClassLoader.loadClass(ClassLoader.java:356)
at sun.launcher.LauncherHelper.checkAndLoadMain(LauncherHelper.java:482)
How should I correct this error? How should I make the java program know the path of this class file?
PS: I am very new to command line execution of a Java file.
Assuming the class is in the package com.prasanna.udp and is named UDPServer. Assuming the com/prasanna/udp directory tree, containing the .class files, is located in /home/Prasanna/java/classes, the command line should be:
java -cp /home/Prasanna/java/classes com.prasanna.udp.UDPServer
If your class depends on a jar file located in /foo/bar/somelib.jar, iy should also be placed in the classpath:
java -cp /home/Prasanna/java/classes:/foo/bar/somelib.jar com.prasanna.udp.UDPServer

Categories