File cannot be opened because it does not exist - spring

In my java spring application, I used a data-set.sql file to populate data for testing
#RunWith(SpringJUnit4ClassRunner.class)
#SpringApplicationConfiguration(classes = ConsultationWebApplication.class)
#Sql(scripts="requests-dataset.sql")
#DirtiesContext(classMode= ClassMode.AFTER_EACH_TEST_METHOD)
#ActiveProfiles("test")
public class StatisticsTest {
//bla
//bla
//bla
}'
But when i push my code to the repository, the travis continuous integration
complains with:
org.springframework.jdbc.datasource.init.CannotReadScriptException: Cannot read SQL script from class path resource [ee/avok/consultation/service/requests-dataset.sql]; nested exception is java.io.FileNotFoundException: class path resource [ee/avok/consultation/service/requests-dataset.sql] cannot be opened because it does not exist
The point is that, the test runs and pass in the localhost, but it fails the CI
It seems that the CI cannot find the file,but the file is in the test package.

In my experience such errors occur when you are developing under Windows and the CI server works under Unix.
My guess is that you have named some folder within the path to your SQL file with an uppercase letter in Windows. Windows doesn't care for upper or lower case and finds the file in any case. Unix distinguishes between upper and lower case and thus does not find the file.
Search for an uppercase character somewhere in the path to your SQL file.

Related

Spring Boot Application - Running jar file gives ResourceFinderException error

Created a jar file for a spring boot multimodule application and ran the jar file using java -jar command. While starting the application, it gives ResourceFinderException. When I analyzed it, the issue is happening because in my ResourceConfig file, i have used the package for my api end points. If I use register(service.class), the application starts fine. Any suggestion how can I provide the package instead of using register? The reason I want to use package is because I have lots of services inside multiple packages and the code looks very ugly if i use register for all the services. The ResourceConfig file looks like below.
public class AppResourceConfig extends ResourceConfig {
public AppResourceConfig {}{
super();
property("jersery.config.beanValidation.enableOutputValidationErrorEntity.server");
**packages("com.api");**
register(GsonProvider.class);
register(RequestContextFilter.class);
register(NotFoundExceptionMapper.class);
register(DefaultExceptionMapper.class);
}
}
Here the issue is with highlighted line: packages("com.api")
If I comment out this code application will be up. Otherwise it is giving org.glassfish.jersey.server.internal.scanning.ResourceFinderException: java.io.FileNotFoundException: api-01.03.00.04-snapshot.jar (No such file or direcotry)
Note: api-01.03.00.04-snapshot.jar is the jar file for one of the module in a project

Spring boot: populate h2 db from schema in test/resources

On my local machine I load an in-memory h2 database to start my spring boot application in a safe environment, here's the properties:
spring.datasource.url: jdbc:h2:mem:DB_TEST;Mode=Oracle
spring.datasource.platform: h2
spring.jpa.hibernate.ddl-auto: none
spring.datasource.continue-on-error: false
spring.jpa.database-platform: org.hibernate.dialect.Oracle10gDialect
Then, in my src/main/resources I have the file schema-h2.sql containing my local db initiations.
That's fine, but then I also have some junit tests I want to execute:
#RunWith(SpringRunner.class)
#SpringBootTest
public class MyTest {
#Autowired
private MyController controller;
#Test
public void myTest(){
controller.doSomething();
}
This is also fine, as the schema-h2.sql is seen.
Anyway according to me it would be better to put the schema-h2.sql in src/test/resources as it has to be used only on my local environment. Doing so also allows maven to exclude it from the final build and that is also pretty fine.
Anyway if I put it there the test keeps working...but the main application breaks as the schema-h2.sql is not found!
How to modify the above properties to specify that the shema-h2.sql has to be searched inside of the test/resources folder?
Thanks
For normal mode, the properties file is put in src/main/resources,
and for the testing method, the properties file in the src/test/resources folder.
By Trying to run a test-class, eclipse runs EACH file ending with .sql (and thus containing a script to create tables or to insert data) it finds under src/main/resources and src/test/resources.
So if you put a script-file schema.sql (containing a script that creates a table: create table ..) in both folders, you'll get an "table already exits" error, if you let jut one, the test will run smoothly.
If you put a script-file (that insert data in a table) in both folder, both scripts will be run.
You can also use the #PropertySource("..") in your repository to tell spring where to find the properties-file to use.

spring-boot adding a custom command to actuatorshell in java

I am trying to add a java command to spring-actuator's ssh remote shell in a spring-boot applicaton. The spring-boot version is 1.2.3.RELEASE.
My sample command is just named 'kafka' and I tried placing it on the classpath in both /crash/commands as well as just /commands. It is never found - it doesn't show up in the help or actually work.
Is there some way to ask the remote shell to tell me what it's scanning/finding when it starts?
Things I have tried include specfically overriding shell.commandPathPatterns though the default seems like it should cover it.
My command - for testing - is very simple:
package commands;
#Usage("Kafka utility commands")
public class kafka extends BaseCommand {
#Command
public Object main(InvocationContext<ObjectName> context) {
return "it's all good";
}
}
After one hour of debugging I've found that the CRaSH remote shell looks for files with the extensions .groovy or .java within the packages are commands and crash.commands. The found files are compiled to bytecode, compiler errors are ignored.
I presume you use something like Maven. When you put your command into src/main/java then Maven will compile it as .class file and CRaSH will not find it. When you put your command into src/main/resources then Maven will not compile it and keep it as .java file instead.
The solution (which is quite odd for me) is to put your java command file into src/main/resources (package commands or crash.commands) so you have a .java source file in your target directory or JAR.
I tested it with spring-boot 1.2.1.RELEASE (crash.shell: 1.3.0) which should not be much different than 1.2.3.RELEASE (crash.shell: 1.3.1).

Junit file path

I am passing a filePath to JUnit as follows
public static void copyFile() throws IOException
{
String inputFileName = "myproj/src/test/resources/list.csv";
File file = new File(inputFileName);
File newFile = new File(new File("/tmp"), file.getName());
FileUtils.copyFile(file, newFile);
}
The package Structure is as follows.
I am running Test.java from IDE (IntelliJ) and it runs fine but when I do mvn clean install i get the below exception
java.io.FileNotFoundException: Source 'myproj/src/test/resources/list.csv' does not exist
at org.apache.commons.io.FileUtils.copyFile(FileUtils.java:1004)
at org.apache.commons.io.FileUtils.copyFile(FileUtils.java:968)
at ......copyFile(Test.java:21)
Can some one please let me know how am i suppose to give path name in the JUnit.
The exception is a strong hint to the problem... the file does not exist at that path.
In your IDE the working directory is set such that your relative path (myproj/src/test/resources/list.csv) resolves correctly.
The Maven task is running from a different working directory, presumably from myproj, so the file path cannot be resolved.
You have a couple of possibilities:
Use an absolute path
Load the resource from the classpath instead, using getResourceAsStream https://docs.oracle.com/javase/7/docs/api/java/lang/Class.html#getResourceAsStream(java.lang.String)
Build different relative paths based on the environment (this is a bad solution)

set class path in weblogic 11g console

How to set class path in weblogic 11g console for classes present in web-inf/classes/
And my classes are in the below structure which needs to be set as class path to avoid exception which needs these classes to be loaded first.
/com/cdy/ws/a.class files
/com/cdy/ws/b.class files
/com/cdy/ws/c.class files
Please help,
Thanks
If you are looking for WebLogic specific...
Lets say your web application dependent jars defined as:
WS_CLASSPATH=/com/cdy/ws
set in your environment variable.
That means your weblogic server can be loaded with weblogic.jar its relavent jars then, your WS_CLASSPATH can be added in the setDomainEnv.sh/cmc script find the EXT_PRE_CLASSPATH and assign to it.
EXT_PRE_CLASSPATH=$WS_CLASSPATH
There will be opposite side of CLASSPATH after the weblogic specific libraries available with EXT_POST_CLASSPATH in the setDomainEnv script.
The startup or shutdown class must be on the classpath of each server to which it is assigned.
To add a class to a server's classpath, do one of the following:
If you use a script to start a server instance, open the script in a text editor. In the command that sets the classpath, add the pathname of the directory that contains your class root package.
Then restart the server.
For example, you create a startup class named StartBrowser in a package named com.mycompany.startup. You archive the class file in a JAR file named c:\myDomain\src\myJAR.jar.
The start script for your server must add c:\myDomain\src\myJAR.jar to the server's classpath.
If you use the Node Manager to start a server instance, do the following on each server that runs the startup class or shutdown class:
In the left pane of the Console, expand Environment and select Servers.
On the Servers page, click on the server name.
Select Configuration > Remote Start.
In the Classpath field, enter the pathname for the classes that WebLogic Server requires to be on the classpath.
Use an absolute pathname or a pathname that is relative to the Node Manager's home directory.
Separate multiple classes with the type of separator that your operating system or shell requires.
For example, on Windows, use ; (semicolon) and in a BASH shell, use : (colon).
For example, weblogic.jar must be on the classpath. For a complete list, refer to Required Environment and Syntax for weblogic.Server.
In the Classpath field, add the pathname for your class or for a JAR file that contains your class.
For example, you create a startup class named StartBrowser in a package named com.mycompany.startup. You archive the class file in a JAR file named c:\myDomain\src\myJAR.jar. In this case, the Classpath field should contain the following value:
c:\Oracle\Middleware\wlserver_10.3\server\lib\weblogicsp.jar;c:\Oracle\Middleware\wlserver_10.3\server\lib\weblogic.jar;c\myDomain\src\myJAR.jar
step1)Class path can be set using ./setWLSEnv.sh(/wlserver_10.3/server/bin)
step2)Adding a jar file to class path abc.jar to this location(wlserver_10.3/server/lib) and follow step1
If in case you need to set Domain ./setWlEnv.sh(Domain_Home/bin) avil here

Resources