My main class (which extends applet) is showing an image with absolute path, but I don't know how to make it get the path from the folder where my html is.
try {
tutorial2 = TextureLoader.getTexture("PNG", new FileInputStream(
new File("D:/eclipse/workspace/Final/res/tutorial2.png")));
} catch (IOException e) {
e.printStackTrace();
System.err.println("Nu s-a gasit imaginea");
}
But my html applet is in another dir, and I want when you move the html to still view the image.
Copy the image(tutorial2.png) and paste that image to the folder where you have saved the above class and then do this
tutorial2 = TextureLoader.getTexture("PNG", new FileInputStream(
new File("tutorial2.png")));
Related
Im trying to zip my created folder. Right now im testing localy and it create folder but after that i want to zip it.
This is my code for zip:
public static void pack(final String sourceDirPath, final String zipFilePath) throws IOException {
Path p = Files.createFile(Paths.get(zipFilePath));
try (ZipOutputStream zs = new ZipOutputStream(Files.newOutputStream(p))) {
Path pp = Paths.get(sourceDirPath);
Files.walk(pp).filter(path -> !Files.isDirectory(path)).forEach(path -> {
ZipEntry zipEntry = new ZipEntry(pp.relativize(path).toString());
try {
zs.putNextEntry(zipEntry);
Files.copy(path, zs);
zs.closeEntry();
} catch (IOException e) {
System.err.println(e);
}
});
} }
But im getting an error AccessDeniedException. Is there any option to zip created folder, i dont want to zip file because in that folder i will have subfolders, so i want to zip main folder. Any suggestion how can i achive that?
According to:
Getting "java.nio.file.AccessDeniedException" when trying to write to a folder
I think you should add the filename and the extension to your 'zipFilePath', for example: "C:\Users\XXXXX\Desktop\zippedFile.zip"
I have this structure in my project:
and my code is simply this:
public class ChapterTwo {
public static void main( String[] args )
{
try {
//File imageFile = new File("../../../../resources/lena.jpg");
String image = ChapterTwo.class.getResource("resources/lena.jpg").toExternalForm();
System.out.println(image);
//MBFImage image = ImageUtilities.readMBF(imageFile);
} catch (Exception e) {
e.printStackTrace();
}
}
}
Now this has been driving me crazy. how hard is it to make java locate an image in a simple directory structure?
I tried:
resources/lena.jpg
/resources/lena.jpg
../resources/lena.jpg
../../../../../resources/lena.jpg
nothing works. When I load the File and call exists() it always returns false. How do I load this image?
PS: My code is just testing code, but you get the idea, I was trying various stuff.
And it is com.foo not com
EDIT:
From the answers:
String imagePath = ChapterTwo.class.getClassLoader().getResource("lena.jpg").toExternalForm();
File imageFile = new File(imagePath);
System.out.println(imageFile.exists());
I get false ....
String image = ChapterTwo.class.getClassLoader().getResource("lena.jpg").getPath();
I am developing a class loader that will load plugins into my software. I have a jar file with two things in it, the package containing my code, and a text file containing the name of the class that I want to load from the jar. Is there a way for my app to read the text in the file and get the class name, then load the class with that name from the jar file?
This is the code that works:
content = new Scanner(new File("plugins/" + listOfFiles[i].getName().replaceAll(".jar", "") + "/" + "plugin.cfg")).useDelimiter("\\Z").next();
URL[] urls = null;
try {
File dir = new File("plugins/" + listOfFiles[i].getName());
URL url = dir.toURL();
urls = new URL[]{url};
} catch (MalformedURLException e) {
}
try {
ClassLoader cl = new URLClassLoader(urls);
Class cls = cl.loadClass(content.replaceAll("Main-Class:", ""));
Method enable = cls.getMethod("enable", (Class<?>[]) null);
enable.invoke(enable, null);
}catch (Exception e) {
System.out.println("One of the installed plugins might have an invalid plugin.cfg.");
}
The code reads a file with the name of the main class in it, and then loads that class from the jar file it extracted earlier.
I know what the problem is I just do not know how to fix it. So I have an image that I am trying to render in my program. I use ImageIO to load the image. But it seems to have a problem wit the path I am giving it. I am using NetBeans as my IDE and I dont know if I am saving the image file correctly.
First method:
public void init(){
BufferedImageLoader loader = new BufferedImageLoader();
try{
spriteSheet = loader.loadImage("/sprite_sheet.png");
}catch(IOException e){
e.printStackTrace();
}
SpriteSheet ss = new SpriteSheet(spriteSheet);
player = ss.grabImage(1,1,32,32);
}
the loader BufferedImageLoader class:
public class BufferedImageLoader {
private BufferedImage image;
public BufferedImage loadImage(String path) throws IOException{
image = ImageIO.read(getClass().getResource(path));
return image;
}
}
I have the image saved under a 'res' folder under 'src' folder.
Error:
Exception in thread "Thread-2" java.lang.IllegalArgumentException: input == null!
Thank you.
Why do you need to use getClass().getResource() ?
Most simple usage of ImageIO.read is as follows.
image = ImageIO.read(new File(path));
You may need to add folders to path also.
spriteSheet = loader.loadImage("/src/res/sprite_sheet.png");
Try using an absolute path for your file or if you need a relative check this post (eg assuming you have a res folder under default package did you try "/res/yourfile"
I have a cheerapp.mp3 in my /res/raw folder
so my code is
String filepath="/res/raw/cheerapp"; //or cheerapp.mp3
file = new File(filePath);
FileInputStream in = null;
try {
in = new FileInputStream( file );
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
The error I got file not found. why?
Use assets folder and ...
InputStream sound = getAssets().open("filename.mp3");
... or raw folder and ...
InputStream sound = getResources().openRawResource(R.raw.filename);
if it doesn't help you, then check this
Never can you access resource files by path!
Because they are compiled in APK file installed in Android. You can only access resource within application by access its generated resource id, from any activity (context):
InputStream cheerSound = this.getResources().openRawResource(R.raw.cheerapp);
or from view:
InputStream cheerSound = this.getContext().getResources().openRawResource(R.raw.cheerapp);
In your case, you should store sound files in external sd-card, then can access them by path. For e.g, you store your file in sounds folder on your sd-card:
FileInputStream inFile = new FileInputStream("/mnt/sdcard/sounds/cheerapp.mp3");
NOTE: path starts with '/' is absolute path, because '/' represents root in Unix-like OS (Unix, Linux, Android, ...)
Maybe you could use the AssetManager to manage your resources. For example:
AssetManager manager = this.getContext().getAssets();
InputStream open;
try {
open = manager.open(fileName);
BitmapFactory.Options options = new BitmapFactory.Options();
...
} catch (IOException e) {
e.printStackTrace();
}