0

So I'm pretty sure I looked as well as I could before asking this question, so here goes. I am writing a Java class for RC4 encryption, I'd like to make use of the Files class in Google Guava. I am compiling using javac on the Ubuntu command line. I am trying to link the jar file, guava-10.0.1.jar" to my class to no success.

This is a snippet from the top:

import java.util.*;
import java.io.*;
import com.google.common.io.*;

class rc4 {

 private static int S[] = new int[256];
 private static int kLen;

 public static void main(String[] args) {
 ...

The line specific to what I am calling is as follows, fully in a try block:

 byte plain[] = Files.toByteArray(file);

I then compile using this:

$ javac -cp FULL/FILE/PATH/guava-10.0.1.jar rc4.java

It compiles, but when I try to run java rc4 with the options I defined to access the function, I get the error:

Exception in thread "main" java.lang.NoClassDefFoundError: com/google/common/io/Files
        at rc4.main(rc4.java:58)
Caused by: java.lang.ClassNotFoundException: com.google.common.io.Files
  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)
   ... 1 more

I've tried a few different calls, and have checked the jar to make sure its not a jar, in a jar, in a jar etc. I then tried using a manifest file:

Manifest-Version: 1.0
Created-By: me
Main-Class: rc4
Class-path: /FULL/FILE/PATH/guava-10.0.1.jar

And then create a jar as such:

jar cvf0m rc4.jar manifest.txt rc4.class

and then run it with:

java -jar rc4.jar

to which I get the same error as above, but it's saying "rc4" can not be found instead. So, I'm a little lost to what I'm missing. I am still a lil fresh to Java so if I'm missing some huge obviously basic necessity I sincerely apologize for the oversight, but if anyone can offer me some help in this, I would really appreciate it.

Poken1151
  • 570
  • 5
  • 20

2 Answers2

1

Compiling does not package referenced libraries into your jar. You must either extract the libraries into your jar file or pass in the guava classpath at runtime.

Garrett Hall
  • 29,524
  • 10
  • 61
  • 76
  • I thought about leaving my own answer, but I'm not sure what to add to this. It about sums it up. Regarding the title of the question, there is no 'linking' in Java. It's all runtime reference. – Dave Oct 25 '11 at 21:26
  • Hey, thank you for replying. So, still in a different mode, hence my use of "linking". But when I do compile with javac isn't: – Poken1151 Oct 25 '11 at 21:46
0

To test that your code actually works you can try manually specifying the classpath:

java -cp yourProject.jar:/path/to/guava-10.0.1.jar rc4

Note that you need to specify both project and lib on the classpath like that or rc4 which is from your project JAR will not be found! (As an aside: consider using packages in your project.)

Then if that works it is time to check your JAR manifest:

  1. Is it located in a directory inside the JAR file called META-INF
  2. Is it called MANIFEST.MF
  3. Does the Class-Path contain your classpath, with proper case and so on? Note your attribute is called Class-path instead.)
user268396
  • 11,576
  • 2
  • 31
  • 26
  • I think this is what the other poster suggested, but you're a little clearer. Thanks to both of you. Yup, I finnaly am able to check, and it does run! I will re-check my Manifest. EDIT: Yes, I did in-fact have a lower-case 'p', it runs. I am stuck in some weird infinite loop, but it runs. Feeling mighty blind now, but thank you for the time so much. – Poken1151 Oct 25 '11 at 21:52