GreenDelta / openlca-python-tutorial

Explains the usage of the openLCA API from Python (Jython)
Other
59 stars 17 forks source link

Cannot open Database #6

Open gvoigt opened 6 years ago

gvoigt commented 6 years ago

I would like to open a database but without success

import os
import sys

# change this path so that it matches the `libs` folder of your openLCA installation
OLCA_LIB_DIR =  "C:/Program Files (x86)/openLCA/plugins/olca-app_1.6.3/libs"

for jar in os.listdir(OLCA_LIB_DIR):
  print("Add jar to path " + jar)
  full_path = os.path.join(OLCA_LIB_DIR, jar)
  sys.path.append(full_path)

# now you can access the openLCA API

from org.openlca.core.database.derby import DerbyDatabase
from java.io import File

# path to our database
folder = "C:/Users/user/openLCA-data-1.4/databases/elcd_3_2_greendelta_v2_16"
db = DerbyDatabase(File(folder))

I also I tried

OLCA_LIB_DIR =  os.path.abspath("C:/Program Files (x86)/openLCA/plugins/olca-app_1.6.3/libs")

But I alway get

log4j:WARN No appenders could be found for logger (org.openlca.core.database.derby.DerbyDatabase).
log4j:WARN Please initialize the log4j system properly.
log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info.
Traceback (most recent call last):
  File ".\jython_olca_example.py", line 19, in <module>
    db = DerbyDatabase(File(folder))
Exception [EclipseLink-30005] (Eclipse Persistence Services - 2.6.4.v20160829-44060b6): org.eclipse.persistence.exceptions.PersistenceUnitLoadingException
Exception Description: An exception was thrown while searching for persistence archives with ClassLoader: org.python.core.SyspathJavaLoader@1c27487
Internal Exception: java.net.URISyntaxException: Relative path in absolute URI: file://C:/Program%20Files%20(x86)/openLCA/plugins/olca-app_1.6.3/libs/olca-core-1.6.3.jar
        at org.eclipse.persistence.exceptions.PersistenceUnitLoadingException.exceptionSearchingForPersistenceResources(PersistenceUnitLoadingException.java:127)
        at org.eclipse.persistence.internal.jpa.deployment.PersistenceUnitProcessor.findPersistenceArchives(PersistenceUnitProcessor.java:349)
        at org.eclipse.persistence.internal.jpa.deployment.PersistenceUnitProcessor.findPersistenceArchives(PersistenceUnitProcessor.java:298)
        at org.eclipse.persistence.internal.jpa.deployment.JPAInitializer.findPersistenceUnitInfoInArchives(JPAInitializer.java:156)
        at org.eclipse.persistence.internal.jpa.deployment.JPAInitializer.findPersistenceUnitInfo(JPAInitializer.java:141)
        at org.eclipse.persistence.jpa.PersistenceProvider.createEntityManagerFactory(PersistenceProvider.java:188)
        at org.openlca.core.database.derby.DerbyDatabase.connect(DerbyDatabase.java:128)
        at org.openlca.core.database.derby.DerbyDatabase.<init>(DerbyDatabase.java:62)
        at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
        at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
        at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
        at java.lang.reflect.Constructor.newInstance(Unknown Source)
        at org.python.core.PyReflectedConstructor.constructProxy(PyReflectedConstructor.java:211)

org.eclipse.persistence.exceptions.PersistenceUnitLoadingException: Exception [EclipseLink-30005] (Eclipse Persistence Services - 2.6.4.v20160829-44060b6): org.eclipse.persistence.
exceptions.PersistenceUnitLoadingException
Exception Description: An exception was thrown while searching for persistence archives with ClassLoader: org.python.core.SyspathJavaLoader@1c27487
Internal Exception: java.net.URISyntaxException: Relative path in absolute URI: file://C:/Program%20Files%20(x86)/openLCA/plugins/olca-app_1.6.3/libs/olca-core-1.6.3.jar

How can I fix this?

msrocka commented 6 years ago

I think the problem are the space characters in the OLCA_LIB_DIR. It works for me when I load the libs from a folder with no space characters (there should be a way to encode them correctly for sys.path but I did not found a quick way). You could just copy the jar files in the libs folder to another folder without space characters and load them from there.

Brotrand commented 6 years ago

I tried the same example with the code above from @gvoigt . I also deleted the space characters.

import os
import sys

# change this path so that it matches the `libs` folder of your openLCA installation
libdir =  "C:\Users\philipp\AppData\Local\openLCA\plugins\olca-app_1.6.3\libs"

for jar in os.listdir(libdir):
    print("Add jar to path " + jar)
    full_path = os.path.join(libdir, jar)
    sys.path.append(full_path)

# now you can access the openLCA API

from org.openlca.core.database.derby import DerbyDatabase
from java.io import File

# path to our database
folder = "C:\Users\philipp\openLCA-data-1.4\databases\test_ProBas"
db = DerbyDatabase(File(folder))

Unfortunatly I get the following output.

log4j:WARN No appenders could be found for logger (org.openlca.core.database.derby.DerbyDatabase).
log4j:WARN Please initialize the log4j system properly.
log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info.
Traceback (most recent call last):
  File "C:\Users\philipp\eclipse-workspace\test\load_ProBas.py", line 19, in <module>
    db = DerbyDatabase(File(folder))
    at org.openlca.core.database.derby.DerbyDatabase.createNew(DerbyDatabase.java:96)
    at org.openlca.core.database.derby.DerbyDatabase.<init>(DerbyDatabase.java:61)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
    at java.lang.reflect.Constructor.newInstance(Unknown Source)
    at org.python.core.PyReflectedConstructor.constructProxy(PyReflectedConstructor.java:211)
org.openlca.core.database.DatabaseException: org.openlca.core.database.DatabaseException: Failed to create database

What do I do wrong? Thanks for your help!

msrocka commented 6 years ago

@Brotrand you have to escape the backslashes in the paths, so write "C:\\Users\\philipp\\openLCA-data-1.4\\databases\\test_ProBas" or "C:/Users/philipp/openLCA-data-1.4/databases/test_ProBas" instead of "C:\Users\philipp\openLCA-data-1.4\databases\test_ProBas". Does this help?

Brotrand commented 6 years ago

@msrocka Indeed, thank you very much. It´s working now!

julopezarc commented 6 years ago

Hi everybody, this is new for me, somebody can help me to play in this level? i do not write macros in openLCA. thanks for all

Julián

2018-01-18 9:16 GMT-05:00 Brotrand notifications@github.com:

@msrocka https://github.com/msrocka Indeed, thank you very much. It´s working now!

— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub https://github.com/GreenDelta/openlca-python-tutorial/issues/6#issuecomment-358659380, or mute the thread https://github.com/notifications/unsubscribe-auth/AY4vesx6-vqWLGhBrpnSNpjuwCTtW0Qyks5tL1JFgaJpZM4Q9a-K .

Pommespapst commented 5 years ago

I am getting a nullpointer exception when running above code. I deleted all spaces in the lib path and I escaped the database path ('C:\Users\user\openLCA-data-1.4\databases\openlca_lcia_methods_1_5_7').

log4j:WARN No appenders could be found for logger (org.openlca.core.database.derby.DerbyDatabase).
log4j:WARN Please initialize the log4j system properly.
log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info.
Traceback (most recent call last):
  File "<input>", line 1, in <module>
  File "C:\Users\user\.IdeaIC2018.2\config\plugins\python-ce\helpers\pydev\_pydev_bundle\pydev_umd.py", line 198, in runfile
    pydev_imports.execfile(filename, global_vars, local_vars)  # execute the script
  File "C:/Users/user/IdeaProjects/OpenLCATest/main.py", line 5, in <module>
    oDB = DerbyDatabase(JavaFile(sDataBasePath))
PersistenceUnitLoadingException: Exception [EclipseLink-30005] (Eclipse Persistence Services - 2.6.4.v20160829-44060b6): org.eclipse.persistence.exceptions.PersistenceUnitLoadingException
Exception Description: An exception was thrown while searching for persistence archives with ClassLoader: org.python.core.SyspathJavaLoader@52d96367
Internal Exception: java.lang.NullPointerException

Any ideas?

msrocka commented 5 years ago

@Pommespapst Is this error trace complete? If not, could you also post the rest of it?

Otherwise: Is the database maybe opened in openLCA? -> you can only connect to a database that is not opened by another process.

Pommespapst commented 5 years ago

I found one (the?) error. My OpenLCA Version is 1.6.3. My JDK is version 10. The method "toValue" of the org.eclipse.persistence.internal.helper.JavaSEPlatform checks JDK versions up to 9. For versions higher than 9 it returns null, causing the database import to crash. I am now updating OpenLCA to see if that solves the problem.

Pommespapst commented 5 years ago

Update: installing newest OpenLCA Version resolved the problem!