I am working on an introduction to spring course, but I have an issue with my SpringConfig.xml file path. As show on the picture, I just added it everywhere as a hailmary but that's not working either:
Exception in thread "main" org.springframework.beans.factory.BeanDefinitionStoreException: IOException parsing XML document from class path resource [SpringConfig]; nested exception is java.io.FileNotFoundException: class path resource [SpringConfig] cannot be opened because it does not exist
My path:
Main method code:
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("SpringConfig");
}
please don't forget to add the file extension ".xml", spring will read it from src/main/resource
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("SpringConfig.xml");
}
Related
When I try to load the xls file it doesn't work even though is in the same folder. I also tried it with absolute paths.
(It all happened because of missing jars, here is the list with all of them. To solve the issue of relative paths the url.getResource() from below works fine.)
My Jar List(Image)
public class Main {
public static void main(String[] args) throws FileNotFoundException, IOException {
FileInputStream f = new FileInputStream("MP.xls");
HSSFWorkbook l = new HSSFWorkbook(f);
}
}
public class Main {
public static void main(String[] args) throws FileNotFoundException, IOException {
FileInputStream f = new FileInputStream("C:\\Users\\alumno.Alumno-PC\\Documents\\NetBeansProjects\\pruebas xlsx\\src\\pruebasxlsx\\MP.xls");
HSSFWorkbook l = new HSSFWorkbook(f);
}
}
This is where the file is located(picture)
This is the error with relative path:
Exception in thread "main" java.io.FileNotFoundException: MP.xls (El sistema no puede encontrar el archivo especificado)
at java.base/java.io.FileInputStream.open0(Native Method)
at java.base/java.io.FileInputStream.open(FileInputStream.java:216)
at java.base/java.io.FileInputStream.<init>(FileInputStream.java:157)
at java.base/java.io.FileInputStream.<init>(FileInputStream.java:111)
at pruebasxlsx.Main.main(Main.java:22)
this is the error with an absolute path
Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/commons/math3/util/ArithmeticUtils
at org.apache.poi.poifs.property.RootProperty.setSize(RootProperty.java:59)
at org.apache.poi.poifs.property.DirectoryProperty.<init>(DirectoryProperty.java:52)
at org.apache.poi.poifs.property.RootProperty.<init>(RootProperty.java:31)
at org.apache.poi.poifs.property.PropertyTable.<init>(PropertyTable.java:58)
at org.apache.poi.poifs.filesystem.POIFSFileSystem.<init>(POIFSFileSystem.java:99)
at org.apache.poi.poifs.filesystem.POIFSFileSystem.<init>(POIFSFileSystem.java:272)
at org.apache.poi.hssf.usermodel.HSSFWorkbook.<init>(HSSFWorkbook.java:399)
at org.apache.poi.hssf.usermodel.HSSFWorkbook.<init>(HSSFWorkbook.java:381)
at pruebasxlsx.Main.main(Main.java:24)
Caused by: java.lang.ClassNotFoundException: org.apache.commons.math3.util.ArithmeticUtils
at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(BuiltinClassLoader.java:641)
at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(ClassLoaders.java:188)
at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:520)
... 9 more
The FileInputStream with the absolute path works. Just the reading with HSSFWorkbook- misses a library.
The relative path is a volatile option. If the file is a read-only source, bundle it with the application, and do not use a File (file system file), but use a resource, a "file" on the class path, possibly bundled in an application jar.
InputStream f = Main.class.getResourceAsStream("/pruebasxslx/MP.xls");
HSSFWorkbook l = new HSSFWorkbook(f);
For HSSFWorkbook a library with org.apache.commons.math3 classes is not found,
an indirect needed library.
As these library dependencies are extra work, already done by others, and involving moving coherent versions of all libraries to work together, one should better use for instance maven, a build infrastructure. Maven (or gradle) projects are supported by most IDEs, and have a bit different directory structure.
File input stream gets the file named by the path name in the file system - that means an absolute path, as parameter, not relative path.
If you want to load a file from the location of your class use:
URL url = class.getResource("file.txt");
and then get absolute path of it by using:
url.getPath();
So, below solution should work (it can be polished but you will get an idea):
public class Main {
static URL url = Main.class.getResource("MP.xls");
public static void main(String[] args) throws FileNotFoundException, IOException {
FileInputStream f = new FileInputStream(url.getPath());
HSSFWorkbook l = new HSSFWorkbook(f);
}
}
I'm trying to write an object to json file by jackson.
If i provide an absolute path "D:/Projects/quiz-red/src/main/resources/com/models/Quizzes.json" it's working and file appears in the directory
But if i provide a relative path - "/com/models/Quizzes.json" i'm just getting Process finished with exit code 0 in console and nothing happens. What am I doing wrong?
There is my code:
public static void writeEntityToJson(Object jsonDataObject, String path) throws IOException {
ObjectWriter writer = mapper.writer(new DefaultPrettyPrinter());
writer.writeValue(new File(path), jsonDataObject);
}
public static void main(String[] args) throws IOException {
Quiz quiz = new Quiz(5L, "Title", "Short desc");
writeEntityToJson(quiz, "/com/models/Quizzes2.json");
}
I want to save a file to resources from DataProvider using relative path
Exception:
Exception in thread "main" java.io.FileNotFoundException: com\models\Quizzes5.json (The system cannot find the path specified)
at java.base/java.io.FileOutputStream.open0(Native Method)
at java.base/java.io.FileOutputStream.open(FileOutputStream.java:298)
at java.base/java.io.FileOutputStream.<init>(FileOutputStream.java:237)
at java.base/java.io.FileOutputStream.<init>(FileOutputStream.java:187)
at com.fasterxml.jackson.core.JsonFactory.createGenerator(JsonFactory.java:1223)
at com.fasterxml.jackson.databind.ObjectWriter.writeValue(ObjectWriter.java:942)
at com.utils.DataProvider.writeEntityToJson(DataProvider.java:33)
at com.utils.DataProvider.main(DataProvider.java:50)
Getting error while running spring program. My program is like below:
And my program to read the xml file :
public static void main(String[] args) {
//String springConfig = "src/main/resources/config/FacebookSimulator.xml";
//String springConfig = "/root/fbdata/FacebookSimulator.xml";
String springConfig = "/config/FacebookSimulator.xml";
//ApplicationContext context = new FileSystemXmlApplicationContext(springConfig);
ApplicationContext context = new ClassPathXmlApplicationContext(springConfig);
JobLauncher jobLauncher = (JobLauncher) context.getBean("jobLauncher");
Job job = (Job) context.getBean("simulatorJob");
This works perfectly in windows.But while running in linux it is showing error like below:
org.springframework.beans.factory.BeanDefinitionStoreException: Unexpected exception parsing XML document from class path resource [config/FaceBookSimulator.xml]; nested exception is org.springframework.beans.FatalBeanException: Class [org.springframework.batch.core.configuration.xml.CoreNamespaceHandler] for namespace [http://www.springframework.org/schema/batch] does not implement the [org.springframework.beans.factory.xml.NamespaceHandler] interface
at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.doLoadBeanDefinitions(XmlBeanDefinitionReader.java:414)
at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:336)
My question is where exactly should I place facebook.xml in linux system.
i made this as a jar and running in linux
With maven I'm building an application which has to load a driver dynamically. With the following code it only works if the driver.so is positioned inside the resulting JAR file. What can I do that the file can be found outside of the JAR within the path ./natives/driver.so.
package com.myproject;
public class Starter {
public static void main(String[] args) {
File classpathRoot = new File(Starter.class.getClassLoader().getResource("driver.so").getPath());
System.out.println(classpathRoot);
}
}
Output when driver is positioned inside JAR is:
jar:file:/home/ted/java/myproject/target/myproject-0.1-SNAPSHOT.jar!/libgdx64.so
Output when positioned outside JAR (in target as well as in target/natives directory) is:
null
I start the application via:
cd /home/ted/java/myproject/target/
java -Djava.library.path=./natives -cp myproject-0.1-SNAPSHOT.jar com.myproject.Starter
What can I do?
Try this:
package com.myproject;
public class Starter {
public static void main(String[] args) {
File file = new File("natives/driver.so");
System.out.println(file);
}
}
or this:
package com.myproject;
public class Starter {
public static void main(String[] args) {
File file = new File(System.getProperty("java.library.path"), "driver.so");
System.out.println(file);
}
}
I have a dropwizard project and I have maintained a config.yml file at the ROOT of the project (basically at the same level as pom.xml). Here I have specified the HTTP port to be used as follows:
http:
port:9090
adminPort:9091
I have the following code in my TestService.java file
public class TestService extends Service<TestConfiguration> {
#Override
public void initialize(Bootstrap<TestConfiguration> bootstrap) {
bootstrap.setName("test");
}
#Override
public void run(TestConfiguration config, Environment env) throws Exception {
// initialize some resources here..
}
public static void main(String[] args) throws Exception {
new TestService().run(new String[] { "server" });
}
}
I expect the config.yml file to be used to determine the HTTP port. However the app always seems to start with the default ports 8080 and 8081. Also note that I am running this from eclipse.
Any insights as to what am I doing wrong here ?
Try running your service as follows:
Rewrite your main method into:
public static void main(String[] args) throws Exception {
new TestService().run(args);
}
Then in eclipse go to Run --> Run configurations...., create a new run configuration for your class, go to arguments and add "server path/to/config.yml" in "program arguments". If you put it in the root directory, it would be "server config.yml"
I believe you are not passing the location/name of the .yml file and thus your configurations are not being loaded. Another solution is to just add the location of your config file to the array you're passing into run ;)