asciidoctor / asciidoctorj

:coffee: Java bindings for Asciidoctor. Asciidoctor on the JVM!
http://asciidoctor.org
Apache License 2.0
625 stars 173 forks source link

Exception in thread "main" java.lang.NoClassDefFoundError: org/asciidoctor/OptionsBuilder #814

Closed JosephDqs closed 5 years ago

JosephDqs commented 5 years ago

Hi, I've a problem with asciidoctor java lib using maven : even with dependencies on my pom and right import, I always have the same error :

Exception in thread "main" java.lang.NoClassDefFoundError: org/asciidoctor/OptionsBuilder at ProgGl.PriseDeNote.Application.view(Application.java:170) at ProgGl.PriseDeNote.Commands.ViewCommand.execute(ViewCommand.java:22) at ProgGl.PriseDeNote.App.main(App.java:65) Caused by: java.lang.ClassNotFoundException: org.asciidoctor.OptionsBuilder at java.net.URLClassLoader.findClass(URLClassLoader.java:381) at java.lang.ClassLoader.loadClass(ClassLoader.java:424) at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:335) at java.lang.ClassLoader.loadClass(ClassLoader.java:357) ... 3 more

My co-worker made a stackoverflow post where he details more :

https://stackoverflow.com/questions/55929946/java-lang-noclassdeffounderror-while-using-maven-eclipse-asciidoctorj

thanks for answering

abelsromero commented 5 years ago

Hi,

The post mentions running a converter directly from a jar with java -jar target/PriseDeNotes-0.0.1-SNAPSHOT.jar but it also mentions using the maven plugin. The use of one or the other depends on your needs and requires different configurations. Can you explain a bit what are you trying to achieve?

JosephDqs commented 5 years ago

Thanks for the fast answer.

The app is a simple school project that involve creating/deleting/listing/research in document etc... But here for the view methods apparently we need asciidoctor lib to convert an asciidoc file to a correct html page so we can see it in our web-browser.

abelsromero commented 5 years ago

The app is a simple school project that involve creating/deleting/listing/research in document etc...

does this mean you are creating the document pragmatically?

JosephDqs commented 5 years ago

we have an .adoc file and what we want Is to use Asciidoctor to convert the content of this file and convert it to an html file so we can open it in a browser. and in general, the .adoc files are created by the user of the application and we don't have a problem with that.

the view function will open the .adoc file and do the conversion and create an html file.

but here the problem seems to be that he can't find the classes built in asciidoctor package .

abelsromero commented 5 years ago

In that case you don't need to meddle with AsciidoctorJ at all. For conversion, just place the asciidoc document in src/docs/asciidoc, setup the maven plugin and run it plugin from the shell as mvn generate-resources. Here is a full example https://github.com/asciidoctor/asciidoctor-maven-examples/blob/master/asciidoc-to-html-example/pom.xml. Remove the <attributes> and <sourceHighlighter> configurations for a more "out of the box" conversion.

abelsromero commented 5 years ago

but here the problem seems to be that he can't find the classes built in asciidoctor package .

You don't need to do that with the maven plugin. But if you want to know, the problem is that you need to add all the required libraries to the classpath when using java -jar target/PriseDeNotes-0.0.1-SNAPSHOT.jar with the --classpath argument. When you compile/run from the IDE, the IDE takes care of that for you, but in the shell, you need to that manually.

I hope I am not giving too much help for the school exercise.

JosephDqs commented 5 years ago

but even if I checked the lib in my project properties, I've to add the --classpath ? d7c9256876 1

JosephDqs commented 5 years ago

And if we use the maven plugin, does that means that any document moved in the repository is automatically convert to html ?

abelsromero commented 5 years ago

but even if I checked the lib in my project properties, I've to add the --classpath ?

That's for running from eclipse. If you build your jar with package maven goal, the jar does not contain any of the dependencies. If you are familiar with c/c++ libs, it's like everything is "dynamically linked" in Java. So when you are running directly with java jar, you need to tell the Java runner where all the require libraries are located.

But this is just in case you want to convert the document directly with Asciidoctorj, which I understood this was not the case.

abelsromero commented 5 years ago

And if we use the maven plugin, does that means that any document moved in the repository is automatically convert to html ?

Yes, the plugin scans all sources in a folder root and converts them inside the target directory.

pzygielo commented 5 years ago

@abelsromero

But if you want to know, the problem is that you need to add all the required libraries to the classpath when using java -jar target/PriseDeNotes-0.0.1-SNAPSHOT.jar with the --classpath argument

Do you mean additional argument -classpath for running java, like java -jar target/PriseDeNotes-0.0.1-SNAPSHOT.jar -classpath someother.jar ?

JosephDqs commented 5 years ago

Actually we want to convert the document with Asciidoctorj using those lines :

public void view(String document) {
        OptionsBuilder op = OptionsBuilder.options().toFile(false);

        Asciidoctor asciidoctor = create();

        String html = asciidoctor.convertFile(new File(this.path + "/" + document + ".adoc"), op.asMap());
        System.out.println(html);
    }

so now I've to lauch app with java -jar target/PriseDeNotes-0.0.1-SNAPSHOT.jar -classpath asciidoctorj-2.0.0.jar ?

pzygielo commented 5 years ago

Then - no. https://docs.oracle.com/en/java/javase/11/tools/java.html#GUID-3B1CE181-CD30-4178-9602-230B800D4FAE

-jar jarfile Executes a program encapsulated in a JAR file. The jarfile argument is the name of a JAR file with a manifest that contains a line in the form Main-Class:classname that defines the class with the public static void main(String[] args) method that serves as your application's starting point. When you use -jar, the specified JAR file is the source of all user classes, and other class path settings are ignored.

abelsromero commented 5 years ago

My mistake, sorry. You are right, just define all jars, included yours with the classpath option, but then you need to add the main class at the end.

java -cp MyJar.jar:lib/* com.somepackage.subpackage.Main
kotaibaalshara commented 5 years ago

Is there another way to specify the main class in mvn projects ? i'm his co worker and i think the problem is our pom.xml file.

abelsromero commented 5 years ago

You could use the https://www.mojohaus.org/appassembler/appassembler-maven-plugin/ to automate the creation of scripts to start the app. You can find plenty of documentation and exemples on the web. Beware of creating a fat jar, I am not sure jRuby behaves correctly in that case.

Regardless, I understand the issues with AsciidoctorJ are fixed right?

JosephDqs commented 5 years ago

the problem is solved we had to run with java -jar target/PriseDeNotes-0.0.1-SNAPSHOT.jar-with-dependencies.jar