0

Here's my goal: Use SableCC to generate several parsers/lexers at runtime and load instances of them for parsing and lexing.

So I figured my steps would be:

  1. Generate the .java files using SableCC. I think I've done this, fine.

  2. Compile the .java file to .class files. I did this using JavaCompiler. I think this worked fine, as well.

  3. Put the generated files in a .jar file, preserving the file structure. I did this using this as a template. Examining the .jar file using JarBrowser showed that the file structure of the packages was preserved. Using it, the Jar Catalog looks the same as a regular .jar file. However, using JarBrowser, my jar doesn't show up under "CLASSPATH catalog." This leads me to believe that my .jar file isn't being created correctly.

  4. Load the .jar file into an instance of URLClassLoader. I'm trying to load it using Class.forName() but it doesn't seem to be working.

Whenever I do step 4., I get a java.lang.ClassNotFoundException. Does anyone have any ideas as to why this is?

Community
  • 1
  • 1
Jesus is Lord
  • 14,971
  • 11
  • 66
  • 97
  • possible duplicate of [How should I load Jars dynamically at runtime?](http://stackoverflow.com/questions/60764/how-should-i-load-jars-dynamically-at-runtime) – Eugene Retunsky Nov 10 '12 at 04:38
  • @EugeneRetunsky: I've already been to that question. I'm using almost identical code but it's not working. Hence different question :P – Jesus is Lord Nov 10 '12 at 04:41
  • Did you try this: http://stackoverflow.com/questions/194698/how-to-load-a-jar-file-at-runtime ? – Eugene Retunsky Nov 10 '12 at 04:49
  • I've tried `ClassLoader.loadClass`, as well. Loading the class works whenever I use another jar someone else has made, just not my jar/package/class. – Jesus is Lord Nov 10 '12 at 04:57
  • put the directory with compiled classes into classpath instead the jar file, and run your test program. If ClassNotFoundException still arise, error is in your classes or package structure, otherwise indeed in jar file. – Alexei Kaigorodov Nov 10 '12 at 06:30

1 Answers1

1

It turns out that I had a slash as a prefix to the file/directory names. I was truncating the parent directory from the file name, but didn't take into account the trailing slash from the parent directory, so /dir1/file1 was being done instead of dir1/file1. I guess JAR files are picky.

Jesus is Lord
  • 14,971
  • 11
  • 66
  • 97