import org.yaml cannot be resolved error - java

If anyone has any experience working with Yaml (.yml extension files), I could use some assistance. I am trying to write a little program that reads .yml files from a folder I made on my desktop.
import java.io.IOException;
import java.io.InputStream;
import org.yaml.snakeyaml.Yaml
public class readYaml {
public static void main(String[] args) throws IOException {
Yaml yaml = new Yaml();
try (InputStream in = readYaml.class.getResourceAsStream("/Collections.yml")) {
Question question = yaml.loadAs(in, Question.class);
System.out.println(question);
}
}
}
EDIT
I created a resources folder in my project file in response to an error on line 8 stating "This is coming from an error I am getting from an error I am getting on line 8, stating "The import org.yaml.snakeyaml.Yaml cannot be resolved"", but now I am getting this error. I think it will be best explained in screen shots. enter image description here
and this happens on my project folder
enter image description here
I hope this makes things clearer to understand.

Related

Java Compiler Completed but Didn't Create the Executable?

(I am new to Java, I don't know what 'classes' or 'api's' are.)
I was trying to compile (javac -g Sphinx.java) this code:
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.io.PrintWriter;
import edu.cmu.sphinx.api.Configuration;
import edu.cmu.sphinx.api.SpeechResult;
import edu.cmu.sphinx.api.LiveSpeechRecognizer;
public class Sphinx {
public static void main(String[] args) throws Exception {
Configuration configuration = new Configuration();
configuration.setAcousticModelPath("models/en-us/en-us");
configuration.setDictionaryPath("models/en-us/cmudict-en-us.dict");
configuration.setLanguageModelPath("models/en-us/en-us.lm.bin");
PrintWriter pw = new PrintWriter(new PrintWriter("status.txt"));
LiveSpeechRecognizer recognizer = new LiveSpeechRecognizer(configuration);
recognizer.startRecognition(true);
pw.print("running");
SpeechResult result = recognizer.getResult();
recognizer.stopRecognition();
pw.print("stopped");
pw.close();
PrintWriter pw2 = new PrintWriter(new PrintWriter("result.txt"));
pw2.println(result);
pw2.close();
}
}
And I got this message:
Note: Some input files use or override a deprecated API.
Note: Recompile with -Xlint:deprecation for details.
So, I re-compiled with -Xlint:deprecation, like it told me to, and it didn't give me any errors this time, so I'm assuming the compiler was finished, and that it compiled successfully.
And then I look, and there's no .jar file, just a new .class file.
Now, I don't really know much about the java compiler, I was just told online that it would give me an executable for the code I had written, which in this case is a .jar file.
I don't know if the compiler sends newly created executables to a special system directory or what, but it's not here, and I don't know why.
Would someone more knowledgeable with Java please give me some context here.
You need just add the next step - look at: docs.oracle.com/javase/tutorial/deployment/jar/build.html
jar cf jar-file input-file(s)

Importing a package with an interface and a class

I am trying to create a Java package to interface with Excel, so I am using the org.apache.poi libraries. I've found the documentation on the POI site that shows the following:
Reading and Rewriting Workbooks
(truncated)
Workbook wb = WorkbookFactory.create(inp);
Sheet sheet = wb.getSheetAt(0);
When I attempt to create a Workbook object, I get errors that it doesn't know the type. The documentation above doesn't show the import statements, but if I'm reading these POI docs correctly, I should be able to import org.apache.poi.ss.usermodel.Workbook, (and it's distinctly possible that I'm wrong because I'm very new to Java) but trying all of the following import statements all result in errors for the Workbook type.
import org.apache.poi.*;
import org.apache.poi.ss.*;
import org.apache.poi.ss.usermodel.*;
I should note that I'm using Eclipse Neon for this, and the editor is not complaining that it can't find the packages, so I'm not sure where the issue lies.
Workbook wb = WorkbookFactory.create(inp);
The actual error I'm getting in the editor is that "Workbook cannot be resolved to a type", and the same error for WorkbookFactory.
So the TL;DR is that the POI docs show that Workbook is an Interface in org.apache.poi.ss.usermodel and that WorkbookFactory is a class in the same usermodel package, but I can't figure out how to import/use them correctly.
EDIT:
The build time error I receive is
Exception in thread "main" java.lang.Error: Unresolved compilation problems:
Workbook cannot be resolved to a type
WorkbookFactory cannot be resolved
at spreadsheet.conversion.ConvertXLSXtoCSV.main(ConvertXLSXtoCSV.java:31)
The message I posted before was the editor popup... I was able to take a screenshot:
EDIT: Posting the full code, because, why not. (The multiple import statements
were just trying things out, mostly in desperation.)
package spreadsheet.conversion;
import java.text.ParseException;
import org.apache.poi.*;
import org.apache.poi.ss.*;
import org.apache.poi.ss.usermodel.*;
import java.io.FileInputStream;
import java.io.InputStream;
public class ConvertXLSXtoCSV {
private static String inputfilename = "";
private static String outputfilename = "";
private static int debug = 0;
private static void test(String arg) {
System.out.println(arg);
}
public static void main(String[] args) throws ParseException, Exception {
if (args.length != 1) {
throw new Exception("Requires 1 parameter. Usage: ConvertXLSXtoCSV <filename>");
}
inputfilename = args[0];
if(debug==1) {
test(inputfilename);
}
InputStream inp = new FileInputStream(inputfilename);
Workbook wb = WorkbookFactory.create(inp);
}
Any help is appreciated.
As unhelpful to others as this may be, it's the solution I've come up with so I'm posting it as an answer. Many thanks for the comments about this, but I've always hated Eclipse, and decided to go with NetBeans IDE instead. It was extremely trivial to right click on Libraries, Add JAR/Folder, and select the POI jar files, and it (and the integrated Maven install) took care of the details and it worked perfectly right off the bat and resolved all library dependencies.
I believe this due to jar file corrupted ( most probably during download for the first time). Here what you can do.
You can delete all poi folder in maven repository .m2/repository/org/apache/poi then you can update maven by Right Click on the project > Maven > Update Project. Eclipse will automatically download the dependencies and auto rebuild the workspace.
im facing the same thing like yours and manage to get it clean.

Java: Nothing will read in this file

Before I start, I'd like to say that I've spent 4 hours today, 6 hours yesterday and 3 hours before that researching this issue. I've read every post I can find, followed every instruction to the letter, restarted my project, reinstalled my IDE (Netbeans) and even fresh installed my OS, and I haven't found a single piece of helpful advice, so I figured I needed to ask for help.
AND YES, I HAVE PUT THE FILE IN THE RIGHT LOCATION
... As a matter of fact, I've put the file in EVERY location. There's a copy in every folder inside my project and also a copy in the overall Projects folder, and also in My Documents. I've checked and changed and defaulted the root directory many times. PLEASE don't tell me to just use an exception handler. The file the program reads in is guaranteed to exist and contain something.
So here's my question:
I'm trying to input and read from a file, however, the result is always that the file can't be found. Here's an example of my code (and it really is down to this atm):
package project2;
import java.io.FileReader;
import java.io.*;
import java.util.Scanner;
public class Project2 {
public static void main(String[] args) {
FileReader inputFile = new FileReader(args[0]);
}
}
Here are two of the errors I get (I also get Filenotfound errors, but I don't think I need to add that):
Exception in thread "main" java.lang.RuntimeException: Uncompilable source
code - unreported exception java.io.FileNotFoundException; must be caught or
declared to be thrown
at project2.Project2.main(Project2.java:14)
C:\Users\jarre\AppData\Local\NetBeans\Cache\8.2\executor-snippets\run.xml:53:
Java returned: 1
BUILD FAILED (total time: 1 second)
Exception in thread "main" java.util.NoSuchElementException: No line found
at java.util.Scanner.nextLine(Scanner.java:1540)
at project2.Project2.main(Project2.java:24)
C:\Users\jarre\AppData\Local\NetBeans\Cache\8.2\executor-snippets\run.xml:53:
Java returned: 1
BUILD FAILED (total time: 0 seconds)
That's it. The file name comes from the arguments, and I have tried every possible variation of the name. I have tried naming the file outside of the arguments, as just the file name itself and also with an explicit file path.
Using a scanner won't let me read anything in. FileReader won't even run.
The text file has no special formatting or characters, and I've used the one I was supplied with and multiple that I hand typed just in case there was an issue with the one I was given. I have also made sure that ".txt" is never read or used twice (I keep my extensions on, anyway).
I have checked attributes and permissions of all files and the Netbeans program itself. I've also made sure that the text files were included in the project build.
I am not using any additional code right now, as I can't do anything until I'm sure that I can read in a file, and then output one as well. I also know that the text files aren't corrupt because I can read them in Python just fine, however, I have to use Java and I have to use Netbeans.
This is a new problem for me, I've always been able to read in files fine, and I've exhausted my options. I really need some help if anyone has any ideas.
The first exception (java.lang.RuntimeException: Uncompilable source
code) is thrown because the code that you have shown us is not valid java source code.
new FileReader(args[0]) is declared as throwing FileNotFoundException and according to the rules of the java language you either have to catch this exception or declare your main method as throwing this exception.
One way to fix this problem is to write your main method like this:
public static void main(String[] args) throws FileNotFoundException {
FileReader inputFile = new FileReader(args[0]);
}
It seems that you have solved this issue because the second exception (java.util.NoSuchElementException: No line found) is thrown by the Scanner.nextLine() method if you try to read past the end of the file.
Since you have not shown any code using the Scanner class it's hard to tell where to problem is in this case.
As a matter of fact, I've put the file in EVERY location. There's a copy in every folder inside my project and also a copy in the overall Projects folder, and also in My Documents.
Don't do that. You are creating a mess with files that will be hard to cleanup. If you want to know which file your program is reading then adding the following simple line tells you the exact path and filename:
System.out.println(new File(args[0]).getAbsolutePath());
Have you ever tried with a simple, minimal example like this:
package project2;
import java.io.FileReader;
import java.io.*;
import java.util.Scanner;
public class Project2 {
public static void main(String[] args) {
System.out.println(new File(args[0]).getAbsolutePath());
FileReader inputFile = new FileReader(args[0]);
try (Scanner s = new Scanner(inputFile)) {
while (s.hasNextLine()) {
System.out.println(s.nextLine());
}
}
}
}
It should print out the name of your file with the complete path and then the contents of the file line by line.
I don't think Java is messing around with you a not found file is a not found file, please elaborate more in this issue by screens of files and directories you are working on.
I would like you to consider take a look at the following:
FileReader
Path of projects on Netbeans
I hope this helps may the code be with you.
This reads a file with no problem. I'll assume you're running JDK 8.
/**
* Read a file example
* User: mduffy
* Date: 4/21/2017
* Time: 7:48 AM
* #link http://stackoverflow.com/questions/43529600/java-nothing-will-read-in-this-file
*/
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
public class Project2 {
public static void main(String[] args) {
if (args.length > 0) {
BufferedReader reader = null;
try (BufferedReader bufferedReader = new BufferedReader(new FileReader(args[0]))) {
bufferedReader.lines().forEach(System.out::println);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
} else {
System.out.println("Usage: Project2 <file>");
}
}
}
Here's the input file I used:
line1
line2
hello, michael
line 4
Here's the output I got:
java Project2 .\src\main\resources\test.txt
line1
line2
hello, michael
line 4
Process finished with exit code 0

Files.copy(src, dst) throws NoSuchFileException

The program:
import java.io.IOException;
import java.nio.file.*;
import java.io.*;
public class Copier {
public static void main(String[] args)
{
try
{
File src = new File("C:\\Users\\k\\Desktop\\sandeep.txt");
File dstn = new File("C:\\Users\\k\\Desktop\\sandeep1.txt");
Files.copy(src.toPath(), dstn.toPath());
}
catch(IOException e) {
System.out.println(e.toString());
}
}
}
On my Desktop, sandeep.txt exists while sandeep1.txt doesn't. I have some text written in sandeep.txt that I want to copy to sandeep1.txt (after its creation). But when I try to do so, it is throwing the exception: java.nio.file.NoSuchFileException: C:\Users\k\Desktop\sandeep1.txt
This question has been asked by someone but is still unanswered so I am asking this again. Thank you so much in advance!
EDIT: In the beginning I tried Files.copy(src, dstn) but it didn't work so I tried Files.copy(dstn, src) and it still didn't work. While posting here I forgot to change it back. Now I have corrected it. Please have a look again.
SOLVED: I did a mistake while creating the file. I created a new text file and named it sandeep.txt. So effectively this file has name sandeep.txt.txt. I am embarrassed that I did this mistake but I will remember it for long. Can't thank you all enough for giving your valuable time.
janos is correct that you have the order wrong. However, you can provide a 3rd, a java.nio.file.CopyOptions parameter with a few options, the defaults can be found in java.nio.file.StandardCopyOption.
common case would look like this:
Files.copy(p1.toPath(), p2.toPath(), StandardCopyOption.REPLACE_EXISTING);

How can I add java client library in java on netbeans?

Hello i read that I have to use the java client library in java to get the revisions list in google drive using google api. I use netbeans. I search for this question and I try to add the bib but i haven't get success. To download the library i visited this link and downloaded the "latest". but after add the jar files I get the message error that the package doesnt exist. Please someone help me!
Yes! I try this but it show the same erros. I add the lib but netbeans (8) show the error in the three lines code: "package [name] doesn't exist".
The three lines are these:
import com.google.api.services.drive.Drive;
import com.google.api.services.drive.model.Revision;
import com.google.api.services.drive.model.RevisionList;
obs: the full code, taken in the google developers site is:
package javaapplication8;
import com.google.api.services.drive.Drive;
import com.google.api.services.drive.model.Revision;
import com.google.api.services.drive.model.RevisionList;
import java.io.IOException;
import java.util.List;
public class MyClass {
private static List<Revision> retrieveRevisions(Drive service,
String fileId) {
fileId = "1XpNdeTFBr2KygyfPtlowBvkpcaJJzjgLckrGjp5oOhg0";
try {
RevisionList revisions = service.revisions().list(fileId).execute();
return revisions.getItems();
} catch (IOException e) {
System.out.println("An error occurred: " + e);
}
return null;
}
}
If it is the error like package is missing, please check whether you have created a folder named com in your project or not. Also, every dot (.) indicates the levels of packages that you use in your project. Please check that com folder followed by the other folder names after every dot are present or not. I think this might help you. I answered your question based on my understanding (i.e, package issue which will be resolved by creating folders in your project. folders means packages). I am sorry if I gave you the wrong answer.

Categories

Resources