Bug 145527 - NoClassDefFound Error in Java Macro when trying to load another class
Summary: NoClassDefFound Error in Java Macro when trying to load another class
Status: VERIFIED FIXED
Alias: None
Product: LibreOffice
Classification: Unclassified
Component: LibreOffice (show other bugs)
Version:
(earliest affected)
6.4.0.3 release
Hardware: All All
: medium normal
Assignee: Not Assigned
URL:
Whiteboard: target:7.4.0 target:7.3.5 target:7.3.4
Keywords: bibisected, regression
: 145526 (view as bug list)
Depends on:
Blocks: Macro
  Show dependency treegraph
 
Reported: 2021-11-03 15:23 UTC by Alex R
Modified: 2022-07-27 06:38 UTC (History)
6 users (show)

See Also:
Crash report or crash signature:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Alex R 2021-11-03 15:23:58 UTC
Description:
When I try to execute a Java Macro which loads any other class despite the one first loaded by the URLClassLoader, I get a NoClassDefFound Error.
I searched before filing this bug if it is already here. I could not find it, in case I overlooked it, my apologies. 


Steps to Reproduce:
1. Go to <LOInstall Folder>\share\Scripts\java
2. Duplicate directory HelloWorld, e. g. to HelloWorldTest
3. Open the new directory, in there is HelloWorld.java
4. Adapt HelloWorld.java as follows:
package org.libreoffice.example.java_scripts;

import com.sun.star.script.provider.XScriptContext;
import com.sun.star.uno.UnoRuntime;
import com.sun.star.text.XTextDocument;
import com.sun.star.text.XTextRange;
import com.sun.star.text.XText;
/**
 *  HelloWorld class
 *
 */
public class HelloWorld {
    public static void printHW(XScriptContext xSc) {

        // getting the text document object
        XTextDocument xtextdocument = (XTextDocument) UnoRuntime.queryInterface(
                                          XTextDocument.class, xSc.getDocument());
        XText xText = xtextdocument.getText();
        XTextRange xTextRange = xText.getEnd();
        xTextRange.setString("Hello World (in Java)");
	LoadMe lm = new LoadMe();  // the actual change
		
	xTextRange.setString("Test String: " + lm.getText());
    }// printHW

}

5. create a new java class file, LoadMe.java e. g. as follows:

package org.libreoffice.example.java_scripts;

public class LoadMe {
	
	public LoadMe() {
		
	}
	 public String getText() {
		 return "Success";
	 }
	
}

6. Compile the java files, e. g. in a dos command window after changing to the right path:

SET OOO=c:\progra~1\LibreOffice\program\classes
SET OOOPATH=%OOO%\juh.jar;%OOO%\jurt.jar;%OOO%\ridl.jar;%OOO%\unoil.jar
javac -cp "%OOOPATH%" LoadMe.java HelloWorld.java 


7. create a new path in your current HelloWorldTest directory
all of those subfolders have to be created according to the java package name of the created class files

org\libreoffice\example\java_scripts

8. copy the class files created by compilation to 

org\libreoffice\example\java_scripts

9. Update the jar file with the new classes, e, g.: 

jar -cvf HelloWorld.jar org

from the command window in the HelloWorldTest folder
If everything is done right, the parcel descriptor should still fit.

10. Now start Writer

11. Go to 
Tools -> Macros -> Run Macro

12. Search the available macros HelloWorldTest in the LibreOffice list and execute it







Actual Results:
NoClassDefFoundError. I can debug until the point LoadMe is called, so HelloWorld is executed but LoadMe is not on the classpath.

Expected Results:
LoadMe is executed and the String in Writer changes from "Hello World (in Java)" to "Test String: Success"


Reproducible: Always


User Profile Reset: Yes



Additional Info:
The issue seems with ScriptProviderForJava.java
Comment 1 Alex R 2021-11-03 15:55:59 UTC
Java is Bellsoft 1.8_282, I also tried Azul (also 1.8)
Comment 2 Alex R 2021-11-03 18:10:57 UTC
*** Bug 145526 has been marked as a duplicate of this bug. ***
Comment 3 Julien Nabet 2022-01-22 10:20:10 UTC
Could you give a try to LO 7.2.5?
Also if you installed LO 64 bits, could you check you got Java 64 bits.
(and if you have LO 32 bits, you must check you got Java 32 bits).

Also, you can rename your LO profile (see https://wiki.documentfoundation.org/QA/FirstSteps#Corrupted_user_profile).
Comment 4 Alex R 2022-01-24 14:49:40 UTC
(In reply to Julien Nabet from comment #3)
> Could you give a try to LO 7.2.5?
> Also if you installed LO 64 bits, could you check you got Java 64 bits.
> (and if you have LO 32 bits, you must check you got Java 32 bits).
> 
> Also, you can rename your LO profile (see
> https://wiki.documentfoundation.org/QA/FirstSteps#Corrupted_user_profile).

Thanks, I just tried with LO 7.2.5.2 (x64)
Seems to behave the same for me, I still get


An exception occurred while running the Java script org.libreoffice.example.java_scripts.HelloWorld.printHW.

Type: java.lang.NoClassDefFoundError

Message: java.lang.NoClassDefFoundError: org/libreoffice/example/java_scripts/LoadMe


I also tried resetting the user profile, and I am fairly sure to have the right 64bit Java configured in Options -> Advanced. As mentioned, I can set breakpoints in the Macro and debug until the LoadMe class is called and loading fails. The first part of the Macro which writes Hello World also gets executed, so Java is working fine.  
When I exchange just ScriptProviderForJava.jar from LO 7.x with the version that was delivered with LO 5.x, everything works as expected also in LO 7.
Comment 5 Samuel Mehrbrodt (allotropia) 2022-05-31 06:15:45 UTC
This was broken by 

commit 418533f0af7cd303d559c8fb136c49e7e9fb0d79
Author: Stephan Bergmann <sbergman@redhat.com>
Date:   Thu Aug 8 11:33:16 2019 +0200

    Related tdf#116767: Call URLClassLoader.close

https://cgit.freedesktop.org/libreoffice/core/commit/?id=418533f0af7cd303d559c8fb136c49e7e9fb0d79
Comment 6 Stephan Bergmann 2022-05-31 06:46:19 UTC
(In reply to Samuel Mehrbrodt (allotropia) from comment #5)
> This was broken by 
> 
> commit 418533f0af7cd303d559c8fb136c49e7e9fb0d79
> Author: Stephan Bergmann <sbergman@redhat.com>
> Date:   Thu Aug 8 11:33:16 2019 +0200
> 
>     Related tdf#116767: Call URLClassLoader.close
> 
> https://cgit.freedesktop.org/libreoffice/core/commit/
> ?id=418533f0af7cd303d559c8fb136c49e7e9fb0d79

Indeed, that commit apparently didn't take into account that script code loaded through that class loader might want to load further classes through it.  Will revert.

Thanks for bibisecting!
Comment 7 Commit Notification 2022-05-31 12:53:09 UTC
Stephan Bergmann committed a patch related to this issue.
It has been pushed to "master":

https://git.libreoffice.org/core/commit/2a263cbdaf16c723a93af020ebc11b3e07210242

tdf#145527 Revert "Related tdf#116767: Call URLClassLoader.close"

It will be available in 7.4.0.

The patch should be included in the daily builds available at
https://dev-builds.libreoffice.org/daily/ in the next 24-48 hours. More
information about daily builds can be found at:
https://wiki.documentfoundation.org/Testing_Daily_Builds

Affected users are encouraged to test the fix and report feedback.
Comment 8 Commit Notification 2022-05-31 16:50:55 UTC
Stephan Bergmann committed a patch related to this issue.
It has been pushed to "libreoffice-7-3":

https://git.libreoffice.org/core/commit/f167224bd9643824393039a57b3d4cd6654dbb4c

tdf#145527 Revert "Related tdf#116767: Call URLClassLoader.close"

It will be available in 7.3.5.

The patch should be included in the daily builds available at
https://dev-builds.libreoffice.org/daily/ in the next 24-48 hours. More
information about daily builds can be found at:
https://wiki.documentfoundation.org/Testing_Daily_Builds

Affected users are encouraged to test the fix and report feedback.
Comment 9 Alex R 2022-06-01 10:15:37 UTC
I just tried todays daily build and the issue is gone, it works as expected. Nice! 
Thank you very much!
Comment 10 Julien Nabet 2022-06-01 15:50:59 UTC
Let's put this one to VERIFIED then.
Comment 11 Commit Notification 2022-06-01 17:13:37 UTC
Stephan Bergmann committed a patch related to this issue.
It has been pushed to "libreoffice-7-3-4":

https://git.libreoffice.org/core/commit/a994ee5b1f1270eefbe1b3e9b7a5cf866ba924d9

tdf#145527 Revert "Related tdf#116767: Call URLClassLoader.close"

It will be available in 7.3.4.

The patch should be included in the daily builds available at
https://dev-builds.libreoffice.org/daily/ in the next 24-48 hours. More
information about daily builds can be found at:
https://wiki.documentfoundation.org/Testing_Daily_Builds

Affected users are encouraged to test the fix and report feedback.