Hi I am a novice in JAVA. I have been getting this file not found exception inspite of the file existing in the very location I have specified in the path which is
Initially I had the issue of file not found. However, after performing a clean and re-run, now I am having an issue which says
Error: Could not find or load main class main.main
import Message.*;
import java.util.*;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.PrintWriter;
public class main{
public static void main(String[] args) {
Message msg=new Message("bob","alice","request","Data####");
MPasser passerObj=new MPasser("C:\\Workspace\\config.txt","process1");
}
}
Also in the MPasser Constructor the following piece of relevant code is there
public class MPasser(String file_name,String someVariable){
InputStream input;
try {
input =new RandomAccessFile(file_name,"r");
} catch (FileNotFoundException e) {
e.printStackTrace();
}
Yaml yaml = new Yaml();
Map<String, String> Object = (Map<String, String>) yaml.load(input);
}
Sorry I have made edits from initial query so that it is more clear
On this line:
input = RandomAccessFile("C:\Workspace\conf.txt",'r');
You need to escape the \'s
input = RandomAccessFile("C:\\Workspace\\conf.txt",'r');
"C:\Workspace\conf.txt"
Those are escape sequences. You probably meant:
"C:\\Workspace\\conf.txt"
You also appear to call it config.txt in one snippet and conf.txt in the other?
Make sure the java process has permissions to read the file.
You have to escape the backslash.
input = RandomAccessFile("C:\\Workspace\\conf.txt",'r');
and also
input = new RandomAccessFile("C:\\Workspace\\conf.txt",'r');
and why you have two different filename conf.txt and config.txt.
Related
This question is about Android, although i dont think that this is android-specific.
I have a project that i want to use two files with: MainActivity.java and filetools.java . I have three methods in filetools.java, read, write and append.
I want to be able to do something like this in my MainActivity:
filetools.write("/sdcard/file.txt", "something");
The code for MainActivity is just the package, imports, the class, and onCreate.
The code for filetools:
package com.tylerr147.FileRW;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.util.Scanner;
public class filetools
{
public String read(String fName){
try{
File mFile = new File(fName);
String content = new Scanner(mFile).useDelimiter("\\Z").next();
return content;
}catch(Exception e) {
return "There was an error retrieving your file. The proccess returned this error:\n"+e.toString();
}
}
public boolean write(String loc, String stuff) {
File mfile = new File(loc);
try {
mfile.createNewFile();
FileOutputStream f = new FileOutputStream(mfile);
OutputStreamWriter f2 = new OutputStreamWriter(f);
f2.append(stuff);
f2.close();
f.close();
} catch(IOException e) {
return false;
}
return true;
}
public void append(String filename, String content) {
write(filename, read(filename)+content);
}
}
Another thing i would like to be able to do is to have a completely different app by the package com.app.importer
how could i do something like
import com.app.importer;
importerAppsMethod();
I have found a few posts on stackoverflow, but they do not help.
Importing my custom class and calling it's method?
There are a few more, and i have searched and can not find anything that works for me. Any help is appreciated
I am putting #ishmaelMakitla comment intocan answer.
To do something like filetools.write("/sdcard/file.txt", "something"); - you need to declare the write method as static. For example: public static boolean write(String loc, String stuff). You may have to do the same for all other methods if you want similar behavior. Is this what you are looking for?
I want to use stanford parser within the coreNLP.
I already got this example working:
http://stanfordnlp.github.io/CoreNLP/simple.html
BUT: I need the german model. So i downloaded "stanford-german-2016-01-19-models.jar".
But how can I set this jar file for usage?
I only found:
LexicalizedParser lp = LexicalizedParser.loadModel("englishPCFG.ser.gz");
but i have a jar with the germn models, NOT a ...ser.gz.
Can anyboady help?
Here is some sample code for parsing a German sentence:
import edu.stanford.nlp.io.IOUtils;
import edu.stanford.nlp.ling.CoreAnnotations;
import edu.stanford.nlp.pipeline.*;
import edu.stanford.nlp.simple.*;
import edu.stanford.nlp.trees.*;
import edu.stanford.nlp.util.CoreMap;
import edu.stanford.nlp.util.PropertiesUtils;
import edu.stanford.nlp.util.StringUtils;
import java.util.*;
public class SimpleGermanExample {
public static void main(String[] args) {
String sampleGermanText = "...";
Annotation germanAnnotation = new Annotation(sampleGermanText);
Properties germanProperties = StringUtils.argsToProperties(
new String[]{"-props", "StanfordCoreNLP-german.properties"});
StanfordCoreNLP pipeline = new StanfordCoreNLP(germanProperties);
pipeline.annotate(germanAnnotation);
for (CoreMap sentence : germanAnnotation.get(CoreAnnotations.SentencesAnnotation.class)) {
Tree sentenceTree = sentence.get(TreeCoreAnnotations.TreeAnnotation.class);
System.out.println(sentenceTree);
}
}
}
Make sure you download the full toolkit to use this sample code.
http://stanfordnlp.github.io/CoreNLP/
Also make sure you have there German models jar in your CLASSPATH. The code above will know to look at all the jars in your CLASSPATH and will recognize that file as being in the German jar.
First of all: This works, Thank you!
But, I don't need this complex way with all these annotators. Thats why I wanted to start with the simple CoreNLP Api. Thats my code:
import edu.stanford.nlp.simple.*;
import java.util.*;
public class Main {
public static void main(String[] args) {
Sentence sent = new Sentence("Lucy is in the sky with diamonds.");
List<String> posTags = sent.posTags();
List<String> words = sent.words();
for (int i = 0; i < posTags.size(); i++) {
System.out.println(words.get(i)+" "+posTags.get(i));
}
}
}
How can I get the german prperties file work with this example?
Or the other way: How do I get only the word with the pos tag in your example?
The german equivalent to the english example is the following:
LexicalizedParser lp = LexicalizedParser.loadModel("germanPCFG.ser.gz");
Extract the latest stanford-german-corenlp-2018-10-05-models.jar file and you will find it inside the folder: stanford-german-corenlp-2018-10-05-models\edu\stanford\nlp\models\lexparser
I have made a java program where in i can use any website to read its Html Content using Scanner class and Varargs.I am not able to get the output while i am using Scanner class and VarArgs.
Below is the following Code.
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.URL;
import java.util.Scanner;
public class ReadWebsite
{
public static void main(String[] args) throws Exception
{
URL oracle = new URL(args[0]);
Scanner s=new Scanner(oracle.openStream());
while (s.hasNext())
{
System.out.println(s.nextLine());
}
s.close();
}
}
OutputShown
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 0
at oodlesTech.ReadWebsite.main(ReadWebsite.java:15)
if you are running from eclipse you have to pass the arguments.
right click program - > run as - > run configurations -> arguments ->program arguments . in this tab pass the actual url which will be passed as args[0] to your main method.
You are not passing the argument to your java program.
For your testing you can either hard code it in code e.g. URL oracle = new URL("http://www.google.com"); or pass an argument to your java program, explained here
I know C++ at a decent level and I am trying to learn java. This will be a silly question but I cannot figure out how to import a .java file into another. I am at Eclipse IDE and in my project I have two files:
FileReader.java
Entry.java
I want to import the Entry.java in the other file but no matter what I do I get an error. Can you help me? Thx in advance.
FileReader.java :
import java.io.*;
class FileReader {
public static void main(String[] args) throws Exception {
System.out.println("Hello, World");
Entry a(10,"a title","a description");
a.print();
}
}
Entry.java:
public class Entry{
int ID;
String title;
String description;
public Entry(int id, String t,String d){
ID=id;
title=t;
description=d;
}
public void print(){
System.out.println("ID:"+ID);
System.out.println("Title:"+title);
System.out.println("Description:"+description);
}
}
At this state I get an error that Entry cannot be resolved as a variable. So I believe that it is related to the import.
Firstly
Entry a(10,"a title","a description");
should be
Entry a = new Entry (10,"a title","a description");
If Entry is in the same package then you will not need to import it.
If Entry is in a different package, say com.example then you will need to do
Either
import com.example.Entry;
or
import com.example.*;
The second import will import all classes in the com.example package - usually not such a good thing.
You need new Entry
The new keyword creates the new object
Entry a = new Entry(10,"a title","a description")
a.print();
An Entry object is created with the a reference with the above instantiation.
For the import part of your question, if two files are in the same package, no import is needed. If you Entry class was in a different package than your FileReader class, then you would need to import mypackage.Entry
Try
Entry a = new Entry(/*args*/);
And if you need to import the class, then use the absolute name (package+class) and put it after import above the class declaration
import com.example.you.Entry;
In Eclipse you can do Ctrl+Shift+O to resolve all imports.
I have a UDF jar which takes in a String as an input through Pig. This java file works through pig fine as running a 'hard coded' string such as this command
B = foreach f generate URL_UDF.mathUDF('stack.overflow');
Will give me the output I expect
My question is I am trying to get information from a text file and use my UDF with it. I load a file and want to pass data within that file which I have loaded to the UDF.
LoadData = load 'data.csv' using PigStorage(',');
f = foreach LoadData generate $0 as col0, $1 as chararray
$1 is the column I needed and researching data types (http://pig.apache.org/docs/r0.7.0/piglatin_ref2.html#Data+Types) a char array is used.
I then tryed using the following command
B = foreach f generate URL_UDF.mathUDF($1);
to pass the data into the jar which fails stating
java.lang.ClassCastException: org.apache.pig.data.DataByteArray cannot be cast to java.lang.String
If anybody has any solution to this that would be great.
The java code I am running is as follows
package URL_UDF;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import org.apache.pig.FilterFunc;
import org.apache.pig.data.Tuple;
import org.apache.pig.EvalFunc;
import org.apache.pig.PigWarning;
import org.apache.pig.data.Tuple;
import org.apache.commons.logging.Log;
import org.apache.*;
public class mathUDF extends EvalFunc<String> {
public String exec(Tuple arg0) throws IOException {
// TODO Auto-generated method stub
try{
String urlToCheck = (String) arg0.get(0);
return urlToCheck;
}catch (Exception e) {
// Throwing an exception will cause the task to fail.
throw new IOException("Something bad happened!", e);
}
}
}
Thanks
You can specify the schema with LOAD as follows
LoadData = load 'data.csv' using PigStorage(',') AS (col0: chararray, col1:chararray);
and pass col1 to the UDF.
Or
B = foreach LoadData generate (chararray)$1 AS col1:chararray;
Actually, this is a bug (PIG-2315) in Pig which will be fixed in 0.12.1. The AS clause in foreach does not work as one would expect.