0

I have been doing a coding exercise inside the IntelliJ IDEA Community Edition 14 IDE, using OpenJDK.

The project is split over 4 .java files all in the same package. My end goal is to run this in the terminal/bash (I use System.console().readLine() which doesnt play nicely in the IDE's console).

I've tried navigating to the directory where these 4 files reside (they all reside in the same dir) and tried:

javac -classpath . BibliotecaApp.java Book.java BookManager.java LibraryDB.java

This creates 4 corresponding .class files fine. The Main is in bibliotecaApp.java/class, so I try run it by:

java BibliotecaApp

But I get the error

Exception in thread "main" java.lang.NoClassDefFoundError: BibliotecaApp (wrong name: com/twu/biblioteca/BibliotecaApp)

Plus about 13 lines of specifics.

Now googling this error seems to be a class path problem, and this is where I get stuck. From places I've read, usingecho $PATH gives me:

/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games

This is still from the directory of the .java files above. That path means nothing to me. I have no idea what it is or what it does, or what even a classpath is! Theres alot of resources out there on setting a classpath, but they aren’t helping me because I don't know what it's meant for.

That was a dead end for me. I tried to create a .jar instead using IDEA's Build Artifacts as seen HERE. It created a .jar file but when I navigate to that directory and try run it via:

java -jar  biblioteca_jar

I get

Error: Invalid or corrupt jarfile biblioteca_jar

Another issue is that in the file explorer, the file actually comes out as biblioteca.jar, but ls on that dir shows biblioteca_jar. Is that normal?

The code is on my GitHub if that helps anything https://github.com/botagar/twu-biblioteca-JohnGeddes

Jonathan Hall
  • 75,165
  • 16
  • 143
  • 189
John Geddes
  • 147
  • 1
  • 1
  • 12
  • 1
    Is your `bibliotecaApp.java/class` file really starts with lowercase b? I think that might be a problem. – fejese Dec 20 '14 at 01:05
  • Sorry no, it's Capital B. I just wrote it wrongly in the post. Rest of it is still as is. The .jar file though IS lower case 'b'. – John Geddes Dec 20 '14 at 01:09
  • 1
    Also I think you need the full name: `java com.twu.biblioteca.BibliotecaApp` running in the `out/production` folder – fejese Dec 20 '14 at 01:11
  • `java out/production/biblioteca/com.twu.biblioteca.BibliotecaApp` Gives me the same error :( – John Geddes Dec 20 '14 at 01:22
  • Sorry, it should be run in `out/production/biblioteca`. That seem to work for me checking out your code – fejese Dec 20 '14 at 01:27
  • Thanks for checking it out. Got it cleared up! Still have problems with the .jar, but it's not critical. Does worry me though. – John Geddes Dec 20 '14 at 01:35

1 Answers1

1

Based on your compiler step, change this

java BibliotecaApp

to

java -cp . BibliotecaApp

Which will add the current directory to the classpath for the java runtime environment. See also the Oracle Technote Setting the Class Path.

A jar file is a kind of zip, and should have a .jar extension. So this

java -jar  biblioteca_jar

should probably be

java -jar  biblioteca.jar

And you can check if the file is valid with any zip archive reader. Including jar itself,

jar tvvf biblioteca.jar

Edit

Based on your comments below,

cd ~/Documents/ThoughtWorks Uni/TWU_Biblioteca-master/src/ 

and then

java -cp . com.twu.biblioteca.BibliotecaApp
Elliott Frisch
  • 198,278
  • 20
  • 158
  • 249
  • I get the same `jgeddes@ubuntu:~/Documents/ThoughtWorks Uni/TWU_Biblioteca-master/src/com/twu/biblioteca$ java -cp . BibliotecaApp Exception in thread "main" java.lang.NoClassDefFoundError: BibliotecaApp (wrong name: com/twu/biblioteca/BibliotecaApp)` – John Geddes Dec 20 '14 at 01:18
  • Edited. Next time for better help sooner please specify that kind of detail in your original question. – Elliott Frisch Dec 20 '14 at 01:19
  • Thanks, It worked (for running the .class). I wasn't aware that it had to be run from the root? package. However, trying to check the .jar if its valid, there are issues. Both `jar tvvf biblioteca.jar` and `jar tvvf biblioteca_jar` return `java.io.FileNotFoundException: biblioteca.jar (No such file or directory)` and `java.io.FileNotFoundException: biblioteca_jar (Is a directory)`. The biblioteca_jar was generated by IDEA. I have no idea why in the file explorer it has a .jar extension but in bash, it's _jar. – John Geddes Dec 20 '14 at 01:33
  • You exported it to a directory named `biblioteca_jar`. It's not a JAR file. – Elliott Frisch Dec 20 '14 at 01:44
  • Oh! I see. So IDEA didn't actually create a .jar then? It's confusing because the file HAS a .jar extension when looking at it through the file explorer. Anyway's, I'm not going to bother too much with the .jar for now. Thanks for the help! – John Geddes Dec 20 '14 at 01:50