opennlp with netbeans is not giving output - netbeans

How to use opennlp with netbeans. I made a small program as given in apache document but it is not working. I have set path to the opennlp bin as stated in the apache document but still i m not geting an output. it is not able to find .bin and hence SentenceModel model.
package sp;
public class Sp {
public static void main(String[] args) throws FileNotFoundException {
InputStream modelIn ;
modelIn = new FileInputStream("en-token.bin");
try {
SentenceModel model = new SentenceModel(modelIn);
}
finally {
if (modelIn != null) {
try {
modelIn.close();
}
catch (IOException e) {
}
}
}
}
}

The current working directory when you run in Netbeans is the base project directory (the one with build.xml in it). Place your .bin file there, and you should be able to open the file like that.

Related

Java WatchService code works in Eclipse but same code fails in Tomcat in Docker

In Eclipse, a unit test fires up this code and when the watched file is altered, outputs the new text.
In Tomcat, it hangs where shown.
I've grovelled around online for quite some time trying to find anyone with a similar problem, but nothing showed up.
This is vanilla watcher code, and because it works fine in Eclipse, there must be a Tomcat-specific issue, but what?
The file is accessible to the code in Tomcat. It's not a permissions problem.
public class Foo extends Thread {
File watchedFile = <the watched file>;
WatchService watcher = FileSystems.getDefault().newWatchService();
Path dir = watchedFile.getParentFile().toPath();
dir.register(watcher, StandardWatchEventKinds.ENTRY_MODIFY);
#Override
public void run() {
while (true) {
WatchKey key;
try {
key = watcher().take(); <<< HANGS HERE IN TOMCAT, DOESN'T HANG HERE IN ECLIPSE.
} catch (InterruptedException | ClosedWatchServiceException e) {
break;
}
try {
for (WatchEvent<?> event : key.pollEvents()) {
WatchEvent.Kind<?> kind = event.kind();
if (kind == StandardWatchEventKinds.OVERFLOW) {
continue;
}
if (kind == StandardWatchEventKinds.ENTRY_MODIFY) {
WatchEvent<Path> pathEvent = (WatchEvent<Path>)event;
if (pathEvent.context().toString().equals(watchedFile.getName()) {
// Do something.
}
}
}
} catch (Exception e) {
throw new RuntimeException("Trouble: " + e.getMessage(), e);
}
if (!key.reset()) {
break;
}
}
}
}
UPDATE: . The problem was that I was writing to the file from outside Docker, so the change event wasn’t seen.
It’s similar to Java WatchService not generating events while watching mapped drives.

Eclipse - Print file's entry into console

I have some file containing lots of numbers (test input), so I want to print it somehow to console.
But If I go to run configuration and I set InputFile: to input.txt then console returns:
[Invalid file specified for stdin file: input.txt]
Anyone know what's the problem?
I'm not sure, about how you want to access your input file. As far as I know, there is no eclipse feature, that allows using files as System.in. (see Eclipse reading stdin (System.in) from a file )
I tried to reproduce your eclipse setup (as posted in your comment) and built a simple program, that outputs every line of the input.
Java8:
public class Main {
public static void main(String[] args) throws IOException, URISyntaxException {
// get resource from classpath
URL resource = ClassLoader.getSystemResource("quickfind/largeUF.txt");
Path path = Paths.get(resource.toURI());
// read all lines
List<String> allLines = Files.readAllLines(path);
// print all lines
allLines.stream().forEach(System.out::println);
}
}
Java7:
public class Main {
public static void main(String[] args) throws IOException, URISyntaxException {
// get resource from classpath
URL resource = ClassLoader.getSystemResource("quickfind/largeUF.txt");
Path path = Paths.get(resource.toURI());
// read all lines
List<String> allLines = Files.readAllLines(path);
// print all lines
for (String line : allLines) {
System.out.println(line);
}
}
}

Error in uploading a file using Jersey rest service

I am using jersey for building rest service which will upload a file. But I am facing problem in writing a file to required location. Java throws a system cannot find specified path error. Here is my Web service :
#POST
#Path("/fileupload")
#Consumes(MediaType.MULTIPART_FORM_DATA)
public Response uploadFile(#FormDataParam("file")InputStream fileUploadStream, #FormDataParam("file")FormDataContentDisposition fileDetails) throws IOException{
StringBuilder uploadFileLocation= new StringBuilder();
uploadFileLocation.append("c:/logparser/webfrontend/uploads");
uploadFileLocation.append("/"+dateFormat.format(Calendar.getInstance().getTime()));
uploadFileLocation.append("/"+fileDetails.getFileName());
writeToFile(fileUploadStream, uploadFileLocation.toString());
return Response.status(200).entity("File saved to " + uploadFileLocation).build();
}
private void writeToFile(InputStream uploadInputStream, String uploadFileLocation)
{
log.debug("UploadService , writeToFile method , start ()");
try{
int read = 0;
byte[] bytes = new byte[uploadInputStream.available()];
log.info("UploadService, writeToFile method , copying uploaded files.");
OutputStream out = new FileOutputStream(new File(uploadFileLocation));
while ((read = uploadInputStream.read(bytes)) != -1)
{
out.write(bytes, 0, read);
}
out.flush();
out.close();
}
catch(Exception e)
{
log.error("UploadService, writeToFile method, error in writing to file "+e.getMessage());
}
}
From looking at just the code (it's usually helpful to include the exception and stack trace), you're trying to write to a directory based on a timestamp which doesn't exist yet. Try adding a call to File.mkdir/mkdirs. See this question/answer: FileNotFoundException (The system cannot find the path specified)
Side note - Unless you have a reason not to, I'd consider using something like Apache commons-io(FileUtils.copyInputStreamToFile) to do the writing.

Xtend Code Generator How to Copy Files

I am implementing my own DSL and using Xtend to generate codes. I need some static resources to be copied to my generate code. I was trying to use commons-io, but I couldn't get anywhere with that! What is the best way to do so? I am trying to avoid reading each file and writing to the corresponding file in output path...
This should do (taken from this web site, slightly modified, not tested)
def static void copyFileUsingChannel(File source, File dest) throws IOException {
FileChannel sourceChannel = null;
FileChannel destChannel = null;
try {
sourceChannel = new FileInputStream(source).getChannel();
destChannel = new FileOutputStream(dest).getChannel();
destChannel.transferFrom(sourceChannel, 0, sourceChannel.size());
}finally{
sourceChannel.close();
destChannel.close();
}
}

Java EE Application - Copy folder from war to file system with

I have a Java EE application which is packaged as a war file.
Under WEB-INF/classes/ I have a config folder which should be copied at startup of the Java EE application into the file system.
String[] filesToCopy = {"foo", ...};
for (String fileName : filesToCopy) {
URL resource = classLoader.getResource(CONFIG_FOLDER_IN_WAR + fileName);
File targetFile = new File(configFolderPath, fileName);
org.apache.commons.io.FileUtils.copyURLToFile(resource, targetFile);
}
that was working so far. But now the config folder contains also subfolders and a lot of files so that I don't want to list them manually.
Is there a way to copy the whole folder incl. all subfolders?
Because I couldn't find a better solution I did it by myself and put it in my own FileUtils:
public static void copyFromWarToFolder(String folderInWar, File targetFolder) {
try {
URL resource = FileUtils.class.getClassLoader().getResource(folderInWar);
VirtualFile virtualFileOrFolder = VFS.getChild(resource.toURI());
copyFromWarToFolder(virtualFileOrFolder, targetFolder);
} catch (Exception e) {
e.printStackTrace();
}
}
private static void copyFromWarToFolder(VirtualFile virtualFileOrFolder, File targetFolder) throws Exception {
if (virtualFileOrFolder.isDirectory()) {
File innerTargetFolder = new File(targetFolder, virtualFileOrFolder.getName());
innerTargetFolder.mkdir();
for (VirtualFile innerFileOrFolder : virtualFileOrFolder.getChildren()) {
copyFromWarToFolder(innerFileOrFolder, innerTargetFolder);
}
} else {
org.apache.commons.io.FileUtils.copyURLToFile(virtualFileOrFolder.asFileURL(), new File(targetFolder, virtualFileOrFolder.getName()));
}
}