Exception:Missing the manifest.properties - java

I am new to opennlp, I am getting Missing the manifest.properties! exception when i excute the following code,please tell me suggestion to avoid this.
public class PrePostProcessing_Peregrine {
public Map<String,Set<String>> btntMap;
public Map<String, String> fishMap;
public SentenceModel sModel;
public SentenceDetectorME sentDet;
public Map<String,Set<String>> topBottomTermSet;
public PrePostProcessing_Peregrine() throws IOException {
FileInputStream str=new FileInputStream("/home/rajendraprasad.yk/Desktop/data/en-sent.bin");
System.out.println(str+"===================>");
SentenceModel sModel = new SentenceModel(str);
System.out.println("===================model =================>"+sModel);
sentDet = new SentenceDetectorME(sModel);
System.out.println("===================>sentDet "+sentDet);
System.err.println("Sentence Detector Initialized");
Exception is:
opennlp.tools.util.InvalidFormatException: Missing the manifest.properties!
at opennlp.tools.util.model.BaseModel.validateArtifactMap(BaseModel.java:217)
at opennlp.tools.sentdetect.SentenceModel.validateArtifactMap(SentenceModel.java:78)
at opennlp.tools.util.model.BaseModel.<init>(BaseModel.java:142)
at opennlp.tools.sentdetect.SentenceModel.<init>(SentenceModel.java:73)
at com.molcon.Text_Mining.PrePostProcessing_Peregrine.<init>(PrePostProcessing_Peregrine.java:66)
at com.molcon.Text_Mining.TextMining.peregrineRun(TextMining.java:207)
at com.molcon.Text_Mining.TextMining.process_journals_for_Mining(TextMining.java:108)
I made mistake at FileInputStream modelIn = new FileInputStream("/home/rajendraprasad.yk/Desktop/data/en-sent.bin"); now I changed to InputStream modelIn = new FileInputStream("/home/rajendraprasad.yk/Desktop/data/en-sent.bin"); from this changes am not getting any exception but not able to load file from SentenceModel.
when i execute this line SentenceModel sModel = new SentenceModel(modelIn); am not getting any response,please help

For what I know there are two possible causes:
the model you are using is corrupted, try to download it again
the version of the model and of the OpenNLP library that you are using are not matching. As I read from the official website models are version specific, so you should try to understand if this is you case, and act accordingly.

If you check this constructor you will see that manifest.properties is not a file, it is a set of hard-coded properties:
Properties manifest = new Properties();
manifest.setProperty(MANIFEST_VERSION_PROPERTY, "1.0");
...
artifactMap.put("manifest.properties", manifest);
When you compare it to the InputStream constructor you can see that manifest.properties is nowhere to be found, only loadModel(in) which leads to conclusion that manifest.properties should be in the model file.
Why this works in a standalone application and not inside Tomcat requires some debugging.
As #5agado suggested, your model's version might be different than the library's.

Related

Saving documents using OpenOffice java api throws Exception

FYI / Context: I am running a portable installation of libreoffice on windows 10 (getting the same exception on a mac with normal installation though).
Reading the document
Maybe this is important... I am reading the document through an InputStream, because the other method fails due to a different exception (probably a story for another time).
public XComponent openFileViaStream(File file) throws CommandAbortedException, Exception {
Object fileAccess = this.componentFactory.createInstanceWithContext(SimpleFileAccessClass, this.context);
XSimpleFileAccess xSimpleFileAccess = (XSimpleFileAccess) UnoRuntime.queryInterface(XSimpleFileAccess.class,
fileAccess);
XStream xInputStream = xSimpleFileAccess.openFileReadWrite(file.getAbsolutePath());
PropertyValue[] loadProps = new PropertyBuilder().add("InputStream", xInputStream).build();
return loader.loadComponentFromURL("private:stream", "_blank", 0, loadProps);
}
Writing the document
PropertyBuilder is a utility class that just builds an Array of PropertyValues for ease of use.
public void save(Object storeMe, File destination) throws IOException, MalformedURLException {
//#formatter:off
PropertyValue[] propertyValue = new PropertyBuilder()
.add("Overwrite", Boolean.TRUE)
.add("FilterName", "StarOffice XML")
.build();
//#formatter:on
XStorable2 st = UnoRuntime.queryInterface(XStorable2.class, storeMe);
// already tried
// st.storeAsURL(destination.toURI().toURL().toString(), propertyValue);
// st.storeToURL(destination.toURI().toString(), propertyValue);
// st.storeToURL(destination.toURI().toURL().toString(), propertyValue);
st.storeAsURL(destination.toURI().toString(), propertyValue);
}
The exception
I couldn't find a solution while searching on stackoverflow...
com.sun.star.task.ErrorCodeIOException: SfxBaseModel::impl_store <file:/E:/test/abc.odt> failed: 0x81a(Error Area:Io Class:Parameter Code:26)
at com.sun.star.lib.uno.environments.remote.Job.remoteUnoRequestRaisedException(Job.java:173)
at com.sun.star.lib.uno.environments.remote.Job.execute(Job.java:139)
at com.sun.star.lib.uno.environments.remote.JobQueue.enter(JobQueue.java:334)
at com.sun.star.lib.uno.environments.remote.JobQueue.enter(JobQueue.java:303)
at com.sun.star.lib.uno.environments.remote.JavaThreadPool.enter(JavaThreadPool.java:87)
at com.sun.star.lib.uno.bridges.java_remote.java_remote_bridge.sendRequest(java_remote_bridge.java:636)
at com.sun.star.lib.uno.bridges.java_remote.ProxyFactory$Handler.request(ProxyFactory.java:146)
at com.sun.star.lib.uno.bridges.java_remote.ProxyFactory$Handler.invoke(ProxyFactory.java:128)
at com.sun.proxy.$Proxy10.storeAsURL(Unknown Source)
at DocumentHandler.save(DocumentHandler.java:54)
at Main.test(Main.java:14)
at Main.main(Main.java:19)
I really have no idea what I am doing wrong. I've looked at examples from api.libreoffice.org etc.
Am I missing something? A PropertyValue?
Thank you in advance!
See if any of these ideas help.
The URI should look like file:///E:/test/abc.odt.
Set the filter name to StarOffice XML (Writer) or writer8. Or don't set it at all; pass one property instead of two.
Verify you have the authorization to write to the file, for example by using standard Java libraries to create a file in that location. Be sure the file is not locked by some other process.

Getting NullPointerException when getting the values from Properties file in Selenium

I am trying to get the properties(key:value pairs) from the properties file but I am getting Null Pointer Exception.
Properties prop;
#BeforeTest
public void beforeTest() throws IOException {
prop=new Properties();
FileInputStream objfile = new FileInputStream(
"\\resources\\config.properties");
prop.load(objfile);
objfile.close();
}
The folder structure is
You should load the properties as a resource, since they are part of your project. No need to use full file system path.
Properties prop = new Properties();
#BeforeTest
public void beforeTest() throws Exception {
try (InputStream in = getClass().getResourceAsStream("/Config.properties") {
prop.load(in);
}
}
Please note that it's not recommended to use the default Java package to avoid classpath clashes. Moving Config.properties to a named package would solve it.
Have you tried to run in debug mode? Seems like your prop object is null. So there is nothing to load to. You need to create your Properties object inside your method. Move the constructor inside the method and then check.
#BeforeTest
public void beforeTest() throws IOException {
Properties prop=new Properties();
FileInputStream objfile = new FileInputStream(
"C:\\Users\\psailaja\\workspace\\TestPropertiesAndKeyword\\Config.properties");
prop.load(objfile);
objfile.close();
}
While trying to get the properties(key:value pairs) from the properties file you are getting Null Pointer Exception. You have to take care of a couple of facts here as follows:
Change the name of the property file from config.properties to config.property
While you take help of project location, use the reference through .
You need to initialize the Properties type of object prop.
Here would be your working code:
Properties prop;
#BeforeTest
public void beforeTest() throws IOException {
File src = new File("./resources/config.property");
FileInputStream fis = new FileInputStream(src);
prop = new Properties();
prop.load(fis);
String propValue = prop.getProperty("propKey");
}
Thanks for the response
The issue is due to not declaring the valid Access Modifier(public static) to the variables that are extracted from the Properties file.
There is no problem in below points
We can declare the properties file without using the resources folder
we can declare the properties file as config.properties or config.property there is no need of any proper name
We can get the file without using the format getClass().getResourceAsStream("/Config.properties") by using FIS

How to get files from resources folder. Spring Framework

I'm trying to unmarshal my xml file:
public Object convertFromXMLToObject(String xmlfile) throws IOException {
FileInputStream is = null;
File file = new File(String.valueOf(this.getClass().getResource("xmlToParse/companies.xml")));
try {
is = new FileInputStream(file);
return getUnmarshaller().unmarshal(new StreamSource(is));
} finally {
if (is != null) {
is.close();
}
}
}
But I get this errors:
java.io.FileNotFoundException: null (No such file or directory)
Here is my structure:
Why I can't get files from resources folder? Thanks.
Update.
After refactoring,
URL url = this.getClass().getResource("/xmlToParse/companies.xml");
File file = new File(url.getPath());
I can see an error more clearly:
java.io.FileNotFoundException: /content/ROOT.war/WEB-INF/classes/xmlToParse/companies.xml (No such file or directory)
It tries to find WEB-INF/classes/
I have added folder there, but still get this error :(
I had the same problem trying to load some XML files into my test classes. If you use Spring, as one can suggest from your question, the easiest way is to use org.springframework.core.io.Resource - the one Raphael Roth already mentioned.
The code is really straight forward. Just declare a field of the type org.springframework.core.io.Resource and annotate it with org.springframework.beans.factory.annotation.Value - like that:
#Value(value = "classpath:xmlToParse/companies.xml")
private Resource companiesXml;
To obtain the needed InputStream, just call
companiesXml.getInputStream()
and you should be okay :)
But forgive me, I have to ask one thing: Why do you want to implement a XML parser with the help of Spring? There are plenty build in :) E.g. for web services there are very good solutions that marshall your XMLs into Java Objects and back...
ClassLoader classLoader = getClass().getClassLoader();
File file = new File(classLoader.getResource("fileName").getFile());
you are suppose to give an absolute path (so add a loading ´/´, where resource-folder is the root-folder):
public Object convertFromXMLToObject(String xmlfile) throws IOException {
FileInputStream is = null;
File file = new File(String.valueOf(this.getClass().getResource("/xmlToParse/companies.xml")));
try {
is = new FileInputStream(file);
return getUnmarshaller().unmarshal(new StreamSource(is));
} finally {
if (is != null) {
is.close();
}
}
}

Can Xerces support XMLCatalogResolver and <xs:include/> at the same time?

Xerces claims to allow XML Catalog support to be added to a reader like this:
XMLCatalogResolver resolver = new XMLCatalogResolver();
resolver.setPreferPublic(true);
resolver.setCatalogList(catalogs);
XMLReader reader = XMLReaderFactory.createXMLReader(
"org.apache.xerces.parsers.SAXParser");
reader.setProperty("http://apache.org/xml/properties/internal/entity-resolver",
resolver);
But as soon as I do this then any <xs:include/> tags in my schemas are no longer processed. It seems like the XMLCatalogResolver becomes the only go-to place for entity resolution once it's added, so includes can't work anymore. Eclipse OTOH successfully validates using the same catalog, so it should be possilbe.
Is there a way around this, or are there any other Java based validators that support catalogs?
Thanks, Dominic.
I finally solved this by overriding the XMLCatalogResolver and logging the various calls made to the resolveEntity() method. I observed 3 types of call being made, only one of which made sense to be resolved using the XML catalog. So, I merely returned a FileInputStream directly for the other two call types.
Here is the code I used inside my custom XMLCatalogResolver class:
public XMLInputSource resolveEntity(XMLResourceIdentifier resourceIdentifier)
throws IOException
{
if(resourceIdentifier.getExpandedSystemId() != null)
{
return new XMLInputSource(resourceIdentifier.getPublicId(),
resourceIdentifier.getLiteralSystemId(),
resourceIdentifier.getBaseSystemId(),
new FileReader(getFile(resourceIdentifier.getExpandedSystemId())),
"UTF-8");
}
else if((resourceIdentifier.getBaseSystemId() != null) &&
(resourceIdentifier.getNamespace() == null))
{
return new XMLInputSource(resourceIdentifier.getPublicId(),
resourceIdentifier.getLiteralSystemId(),
resourceIdentifier.getBaseSystemId(),
new FileReader(getFile(resourceIdentifier.getBaseSystemId())),
"UTF-8");
}
else
{
return super.resolveEntity(resourceIdentifier);
}
}
private File getFile(String urlString) throws MalformedURLException
{
URL url = new URL(urlString);
return new File(url.toURI());
}
I'm not sure why this wouldn't be done by default within Xerces, but hopefully this helps the next person that encounters this problem.

How to read a properties file in Java? [duplicate]

This question already has answers here:
Where to place and how to read configuration resource files in servlet based application?
(6 answers)
Closed 7 years ago.
I am using servlets where I hard-code the database connection details, so if make any change I have to recompile the code. So instead I'd like to use a .properties file (which I can modify later) and use that as the source for my database connection.
The problem is I don't know how to read the property file. Could someone please help me to read the file?
. . .
// create and load default properties
Properties defaultProps = new Properties();
FileInputStream in = new FileInputStream("defaultProperties");
defaultProps.load(in);
in.close();
// create application properties with default
Properties applicationProps = new Properties(defaultProps);
// now load properties from last invocation
in = new FileInputStream("appProperties");
applicationProps.load(in);
in.close();
. . .
Example is coming from here Properties (Java)
The methods of Properties can throw exceptions.
- When the file path is not valid (FileNotFoundException). Please try to create a File object and check, whether the File is existing.
- ...
You may take a look at Apache Commons Configuration. Using it you can read properties file like that:
Configuration config = new PropertiesConfiguration("user.properties");
String connectionUrl = config.getString("connection.url");
This information regarding file location may be also important:
If you do not specify an absolute
path, the file will be searched
automatically in the following
locations:
in the current directory
in the user home directory
in the classpath
So in case of reading properties file in a servlet you should put properties file in a classpath (e.g. in WEB-INF/classes).
You can find more examples at their website.
You can use java.util.Properties
The biggest problem in reading a property file in web application is that you actually don't know about the actaul path of the file. So we have to use the relative path and for that we have to use various functions and classes like getresourceAsStream(), InputStream, FileinputStream etc.
And the method getReourceAsStream behaves differently in static and non static methogs..
you can do this in below way
Non Static
InputStream input = getClass().getClassLoader().getResourceAsStream("config.properties");
Static
InputStream input = ReadPropertyFile.class.getClassLoader().getResourceAsStream("config.properties");
For complete reference you can follow these links..
http://www.codingeek.com/java/using-getresourceasstream-in-static-method-reading-property-files
http://www.codingeek.com/java/read-and-write-properties-file-in-java-examples/
InputStream in = Thread.currentThread().getContextClassLoader().getResourceAsStream("my.properties");
Properties p = new Properties();
p.load(in);
in.close();
The below code, will add a Listener which checks for file configured with dbprops system property. For every given interval it will look if the file is modified, if it is modified it will load the Properties from the file.
package com.servlets;
import java.io.File;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.Timer;
import java.util.TimerTask;
import javax.servlet.ServletContext;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
public class DBPropsWatcherListener
implements ServletContextListener
{
public void contextInitialized(ServletContextEvent event)
{
ServletContext servletContext = event.getServletContext();
Timer timer = new Timer("ResourceListener");
timer.schedule(new MyWatcherTask(servletContext), 15);
}
public void contextDestroyed(ServletContextEvent event)
{
}
private class MyWatcherTask extends TimerTask
{
private final ServletContext servletContext;
private long lastModifiedTime = -1;
public MyWatcherTask(ServletContext servletContext)
{
this.servletContext = servletContext;
}
public void run()
{
try {
File resourceFile = new File(System.getProperty("dbProps"));
long current = resourceFile.lastModified();
if (current > lastModifiedTime) {
java.io.InputStream dbPropsStream = new FileInputStream(resourceFile );
java.util.Properties dbProps = new java.util.Properites();
dbProps.load(dbPropsStream);
realoadDBProps();
}
lastModifiedTime = current;
} catch (MalformedURLException e) {
e.printStackTrace();
}
}
}
}
Below program read the properties file a display using key value pair
File f1 = new File("abcd.properties");
FileReader fin = new FileReader(f1);
Properties pr = new Properties();
pr.load(fin);
Set<String> keys = pr.stringPropertyNames();
Iterator<String> it = keys.iterator();
String key, value;
while (it.hasNext())
{
key = it.next();
value = pr.getProperty(key);
System.out.println(key+":"+value);
}
}
If your application is small enough with only a handful of properties coming from just one or two property files, then I would suggest to use the JDK's own Properties class which load the properties from a file and use it just like the way you use a hashtable. Properties class itself inherits from Hashtable. But, your application is significantly large with sizable number of properties coming from different sources like property files, xml files, system properties then I would suggest to use Apache commons configuration. It presents a unified view of properties from across different configuration sources and allows you to define an override and preference mechanism for common properties appearing in different sources. Refer this article http://wilddiary.com/reading-property-file-java-using-apache-commons-configuration/ for a quick tutorial on using the commons configuration.
This may work::
Properties prop = new Properties();
FileReader fr = new FileReader(filename);
prop.load(fr);
Set<String> keys = pr.stringPropertyNames();
//now u can get the values from keys.
The Properties class has a convenient load method. That's the easiest way to read a java properties file.
That is a good idea to read the database values from properties file
You can use a properties class from Util package. The important thing to keep in mind is closing the stream after reading the file or writing the file to disk. Otherwise it causes problems. Here is an example for your reference:
import java.io.FileInputStream;
import java.io.IOException;
import java.util.Properties;
public class App
{
public static void main( String[] args )
{
Properties prop = new Properties();
try {
//load a properties file
prop.load(new FileInputStream("config.properties"));
//get the property value and print it out
System.out.println(prop.getProperty("database"));
System.out.println(prop.getProperty("dbuser"));
System.out.println(prop.getProperty("dbpassword"));
} catch (IOException ex) {
ex.printStackTrace();
}
}
}
Output
localhost
mkyong
password
ResourceBundle rb = ResourceBundle.getBundle("mybundle");
String propertyValue = rb.getString("key");
assuming mybundle.properties file is in classpath
Read this.Usually the properties file is kept in the classpath so that this method can read it.

Categories