How to set OpenNLP Model binary file path in Eclipse? - java

I have downloaded the Binary model files from here but i am not getting how do i have to set the path. I am using Eclipse, i tried adding these binary file into the path of my project.Second point is,I am not working on Maven Project.
import opennlp.tools.sentdetect.SentenceDetectorME;
import opennlp.tools.sentdetect.SentenceModel;
import opennlp.tools.util.InvalidFormatException;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
public class OpenNlpTest {
public static void SentenceDetect() throws InvalidFormatException,
IOException {
String paragraph = "Hi. How are you? This is Mike.";
// always start with a model, a model is learned from training data
InputStream is = new FileInputStream("en-sent.bin");
SentenceModel model = new SentenceModel(is);
SentenceDetectorME sdetector = new SentenceDetectorME(model);
String sentences[] = sdetector.sentDetect(paragraph);
System.out.println(sentences[0]);
System.out.println(sentences[1]);
is.close();
}
public static void main(String []z){
try {
SentenceDetect();
} catch (InvalidFormatException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
this is the code i am trying to run but it gives the following error
java.io.FileNotFoundException: en-sent.bin (The system cannot find the file specified)
at java.io.FileInputStream.open(Native Method)
at java.io.FileInputStream.<init>(Unknown Source)
at java.io.FileInputStream.<init>(Unknown Source)
at OpenNlpTest.SentenceDetect(OpenNlpTest.java:17)
at OpenNlpTest.main(OpenNlpTest.java:31)
This is the hierarchy of my project which i have just started

I got the solution.
This helped me to crack the problem
Basically what I did is I gave the absolute path of the file rather than relative path.

As you can see it is showing
java.io.FileNotFoundException: de-sent.bin (The system cannot find the file specified)
You haven't added necessary dependent libraries.In the link you've provided there is a bin named de-sent.bin.You have to add it to the library path

Related

How can I convert a xml file to a xml written with javascript file with java?

I have got two XML files from two different databases, but they has got the same infos. One of them has got VSReports and the other Jasperreports(JavaScript). I has to convert the XML file from VSReports into the Jasperreports. The only programming language I am allowed to use is java.
I am already stucked when I try to read in a xml file with my code.
import javax.swing.*;
import java.io.IOException;
import java.nio.charset.Charset;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
public class InputBox {
public static void main(String[] args) {
Pfad();
}
//opens JFileChooser
public static void Pfad() {
JFileChooser chooser = new JFileChooser();
int rueckgabeWert = chooser.showOpenDialog(null);
if (rueckgabeWert == JFileChooser.APPROVE_OPTION) {
System.out.println("Die zu öffnende Datei ist: "
+ chooser.getSelectedFile().getName());
}
Path path = Paths.get(chooser.getSelectedFile().getName());
String content = null;
try {
content = Files.readString(path, Charset.defaultCharset());
} catch (IOException e) {
e.printStackTrace();
}
//System.out.println the content of the file
System.out.println(content);
}
}
It works really fine with a txt file, but when I try a XML file it comes to a error:
java.nio.file.NoSuchFileException: 123.xml
at java.base/sun.nio.fs.WindowsException.translateToIOException(WindowsException.java:85)
at java.base/sun.nio.fs.WindowsException.rethrowAsIOException(WindowsException.java:103)
at java.base/sun.nio.fs.WindowsException.rethrowAsIOException(WindowsException.java:108)
at java.base/sun.nio.fs.WindowsFileSystemProvider.newByteChannel(WindowsFileSystemProvider.java:231)
at java.base/java.nio.file.Files.newByteChannel(Files.java:370)
at java.base/java.nio.file.Files.newByteChannel(Files.java:421)
at java.base/java.nio.file.Files.readAllBytes(Files.java:3205)
at java.base/java.nio.file.Files.readString(Files.java:3283)
at InputBox.Pfad(InputBox.java:26)
at InputBox.main(InputBox.java:10)
null
you are trying to open a file without giving it an absolute path, it will only work if the file is in the current working directory
Based on your description, it seems you didn't pass the full name of the xml file to your function.
So try
File f = chooser.getSelectedFile();
String path = f.getAbsolutePath + f.getName();
try {
content = Files.readString(path, Charset.defaultCharset());
} catch (IOException e) {
e.printStackTrace();
}
Hope this help.
As said, File#getName() loses the directory part of the path, so it will only find a file if it is in the current directory. To convert a File object to a Path, just use its toPath() method:
Path path = chooser.getSelectedFile().toPath();

util.Properties bug corrupting .properties file on win10

This bug is happening only in win10. Currently in win7 it does not happen.
Essentially I write some text that the user has inputted in various fields.
I use this to save all the info the user wrote, so when the application is relaunched the info is already there.
Also, I call this class every time the user clicks a button.
With normal behavior, the application runs fine on win7, and it saves everything properly. With win10 though, saving effectively "corrupts" the file, or at least, it makes it empty.
package whatever;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.Properties;
public class SaveFile {
public static void mainSave(String[] args) {
Properties prop = new Properties();
OutputStream output = null;
try {
String instaPathCombined = "config.properties";
output = new FileOutputStream(instaPathCombined);
// set the properties value, the get methods all retrieve a string.
prop.setProperty("a", UI.geta());
prop.setProperty("b", UI.getb());
prop.setProperty("c", UI.getc());
prop.setProperty("d", UI.getd());
prop.setProperty("e", UI.gete());
prop.setProperty("f", UI.getf());
prop.setProperty("g", UI.getg());
prop.setProperty("h", UI.geth());
prop.setProperty("i", UI.geti());
prop.setProperty("j", UI.getj());
prop.setProperty("k", UI.getk());
prop.setProperty("l", UI.getl());
prop.setProperty("m", UI.getm());
prop.setProperty("n", UI.getn());
prop.setProperty("o", UI.geto());
prop.setProperty("p", UI.getp());
prop.setProperty("q", UI.getq());
prop.setProperty("r", UI.getr());
prop.setProperty("s", UI.gets());
prop.setProperty("t", UI.gett());
prop.setProperty("u", UI.getu());
prop.setProperty("v", UI.getv());
// save properties to project root folder
prop.store(output, null);
} catch (IOException io) {
io.printStackTrace();
} finally {
if (output != null) {
try {
output.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
I have no idea on why it does this.
Also, this is literally a win10 issue, no other factors matter here, as I tried it on a VM with a fresh win10 installation and it happened.

getClassLoader().getResourceAsStream method returns null for a file with ".LIB" extension

I'm trying to load a file from resources/ path using
getClassLoader().getResourceAsStream("file.LIB")
but the method always returns null, unless I rename the file into another extension, say ".dll".
I've looked into the official Java documentation, but to no avail.
Why does the method acts strange on that file type?
Note: I'm using JDK 1.8.0_111 x86 (due to constraints on that lib file, which only works well with a 32-bit JVM)
It does works for me, you need to be sure what exactly you are doing with lib file.
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;
public class FileHelper {
public String getFilePathToSave() {
Properties prop = new Properties();
String filePath = "";
try {
InputStream inputStream =
getClass().getClassLoader().getResourceAsStream("abc.lib");
prop.load(inputStream);
filePath = prop.getProperty("json.filepath");
} catch (IOException e) {
e.printStackTrace();
}
return filePath;
}
public static void main(String args[]) {
FileHelper fh = new FileHelper();
System.out.println(fh.getFilePathToSave());
}
}

Creating a Java.util.List from an existing .txt file 'FIXED'

I'm trying to create a java.util.List from an existing .txt file, when the list is created and filled with the java.util.String from the .txt file I would like to print it to the console to test if the List is filled. I have tried a lot but I always keep getting the same error.
The layout of the project is as following: Module (QaA), src(WriteTest.java and testfile.txt).
The testfile.txt contains the following:
Sentence one testing
Sentence two testing
Sentence three testing
Sentence four testing
the WriteTest Class:
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.List;
public class WriteTest {
public static void main(String[] args) throws IOException {
Path testPath = Paths.get("src/testfile.txt");
try {
List<String> lines = Files.readAllLines(testPath);
for (String line : lines) {
System.out.println(line);
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
The following errors are received:
java.nio.file.NoSuchFileException: src\testfile.txt
at sun.nio.fs.WindowsException.translateToIOException(WindowsException.java:79)
at sun.nio.fs.WindowsException.rethrowAsIOException(WindowsException.java:97)
at sun.nio.fs.WindowsException.rethrowAsIOException(WindowsException.java:102)
at sun.nio.fs.WindowsFileSystemProvider.newByteChannel(WindowsFileSystemProvider.java:230)
at java.nio.file.Files.newByteChannel(Files.java:361)
at java.nio.file.Files.newByteChannel(Files.java:407)
at java.nio.file.spi.FileSystemProvider.newInputStream(FileSystemProvider.java:384)
at java.nio.file.Files.newInputStream(Files.java:152)
at java.nio.file.Files.newBufferedReader(Files.java:2784)
at java.nio.file.Files.readAllLines(Files.java:3202)
at java.nio.file.Files.readAllLines(Files.java:3242)
at WriteTest.main(WriteTest.java:15)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:497)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:140)
I have found another reason why I had loads of errors. The location of my .txt file should not have been within the src. I tried moving it out, parallel to my module, and there it was detectable.
Your code had error on my workspace, I changed the readAllLines as you see below, and added toAbsolutePath for the path
public static void main(String[] args) throws IOException
{
Path testPath = Paths.get("src/testfile.txt").toAbsolutePath();
System.out.println("path:\t" + testPath);
try
{
Charset cs = Charset.defaultCharset();
List<String> lines = Files.readAllLines(testPath, cs);
for (String line : lines)
{
System.out.println(line);
}
}
catch (IOException e)
{
e.printStackTrace();
}
}
Make the changes as below to get the given file path.
Path testPath = Paths.get(System.getProperty("user.dir")+"src/testfile.txt");

The exe doesn't run

I used the following Java code for move the file form one directory to another, then execute the file in the destination directory. My code is
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.IOException;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import org.apache.commons.io.FileUtils;
public class JFileChooserTest {
public static void main(String[] args) {
String filelocation="C:\\Users\\FSSD\\Desktop\\OutPut\\Target";
File trgDir = new File(filelocation);
System.err
.println("file location>>>>>>>>>>>>>>>>>>>"
+ filelocation);
File desDir = new File(
"C:\\Users\\FSSD\\IndigoWorkSpace\\Swing\\test");
try {
FileUtils.copyDirectory(trgDir, desDir);
// FileUtils.copyDirectory(srcDir, trgDir);
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
Runtime rt=Runtime.getRuntime();
try {
Process p=rt.exec("test\\setup.exe");
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
While i executing this code, the file was successfully moved by i execute this code i got the following error.
java.io.IOException: Cannot run program "test\setup.exe": CreateProcess error=740, The requested operation requires elevation
at java.lang.ProcessBuilder.start(ProcessBuilder.java:460)
at java.lang.Runtime.exec(Runtime.java:593)
at java.lang.Runtime.exec(Runtime.java:431)
at java.lang.Runtime.exec(Runtime.java:328)
at JFileChooserTest.main(JFileChooserTest.java:34)
Caused by: java.io.IOException: CreateProcess error=740, The requested operation requires elevation
at java.lang.ProcessImpl.create(Native Method)
at java.lang.ProcessImpl.<init>(ProcessImpl.java:81)
at java.lang.ProcessImpl.start(ProcessImpl.java:30)
at java.lang.ProcessBuilder.start(ProcessBuilder.java:453)
... 4 more
Here i used "setup.exe". It is an executable file. while i execute at run time i got the above error. Why it will happens, is any possible to resolve it. Thanks in advance..
I'm guessing you are using windows 7, when you run your batch process to start your JFileChooser test, right click and select "run as administrator".
The requested operation requires elevation
You do not have the rights to execute this setup. Probably UAC blocks it.
Use the runas command with Runtime#exec in Java.

Categories

Resources