mlaiks
mlaiks

Reputation: 11

Java run problems from terminal

I'm using a cloud VM in which Ubuntu is installed. Java version installed is:

java version "1.8.0_66"
Java(TM) SE Runtime Environment (build 1.8.0_66-b17)
Java HotSpot(TM) 64-Bit Server VM (build 25.66-b17, mixed mode)

I've never used terminal to compile and run programs. However, this program works using Eclipse.

I have to use two jars when I compile my java program: disco-2.1.jar and sqlite-jdbc-3.8.11.2.jar. The terminal command I use is:

javac -cp '/home/ubuntu/workspace/sem/*' USem.java

Using /home/ubuntu/workspace/sem/* adds disco and sqlite jars to the classpath.

This creates my USem.class file in sem directory, without errors. Those jars are contained in sem directory.

USem.java contains this part of code, starting from the beginning:

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.sql.*;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import org.apache.lucene.index.CorruptIndexException;

import de.linguatools.disco.CorruptConfigFileException;
import de.linguatools.disco.DISCO;
import de.linguatools.disco.TextSimilarity;
import de.linguatools.disco.DISCO.SimilarityMeasure;

public class USem {

//irrelevant code here

public static void main(String[] args) throws IOException, CorruptConfigFileException, SQLException{

The problem starts when I run this in the terminal:

java USem

The terminal shows me:

Error: A JNI error has occurred, please check your installation and try again Exception in thread "main" java.lang.NoClassDefFoundError: de/linguatools/disco/CorruptConfigFileException at java.lang.Class.getDeclaredMethods0(Native Method) at java.lang.Class.privateGetDeclaredMethods(Class.java:2701) at java.lang.Class.privateGetMethodRecursive(Class.java:3048) at java.lang.Class.getMethod0(Class.java:3018) at java.lang.Class.getMethod(Class.java:1784) at sun.launcher.LauncherHelper.validateMainClass(LauncherHelper.java:544) at sun.launcher.LauncherHelper.checkAndLoadMain(LauncherHelper.java:526) Caused by: java.lang.ClassNotFoundException: de.linguatools.disco.CorruptConfigFileException at java.net.URLClassLoader.findClass(URLClassLoader.java:381) at java.lang.ClassLoader.loadClass(ClassLoader.java:424) at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:331) at java.lang.ClassLoader.loadClass(ClassLoader.java:357) ... 7 more

I think there's a problem with disco-2.1.jar. I checked the jar classes contained in it and everything was ok.

My workspace is organized like so:

home/ubuntu/workspace/sem

In the sem directory I have my .java file and the .jar files are added there.

What am I doing wrong? I tried uninstalling JDK and reinstalling it, changing the terminal folder in which I run commands, but nothing changed.

--Update--

Now I wrote

java -cp '/home/ubuntu/workspace/sem/*' USem

However, I obtained

Error: Could not find or load main class USem

Upvotes: 0

Views: 836

Answers (4)

neo73
neo73

Reputation: 464

move all the required jars in thejre/lib/ext folder and simply run the command
java filename worked for me!

Upvotes: 0

mlaiks
mlaiks

Reputation: 11

All your comments were useful. I'd like to post my solution.
I declared CLASSPATH environment

export CLASSPATH=/home/ubuntu/workspace/sem/disco-2.1.jar:**other paths for other external jars**:.

I moved into workspace and compiled .class file

javac -cp 'sem/*' sem/USem.java

Then I moved into sem and run

java USem

and worked.

Upvotes: 0

user2543253
user2543253

Reputation: 2173

The default classpath when there's no -classpath argument and no CLASSPATH environment variable is .. Now when your class file is in your working directory and you call java USem, Java finds your class but not the classes in the other jar files because these are not in the classpath.

When you add -cp '/home/ubuntu/workspace/sem/*' Java will find the classes in the jar files. But this also overrides the default classpath, so . is no more in there, therefore Java doesn't find your own class anymore. You need to explicitly add . (or /home/ubuntu/workspace/sem) again: -cp '.:/home/ubuntu/workspace/sem/*'

Upvotes: 0

Raman Shrivastava
Raman Shrivastava

Reputation: 2953

Add Disco jar (or any other required jar for that matter) to classpath while executing java command

java -cp "Whatever.jar" my.package.MainClass

Upvotes: 1

Related Questions