Eclipse Maven Project points throws File.not.Found error - java

Hi Guys I am having an File inside src/main/resources/CustomersDetails.txt but i am getting an java.io.FileNotFoundException. But the Url is not null and my if condition is true but my eclipse throws me the following error: Why it is looking on target and in my target folder i have only the jar and my file is also present on the target folder Any ideas?
java.io.FileNotFoundException: C:\Users\Balachander%20K\eclipse-workspace\JavaLearning\target\classes\CustomersDetails.txt (The system cannot find the path specified)
at java.io.FileInputStream.open0(Native Method)
at java.io.FileInputStream.open(FileInputStream.java:195)
at java.io.FileInputStream.<init>(FileInputStream.java:138)
at java.io.FileReader.<init>(FileReader.java:72)
at com.syncfusion.Persons.main(Persons.java:22)
My Program:
package com.syncfusion;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import org.apache.commons.io.FileUtils;
public class Persons {
public static void main(String[] args) {
Persons person = new Persons();
try {
File file = person.getFileNameFromResources("CustomersDetails.txt");
FileReader fileReader = new FileReader(file);
BufferedReader bufferedReader = new BufferedReader(fileReader);
String line=bufferedReader.readLine();
while(line!=null) {
System.out.println(line);
line=bufferedReader.readLine();
}
} catch (IOException e) {
e.printStackTrace();
}
}
private File getFileNameFromResources(String fileName) {
ClassLoader loader = getClass().getClassLoader();
URL url = loader.getResource(fileName);
if(url!=null) {
System.out.println("File Found");
return new File(url.getFile());
}
System.out.println("File Not Found");
return null;
}
}

I tried to duplicate your program with Maven and running it with Eclipse, I got it to work!
Did you import your Maven project correctly?
When I import the Maven project into Eclipse, it will generate the target folder and place it in the correct path (target\classes\CustomerDetails.txt).
My second guess is that you probably added the txt file after importing the project into eclipse? In this way, it might not get compiled correctly?
You can either delete the project in eclipse (don't delete contents on disk) and delete the "target" folder, then import it again.
Or you can compile your mvn from the terminal,
mvn compile
mvn package
go back to your eclipse (right click --> refresh your project).

Related

create .gitignore with java

I'm aware this question might be a duplicate in some sense but first hear me out.
I tried to create a code where i can create gitignore file with contents and for some reason i always end up having a file with txt extension and without name. Can someone explain this behavior and why?
Example Code:
System.out.println(fileDir+"\\"+".gitignore");
FileOutputStream outputStream = new FileOutputStream(fileDir+"\\"+".gitignore",false);
byte[] strToBytes = fileContent.getBytes();
outputStream.write(strToBytes);
outputStream.close();
You can use java.nio for it. See the following example:
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.List;
public class StackoverflowMain {
public static void main(String[] args) {
// create the values for a folder and the file name as Strings
String folder = "Y:\\our\\destination\\folder"; // <-- CHANGE THIS ONE TO YOUR FOLDER
String gitignore = ".gitignore";
// create Paths from the Strings, the gitignorePath is the full path for the file
Path folderPath = Paths.get(folder);
Path gitignorPath = folderPath.resolve(gitignore);
// create some content to be written to .gitignore
List<String> lines = new ArrayList<>();
lines.add("# folders to be ignored");
lines.add("**/logs");
lines.add("**/classpath");
try {
// write the file along with its content
Files.write(gitignorPath, lines);
} catch (IOException e) {
e.printStackTrace();
}
}
}
It creates the file on my Windows 10 machine without any problems. You need Java 7 or higher for it.

Not able read json file loacted in app's resource folder on azure

I have spring boot app. I am trying to read json file which is located in my resource folder (using class loader). I have deployed my app on azure its giving me error no such file exist and when i print path it is giving me null.
I tried to create a simple Maven project to fix your issue.
My source code structure is like below.
simpleMavenProj
|-src/main/java/Hello.java
|-src/main/resources/hello.json
The content of Hello.java is as below.
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.URL;
public class Hello {
public static void main(String[] args) throws IOException {
InputStream resourceInputStream = null;
URL resourceURL = Hello.class.getClassLoader().getResource("resources/hello.json");
if(resourceURL == null) {
System.out.println("Get the InputStream of hello.json in IDE");
resourceInputStream = new FileInputStream(Hello.class.getClassLoader().getResource("").getPath()+"../../src/main/resources/hello.json");
} else {
System.out.println("Get the InputStream of hello.json from runnable jar");
resourceInputStream = Hello.class.getClassLoader().getResourceAsStream("resources/hello.json");
}
System.out.println();
StringBuilder builder = new StringBuilder();
BufferedReader br = new BufferedReader(new InputStreamReader(resourceInputStream));
String line = null;
while((line = br.readLine()) != null) {
builder.append(line+"\n");
}
br.close();
System.out.println(builder.toString());
}
}
And hello.json:
{
"hello":"world"
}
If you are developing in an IDE, run the code and the result is:
Get the InputStream of hello.json in IDE
{
"hello":"world"
}
Else for generating a runable jar file, then to run the jar file via java -jar simpleMavenProj.jar and the result is:
Get the InputStream of hello.json from runnable jar
{
"hello":"world"
}
Hope it helps. Any concern, please feel free to let me know.

i get an error when i run this code

package demo;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.OutputStream;
import org.apache.poi.openxml4j.opc.*;
import org.apache.poi.xwpf.converter.pdf.PdfConverter;
import org.apache.poi.xwpf.converter.pdf.PdfOptions;
import org.apache.poi.xwpf.usermodel.XWPFDocument;
public class DocxToPdf {
public static void main(String[] args){
try
{
String inputFile = "F:\\MY WORK\\CollectionPractice\\WebContent\\APCR1.docx";
String outputFile = "F:\\MY WORK\\CollectionPractice\\WebContent\\APCR1.pdf";
System.out.println("inputFile:" + inputFile + ",outputFile:" + outputFile);
FileInputStream in = new FileInputStream(inputFile);
XWPFDocument document = new XWPFDocument(in);
File outFile = new File(outputFile);
OutputStream out = new FileOutputStream(outFile);
PdfOptions options = null;
PdfConverter.getInstance().convert(document, out, options);
} catch (Exception e) {
e.printStackTrace();
}
}
}
when i run this code an error occur like these and i have used following jar files also.
error:
java.lang.NoSuchMethodError: org.apache.poi.POIXMLDocumentPart.getPackageRelationship()Lorg/apache/poi/openxml4j/opc/PackageRelationship;
jars:
List of jar files
You likely have jar-versions of POI mixed up. The error indicates that the class that was loaded did not have a method that the calling class saw during compilation, so you have a different version of POI in your classpath.
See "Component Map" at https://poi.apache.org/overview.html for the different components that are included and which jars they end up, make sure you only have one of these jars in your classpath, not multiple different versions.

Put specific image in folder from resource

I need put one specific image from my project to specific folder in Java. Thanks for helping.
Edit:
Im creating a folder with File and the folder I'm creating i need to put an image i have in resources. Thanks for helping.
You can use NIO package in Java 7 with the class Files and the static method copy.
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.StandardCopyOption;
public class Main_copie {
public static void main(String[] args) {
Path source = Paths.get("data/image1.png");
Path destination = Paths.get("MyFolder/image1_copied.png");
try {
Files.copy(source, destination, StandardCopyOption.REPLACE_EXISTING);
} catch (IOException e) {
e.printStackTrace();
}
}
}
Do you mean to copy the file from 1 location to another? If so, then the approach would be to create a new File at the desired location, create a FileOutputStream and write everything from the original File (using an FileInputStream) to this OutputStream.
Maybe this post can help you out.

Cannot open file in java

I am trying to open a text file in java. I am using ubuntu 12.04. Following is my code:
package nlp;
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileReader;
import java.io.InputStreamReader;
public class sentence {
public static void main(String[] args) {
System.out.println("hello");
FileReader fr = new FileReader("test.txt");
BufferedReader br = new BufferedReader(fr);
String line;
while ((line = br.readLine()) != null) {
// process the line.
}
br.close();
}
}
I am using Eclipse for development. It says "FileNotFound". I have put the text file in .class as well as .java folder. Where am I going wrong?
The default execution directory in Eclipse is the root of the project folder. Put the file there or prefix the path with correct underlying folder structure.
you need to put that in try-catch because it throws IOException which is checked Exception.Put the code in try-catch or handle the exception using "public static void main(String[] args) throws IOException"
The file should be in root of the project folder as given below. Your code is working fine.
As Arnaud mentioned try this to find where Java is expecting the file:
System.out.println(new File(".").getAbsolutePath());

Categories

Resources