Access files under a package in java - java

I have the package structure something like this
src
main
java
com
org
-- Loader.java
resources
schemas
-- a.schema
-- b. schema
Now I want to be able to access the list of schemas under the schema folder which could vary with time from the Loader class. I package it as a jar and in runtime access all the files under the schema folder. How can I do it?

Do this inside Loader.java:
InputStream in = Loader.class.getResourceAsStream("/data/schemas/a.schema");
The leading slash means from starting from the root of classpath (i.e. your JAR file).

Using java 8, you should get it easily. See below code:
import java.util.stream.*;
import java.nio.file.*;
import java.util.*;
import java.io.*;
public class HelloWorld{
public static void main(String []args) throws IOException {
try (Stream<Path> filePathStream = Files.walk(Paths.get("/data/schemas/"))) {
List<File> filesInFolder = filePathStream.filter(Files::isRegularFile)
.map(Path::toFile)
.collect(Collectors.toList());
System.out.println(filesInFolder);
}
}
}
here the try-with-resource constructs ensures that the stream is closed automatically after using.

Related

How to configure root for temp dirs in Java

We run code which does the standard for creating a temp directory:
import java.nio.file.Files;
And then:
tmp = Files.createTempDirectory("ourprefix-");
This, effectively, creates the directories under /tmp/ so that we get things like /tmp/ourprefix-1234 or similar.
Unfortunately, this base directory /tmp/ seems to be fixed and since on our build server lots of things tend to put their temp stuff there and because the partition the /tmp/ is on is rather small, this is a problem.
Is there a way to configure this facility from the outside (i. e. without changing the code)? I would have guessed that /tmp/ is a default and can be overridden by setting a special environment variable or (more Javaish) passing a special property to the compiler (e. g. -Djava.tmp.root=/path/to/my/larger/partition/tmp).
I tried using java.io.tmpdir but setting this did not have any effect; it seems to be the default in case nothing is given to createTempDirectory() but in our case the code passes a prefix.
Any idea how to achieve what I want without changing the source code?
EDIT
After some investigation I found that this works just fine:
import java.nio.file.Path;
import java.nio.file.Files;
import java.io.IOException;
public class TestTempDir {
public static void main(String[] args) throws IOException {
System.out.println(System.getProperty("java.io.tmpdir"));
Path path = Files.createTempDirectory("myprefix-");
System.out.println(path.toFile().getAbsolutePath());
}
}
Compile with javac TestTempDir.java, prepare with mkdir tmp and run with java -Djava.io.tmpdir=pwd/tmp TestTempDir this just works as expected:
/my/work/path/tmp
/my/work/path/tmp/myprefix-1525078348397347983
My issue rather seems to be one with Jenkins and its Maven plugin which does not pass the set properties along to the test cases :-/
if you pass the java.io.tmpdir property as a custom JVM property as you run the JVM, it should work.
Something like that :
java -Djava.io.tmpdir=myPath myClass
I tested and it works :
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
public class TestTempDir {
public static void main(String[] args) throws IOException {
System.out.println(System.getProperty("java.io.tmpdir"));
Path dir = Files.createTempDirectory("helloDir");
System.out.println(dir.toString());
}
}
$ java -Djava.io.tmpdir=D:\temp TestTempDir
D:\temp
D:\temp\helloDir5660384505531934395

Unit Testing with different behaviour on existance of a file

There are some cases that the software shall behave differently according to some environmental conditions, for example whether a file exists at some place or not.
In my case, I'm developing a library, and it is configured according to a configuration file in classpath, (and falls back to default behavior if the config file does not exists).
How shall I unit test this class?
I need to write tests for evaluating the class in following cases:
the file does not exists on classpath
the file with content A exist on classpath
the file with content B exist on classpath
But I don't know how to configure environment to justify all of them. And execute the test one after each other.
By the way I'm using Java, and I have both JUnit and TestNG on the test classpath.
Edit:
One of the problems is that the config file resides in classpath, so if the normal ClassLoader finds and loads it, it returns the same content as long as the same class loader is used.
And I believe using a custom ClassLoader for testing is so complicated, that it needs tests to validate the tests!
You can use a temporary file created by your test to mock out the path in your class.
ConfigurationTest.java:
import static org.hamcrest.Matchers.*;
import static org.junit.Assert.*;
import static org.junit.Assume.assumeThat;
import java.nio.file.Files;
import org.junit.Test;
public class ConfigurationTest {
private Configuration config = new Configuration();
#Test
public void testWithConfigFile() throws Exception {
config.configFile = Files.createTempFile("config_",".ini");
config.configFile.toFile().deleteOnExit();
assertFalse(config.isInDefaultMode());
}
#Test
public void testWithoutConfigFile() throws Exception {
assumeThat(Files.exists(config.configFile), is(false));
assertTrue(config.isInDefaultMode());
}
}
Configuration.java:
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
public class Configuration {
Path configFile = Paths.get("config.ini");
public boolean isInDefaultMode() {
return !Files.exists(configFile);
}
}

How to import a custom package into another? Getting error package "packagename" does not exist

So right now in my code I have this:
package pokemonbattle;
import pokemon.Pokemon.*;
public class PokemonBattle { //Do stuff }
and then I also have:
package pokemon;
public class Pokemon {
public static void main(String[] args) {}
public String getName(int pokemon) {
//stuff
}
Except that I'm getting the error "Package pokemon.Pokemon does not exist." When it does. The file name it's under is called Pokemon, but I've tried using different capitalization but nothing is working. Any suggestions? Thanks!
Packages are like directories and you should put your files into the place specified by their package. In each directory can either be files (classes, interfaces, ..) or other sub directories.
When you do
package pokemon;
public class Pokemon {
You express that this is the Pokemon.java file in the \pokemon directory. In other words \pokemon\Pokemon.java.
To use that class in another place you do either
import pokemon.Pokemon;
to import just that one class specifically or you do
import pokemon.*;
to import all the classes in that package (but not sub-packages thereof) When you say pokemon.Pokemon.* it's looking for a directory named \pokemon\Pokemon\ which doesn't exist.

Need to import custom package to my class

Hi I have a custom class which consists of a custom exception folder and 3 .java files.
My structure is com/raeside/family/
So I want to do the following
import com.raeside.family.*;
But it is just giving me a red underline and when I try to let netbeans repair it, it doesn't work. Where should I store my com/ folder so that I can import it into my java program? I tried storing it inside of projectname/src/ but it's to no avail.
package com.raeside.family;
public class W4Q3 {
public static void main(String[] args) {
System.out.println("hello");
}
}
Is my code, my W4Q3 file is in the family folder. Is there something I am missing?
If all of your classes are in the same package (e.g. com.raeside.family) then you don't need the import statement at all. Make sure that all of your java files are in the src/com/raeside/family directory and have
package com.raeside.family;
at the top.

import from external lib jython

I am trying to imported a java class from an external lib in jyhon and it does not work. An example
package run;
import import.Imported;
Class Run()
{
public static void main(String[] args){
pi = new PythonInterpreter(null);
pi.execfile('script.py');
}
}
//this is an external libary
package import;
Class Imported()
{
//some stuff;
}
//py script
from import import Imported //this line throws an error Module not found
#do some stuff
The strangest thing is that it runs when it is compiled in Eclipse, but does not from command line.
Any help?
Sounds like your classpath is probably set incorrectly at runtime. The easiest solution is typically just to add the directory or jar file containing 'import' to sys.path.
(Also, naming your packages 'import' is just asking for trouble.)

Categories