Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » Advanced Scripting Environment (EASE) » ClassNotFoundException: com.mysql.jdbc.Driver(Class in module is not loaded when called thru a script method)
ClassNotFoundException: com.mysql.jdbc.Driver [message #1855228] Mon, 03 October 2022 08:42 Go to next message
Bart Devos is currently offline Bart DevosFriend
Messages: 2
Registered: October 2022
Junior Member
Hi list,

I try to make a EASE javascript which needs information from a mysql database and based on that, appends text to the selected java editor. I made a eclipse plugin project as described in codeandme.blogspot.com/2014/10/writing-modules-for-ease.html. I added the logic (simplyfied for the example) as follows:
package be.zokola.main;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.List;

import org.eclipse.ease.modules.WrapToScript;

public class MysqlTableQueries {
	private String driver = "com.mysql.jdbc.Driver";
	private String dburl = "jdbc:mysql://localhost:3306/";
	private String dbName = "zokoladblabeltest";
	private String credname = "<myDBUsername>";
	private String cred = "<myDBPassword>";
	private String tableName = null;
	private Connection conn;

	public MysqlTableQueries() {
	}
	
	@WrapToScript
	public String test() {
		return "Helllooowwww....";
	}
	/**
	 * Make a connection to the database. If dbName is ommitted, zokoladblabelstest is used
	 * @param dbName
	 * @throws ClassNotFoundException
	 * @throws SQLException
	 */
	@WrapToScript
	public void connect2db() throws ClassNotFoundException, SQLException {
		if(dbName != null) {
			this.dbName = dbName;
		}
		
		Class.forName(driver);
		this.conn = DriverManager.getConnection(dburl+dbName, credname, cred);
	}
	
	public Boolean doesTableExist(String tableName) throws SQLException {
		Integer result = 0;
		this.tableName = tableName;
		String q = "SELECT COUNT(*) "
				+ "FROM information_schema.tables  "
				+ "WHERE table_schema = \"" + dbName + "\" "
				+ "AND table_name = \"" + tableName + "\";";

		Statement st = null;
		ResultSet rs = null;
		try {
			st = conn.createStatement();
		      rs = st.executeQuery(q);
		      if(rs.next()) {
		    	  result = rs.getInt(1);
		      }
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			st.close();
		}
		if(result == 1) {
			return true;
		} else {
			return false;
		}
	}


Now my EASE script:
/**
 * Compares a given entity with a table in the database
 * name: DiffTableEntity
 * popup: enableFor(org.eclipse.jface.text.ITextSelection)
 * image: ../icns/MySQL-Logo2.png
 */
loadModule('/System/UI');
loadModule("/System/JVM", false);
module = createInstance("workspace://EASEscript/src/be/zokola/main/MysqlTableQueries.java");
wrap(module);


var question1 = "Databasename:";
var defaultvalue1 = "zokoladblabeltest";
var title1 = "Enter the databasename to query";
 
var question2 = "Tablename:";
var defaultvalue2 = "";
var title2 = "Enter the table to query";
 
var dbname = showInputDialog(question1, defaultvalue1, title1);
if(dbname != null & dbname != ""){
	var table = showInputDialog(question2, defaultvalue2, title2);
	if(table != null && table != ""){
		print("============>>>>>>>>>>" + test());
		connect2db();
		print("============>>>>>>>>>>" + doesTableExist(table));
		print(dbname + " - " + table);
		closeConnection();
	}
}


What does work:
1) Executing scriptmethod "test" (see
print("============>>>>>>>>>>" + test());

2) Calling "doesTableExist(String table)" from a test class in java. So, the mysql driver is loaded correctly in that case.

What does NOT work:
print("============>>>>>>>>>>" + doesTableExist(table));
from the script. I get following error stack trace:
============>>>>>>>>>>Helllooowwww....
org.eclipse.ease.ScriptExecutionException: JavaError: com.mysql.jdbc.Driver
	at DiffTableEntity.js,workspace://EASEproject/livescripts/DiffTableEntity.js

Java Stacktrace:
java.lang.ClassNotFoundException: com.mysql.jdbc.Driver
	at java.base/java.net.URLClassLoader.findClass(URLClassLoader.java:476)
	at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:589)
	at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:522)
	at java.base/java.lang.Class.forName0(Native Method)
	at java.base/java.lang.Class.forName(Class.java:315)
	at be.zokola.main.MysqlTableQueries.connect2db(MysqlTableQueries.java:48)
	at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
	at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
	at java.base/java.lang.reflect.Method.invoke(Method.java:566)
	at org.mozilla.javascript.MemberBox.invoke(MemberBox.java:126)
	at org.mozilla.javascript.NativeJavaMethod.call(NativeJavaMethod.java:225)
	at org.mozilla.javascript.gen.c_17._c_connect2db_4(Unknown Source)
	at org.mozilla.javascript.gen.c_17.call(Unknown Source)
	at org.mozilla.javascript.optimizer.OptRuntime.callName0(OptRuntime.java:74)
	at org.mozilla.javascript.gen.workspace___EASEproject_livescripts_DiffTableEntity_js_13._c_script_0(Unknown Source)
	at org.mozilla.javascript.gen.workspace___EASEproject_livescripts_DiffTableEntity_js_13.call(Unknown Source)
	at org.mozilla.javascript.ContextFactory.doTopCall(ContextFactory.java:405)
	at org.mozilla.javascript.ScriptRuntime.doTopCall(ScriptRuntime.java:3508)
	at org.mozilla.javascript.gen.workspace___EASEproject_livescripts_DiffTableEntity_js_13.call(Unknown Source)
	at org.mozilla.javascript.gen.workspace___EASEproject_livescripts_DiffTableEntity_js_13.exec(Unknown Source)
	at org.mozilla.javascript.Context.evaluateReader(Context.java:1293)
	at org.eclipse.ease.lang.javascript.rhino.RhinoScriptEngine.internalExecute(RhinoScriptEngine.java:223)
	at org.eclipse.ease.lang.javascript.rhino.RhinoScriptEngine.execute(RhinoScriptEngine.java:202)
	at org.eclipse.ease.AbstractScriptEngine.inject(AbstractScriptEngine.java:190)
	at org.eclipse.ease.AbstractScriptEngine.run(AbstractScriptEngine.java:243)
	at org.eclipse.core.internal.jobs.Worker.run(Worker.java:63)


Why is the mysql driver correctly loaded when executing from java but not when called from the EASE script. Please, any advice would be much appreciated since i'm fairly new to EASE and building modules.
BTW: This is an excellent addition to the vast eclipse eco-system!

Kind regards,

Bart Devos
Belgium.
Re: ClassNotFoundException: com.mysql.jdbc.Driver [message #1855235 is a reply to message #1855228] Mon, 03 October 2022 14:59 Go to previous messageGo to next message
Christian Pontesegger is currently offline Christian PonteseggerFriend
Messages: 250
Registered: July 2009
Location: Graz, Austria
Senior Member
Hi Bart,

I guess your mysql driver is not part of the running platform, that executes your script.

Your java project quite likely has a dependency to your mysql driver jar. When you launch your java programm, this jar will be added to your classpath. See [1] (at the bottom of the post) for a short example. Scripts however do expect that everything needed is already loaded and simply queries the actual classpath.
So does the eclipse instance that is executing the script have the mysql driver on the classpath? I guess not, so loading the driver classes fails. You could try to use the script command loadJar() first, to add any external requirements.

Generally createInstance() is meant for simple scripts that do not introduce any new dependencies to the running IDE. It is more of a rapid debugging tool.
The "usual" way to create modules is to create a new eclipse plugin-in, provide your code, package and install it into your IDE. Then the plugin also takes care of required dependencies. See [2] for examples.

HTH
Christian

[1] http://codeandme.blogspot.com/2015/12/a-new-interpreter-for-ease-4-provide.html
[2] https://git.eclipse.org/c/ease/org.eclipse.ease.modules.git/tree/plugins/org.eclipse.ease.modules.platform
Re: ClassNotFoundException: com.mysql.jdbc.Driver [message #1855245 is a reply to message #1855235] Tue, 04 October 2022 06:34 Go to previous message
Bart Devos is currently offline Bart DevosFriend
Messages: 2
Registered: October 2022
Junior Member
Hello Christian,

I did try loadJar without success. But now i have a better understanding of what's going on, i'll will digg further into plugin development to solve my problem. Thank you for your answer!
Kind regards,
Bart.
Previous Topic:Keyboard shortcut- EASE (Context: Python4Capella)
Next Topic:How many core contributors/ maintainers are there for EASE
Goto Forum:
  


Current Time: Thu Apr 25 10:06:36 GMT 2024

Powered by FUDForum. Page generated in 0.03007 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top