How to access files outside a .jar file netbeans project
Hi, i need to access a file outside a built project inside netbeans. Ideally i would like this file next to the .jar file as myapp.properties. If possible i would like it so that when i am compiling and running the project in netbeans the file can be with the class files or something along those likes. Either way it would be nice to have different code for built projects and just running them outside a jar.
Heres what i got so far
properties = new Properties();
InputStream in = this.getClass().getResourceAsStream("applications.properties");
properties.load(in);
in.close();this only works if the file is next to the classes. (think thats called the classpath ? )
Thanks for any replies!!
Your wrong code:
properties = new Properties();
InputStream in
= this.getClass().getResourceAsStream("applications.properties");
properties.load(in);
in.close();A right code:
// basic assumption 1: applications.properties
// is a pure ISO 8859-1 text file
// basic assumption 2: the file applications.properties
// is stored in the same directory where your
// jar file is stored
properties = new Properties();
try{
FileInputStream in // assume current dir
= new FileInputStream("applications.properties");
properties.load(in);
in.close();
catch (Exception e){
e.printStackTrace();
}
Similar Messages
-
How to access images in a jar file
Hello,
I am currently developping a swing application that uses icons (gif files) and I have zipped all my classes and images in a single .jar file.
When I try to access those files it doesn't work ... it seems that I am not using the right way ...
Could anyone explain me how to achieve this ?
thanks in advanceI get the Images from jar file to set ImageIcon for my components like this:
ImagesLocator.getImage("imageName.gif");
anu
//ImagesLocator.java
import java.net.URL;
import java.awt.*;
import javax.swing.*;
* Load images from correct place (directory or JAR file) into GUI.
public class ImagesLocator
* Load image from correct place (directory or JAR file).
* @param imageName name of image to be loaded (with no path)
* @return loaded image
public static ImageIcon getImage(String imageName)
ClassLoader cl = ImagesLocator.class.getClassLoader();
ImageIcon i = new ImageIcon(cl.getResource(imageName));
return i; -
How to read a file outside my .jar
Hello!
I have my j2me application in a .jar file , and my application needs to read a .txt file which is outside my .jar but in the same directory.
I tried to do :
InputStream input = getClass().getResourceAsStream("/myfile.txt");
as well as
InputStream input = getClass().getResourceAsStream("myfile.txt");
but nothing is working...
Could you help me, please??? I'm getting crazy with this :(
Thanks in advance!Thanks sabre150,
sabre150 wrote:
You cannot update a running jar file. I am sorry. I didn't know about it. Thanks to point out.
There are two ways I approach this depending on my exact requirements -
1) use the java.util.prefs.Preferences APII will try this one
>
2) if not already present I copy the preferences file from the jar to a known place. I use a directory in the user's home directory and normally make the directory name the program with a '.' prefix.
One cannot access files in a jar file using the File API. One needs to use the getResource() or getResourceAsStream() methods on Class or ClassLoader. -
How can I access EJB through executable jar file?
Hello buddies!!
I wonder if anyone could give me an explanation of how accessing EJB through executable jar file?Thanks!!Hi there,
You'll need to install the J2EE SDK on the client and include the <j2ee install dir>\lib\j2ee.jar file in your CLASSPATH along with the Client Jar file that was generated by the deploytool when you deployed the application.
In your client code the Context properties are as follows:
Properties p = new Properties();
p.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.cosnaming.CNCtxFactory");
p.put(Context.PROVIDER_URL, "iiop://hostname:portnumber");
Context cntx = new InitialContext(p).Hope this helps
Amanda
Developer Technical Support
Sun Microsystems
http://www.sun.com/developers -
Hi Folks,
I am new in Java programming, have a .jar file that contains .java files that I want no access and use from another .java file. These files in the .jar file basically form a library thus have no main-method. However I can not declare an instance of the library (in the .jar file) and use it within my program.
Plz help ...I have tried all I know in vain.
Thanks.temba wrote:
Hi Folks,
I am new in Java programming, have a .jar file that contains .java files that I want no access and use from another .java file. These files in the .jar file basically form a library thus have no main-method. However I can not declare an instance of the library (in the .jar file) and use it within my program. You are making little sense. You can't instantiate .java files.
Plz help ...I have tried all I know in vain.
Thanks.Could you post WHAT you have tried and also post what error messages you received? -
How to read a file inside the JAR file
Hi All,
I want to save some preferences in a file called "preferences". I kept the preferences file under my java package.
If i am running the code inside the NetBeans 6.9 IDE it's working file. But once i have created a JAR and try to run the application, it couldn't find the path.
Please help me that how to resolve this issue. I don't want to save this preferences file outside of my JAR (i.e) within my java package.
Here is the code,
package mypackage;
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.PrintWriter;
public class ReadFile
private void writeFile()
try
OutputStream out = new FileOutputStream(getClass().getClassLoader().getResource("mypackage/preferences").getPath().replace("%20", " "));
PrintWriter writer = new PrintWriter(out);
writer.println("Hello Java!");
writer.close();
out.close();
catch (Exception ex)
System.out.println(ex.getMessage());
private void readFile()
try
FileInputStream fi = new FileInputStream(getClass().getClassLoader().getResource("mypackage/preferences").getPath().replace("%20", " "));
BufferedReader br = new BufferedReader(new InputStreamReader(fi));
System.out.println(br.readLine().trim());
br.close();
fi.close();
catch (Exception ex)
System.out.println(ex.getMessage());
public static void main(String[] args)
ReadFile read = new ReadFile();
read.writeFile();
read.readFile();
}If i run the JAR, i get the following error message,
{color:#ff0000}*file:\my jar path\jarname.jar!\mypackage\preferences (The filename, directoryname, or volume label syntax is incorrect)*{color}Thanks sabre150,
sabre150 wrote:
You cannot update a running jar file. I am sorry. I didn't know about it. Thanks to point out.
There are two ways I approach this depending on my exact requirements -
1) use the java.util.prefs.Preferences APII will try this one
>
2) if not already present I copy the preferences file from the jar to a known place. I use a directory in the user's home directory and normally make the directory name the program with a '.' prefix.
One cannot access files in a jar file using the File API. One needs to use the getResource() or getResourceAsStream() methods on Class or ClassLoader. -
Java Files inside the jar file cannot be read or accessed through Eclpse
The Java File inside the jar file of the eclipse cannot be accessed through the Eclipse . It shows error for the modules in the jar file .
But when compiled with adding the jar files to the class path the compilation is successful .
How can i browse through the files in the jar like going into the function definition .
TIA ,
ImranOpen MPlayer OSX and show the playlist (command-shift-p) then drag the file into the playlist. Click the file in the playlist and click the "i"nfo button. What does it list for file, video and audio format?
Not all of the codecs used in avi and wmv files are available for OS X so I'm guessing your file happens to be using one that isn't... -
Dear Friends,
I have a jar file, which has executed classes and methods in it. I want to make use of these methods inside my springcontext piece of code.
Can someone please share an example of how to write spingcontext code which is accessing classes/methods from JAR files along with the any setup?
Thanks,I have found the answer... as described in:
http://java.sun.com/javase/6/docs/technotes/guides/lang/resources.html
the problem was that the properties are loaded with the getResource & getResourceAsStream methods and I didn't know that one. I thought that is was loaded through findClass because I saw the property files trying to be loaded through findClass.
The truth is that it tries to load with the getRessources methods and if it fails tries with the findClass/loadClass.
To Fix the problem, I have simply overriden the getRessourceAsStream to do my magic and that was it.
Thanks -
How to load xsl-files out of jar-files
Hi,
I have included some xsl-files into a jar-file. I can access this files with the following code-snipped if the jar-file is local on my harddisk:
ClassLoader.getSystemClassLoader().getResource("pcodeliste.xsl").openStream()
But the problem is that I use this jar-file as a part of a j2ee-application (it's a ear-file). After deploying this ear-file to my application-server (oc4j) I get a null-pointer exception if I call the xsl-file with the code above.
Can anybody help me how to change this code or how to change the j2ee-application to have access to this files??
Thank you very much
TobiasHi Rykk!
Thanks for the answer, but it does not work since the jar, containing my file is inside an ear.
Greets,
Jay -
How can I get a single jar file with NetBeans?
How can I get a single jar file with NetBeans?
When I create the project I get these files:
dist/lib/libreria1.jar
dist/lib/libreria2.jar
dist/software.jar
The libraries that have been imported to create the project are in separate folders:
libreria1/libreria1.jar
libreria2/libreria2.jar
libreria1, libreria2, dist folders are all located inside the project folder.
I added the following code to the build.xml:
<target name="-post-jar">
<jar jarfile="dist/software.jar">
<zipfileset src="${dist.jar}" excludes="META-INF/*" />
<zipfileset src="dist/lib/libreria1.jar" excludes="META-INF/*" />
<zipfileset src="dist/lib/libreria2.jar" excludes="META-INF/*" />
<manifest>
<attribute name="Main-Class" value="pacco.classeprincipale"/>
</manifest>
</jar>
</target>
Of course there is also the project folder:
src/pacco/classeprincipale.form
src/pacco/classeprincipale.java
Can you tell me what is wrong? The error message I get is as follows:
C:...\build.xml:75: Problem creating jar: archive is not a ZIP archive BUILD FAILED (total time: 2 seconds)This is not a NetBeans forum, it is a JDeveloper forum. You might want to try http://forums.netbeans.org/. I also saw your other question - try looking in the New to Java forum: New To Java
-
How to create and use library JAR files with command-line tools?
Development Tools -> General Questions:
I am trying to figure out how to put utility classes into JAR files and then compile and run applications against those JAR files using the command-line javac, jar, and java tools. I am using jdk1.7.0_17 on Debian GNU/Linux 6.0.7.
I have posted a simple example with one utility class, one console application class, and a Makefile:
http://holgerdanske.com/users/dpchrist/java/examples/jar-20130520-2134.tar.gz
Here is a console session:
2013-05-20 21:39:01 dpchrist@desktop ~/sandbox/java/jar
$ cat src/com/example/util/Hello.java
package com.example.util;
public class Hello {
public static void hello(String arg) {
System.out.println("hello, " + arg);
2013-05-20 21:39:12 dpchrist@desktop ~/sandbox/java/jar
$ cat src/com/example/hello/HelloConsole.java
package com.example.hello;
import static com.example.util.Hello.hello;
public class HelloConsole {
public static void main(String [] args) {
hello("world!");
2013-05-20 21:39:21 dpchrist@desktop ~/sandbox/java/jar
$ make
rm -f hello
find . -name '*.class' -delete
javac src/com/example/util/Hello.java
javac -cp src src/com/example/hello/HelloConsole.java
echo "java -cp src com.example.hello.HelloConsole" > hello
chmod +x hello
2013-05-20 21:39:28 dpchrist@desktop ~/sandbox/java/jar
$ ./hello
hello, world!I believe I am looking for:
1. Command-line invocation of "jar" to put the utility class bytecode file (Hello.class) into a JAR?
2. Command-line invocation of "javac" to compile the application (HelloConsole.java) against the JAR file?
3. Command-line invocation of "java" to run the application (HelloConsole.class) against the JAR file?
I already know how t compile the utility class file.
Any suggestions?
TIA,
DavidI finally figured it out:
1. All name spaces must match -- identifiers, packages, file system, JAR contents, etc..
2. Tools must be invoked from specific working directories with specific option arguments, all according to the project name space.
My key discovery was that if the code says
import com.example.util.Hello;then the JAR must contain
com/example/util/Hello.classand I must invoke the compiler and interpreter with an -classpath argument that is the full path to the JAR file
-classpath ext/com/example/util.jarThe code is here:
http://holgerdanske.com/users/dpchrist/java/examples/jar-20130525-1301.tar.gz
Here is a console session that demonstrates building and running the code two ways:
1. Compiling the utility class into bytecode, compiling the application class against the utility bytecode, and running the application bytecode against the utility bytecode.
2. Putting the (previously compiled) utility bytecode into a JAR and running the application bytecode against the JAR. (Note that recompiling the application against the JAR was unnecessary.)
(If you don't know Make, understand that the working directory is reset to the initial working directory prior to each and every command issued by Make):
2013-05-25 14:02:47 dpchrist@desktop ~/sandbox/java/jar
$ cat apps/com/example/hello/Console.java
package com.example.hello;
import com.example.util.Hello;
public class Console {
public static void main(String [] args) {
Hello.hello("world!");
2013-05-25 14:02:55 dpchrist@desktop ~/sandbox/java/jar
$ cat libs/com/example/util/Hello.java
package com.example.util;
public class Hello {
public static void hello(String arg) {
System.out.println("hello, " + arg);
2013-05-25 14:03:03 dpchrist@desktop ~/sandbox/java/jar
$ make
rm -rf bin ext obj
mkdir obj
cd libs; javac -d ../obj com/example/util/Hello.java
mkdir bin
cd apps; javac -d ../bin -cp ../obj com/example/hello/Console.java
cd bin; java -cp .:../obj com.example.hello.Console
hello, world!
mkdir -p ext/com/example
cd obj; jar cvf ../ext/com/example/util.jar com/example/util/Hello.class
added manifest
adding: com/example/util/Hello.class(in = 566) (out= 357)(deflated 36%)
cd bin; java -cp .:../ext/com/example/util.jar com.example.hello.Console
hello, world!
2013-05-25 14:03:11 dpchrist@desktop ~/sandbox/java/jar
$ tree -I CVS .
|-- Makefile
|-- apps
| `-- com
| `-- example
| `-- hello
| `-- Console.java
|-- bin
| `-- com
| `-- example
| `-- hello
| `-- Console.class
|-- ext
| `-- com
| `-- example
| `-- util.jar
|-- libs
| `-- com
| `-- example
| `-- util
| `-- Hello.java
`-- obj
`-- com
`-- example
`-- util
`-- Hello.class
19 directories, 6 filesHTH,
David -
How to handle 2 or more .jar files with an applet
Hey out there
I have created an ftpClient application that uses "jakarta ftpClient". It works fine as an JFrame application � But when I converted the Application into an JApplet I get the following Exception:
java.lang.NoClassDefFoundError: org/apache/commons/net/ftp/FTPClient
I have bundled the main application into a .jar file (Application,jar). But I don't know how to handle the 2 jakarta .jar files with my JApplet??
I Tried to append the 2 jakarta .jar files to the Application,jar with the following code:
jar cvf Application.jar 1.class 2.class�. commons-net-1.4.1.jar jakarta-oro-2.0.8.jar
But with the same result / Exception (I have signed the Jar file!)
Can anyone help meHi i have a question with your application can you down- or upload more files at the same time? Because i'm having problems with my ftp application.
Here is the link with my problem maybe you can help me. I will be very pleased when you can help me.
http://forum.java.sun.com/thread.jspa?threadID=5162042&tstart=0
Thx
Satanduvel -
How to install a Application in *.jar file format?
How to install a Application in *.jar file format?
I have taken the *.jar file into the device into media folder. but device is not recognizing the file format
could some one plz provide some suggestion to proceed with this?
Thanks
Mohamed JaveedI'm having the same problem. I've put .jar into the 'system' folder but that doesn't seem to make the program work neverless see it on my device. Help.
-
How to read a text file from a Jar file
Hi
How can I read a text file from a Jar file?
Thanx in advance..thanx
helloWorld it works.damn, I didn't remove it fast enough. Even if it is urgent, it is best not to mention it, telling people just makes them take longer. -
How can I include my database file into my jar file?
Hi,
I am doing several tests for the jar command....
I tried to put the database file into my jar file, but when the program run, it just couldn't find the database file, so how may I include the database file into the jar file? Not only that, it also can't find the policy file etc.sorry, I think I found the solution, I should have had read the API more carefully.
Maybe you are looking for
-
Lots of Permissions Need Repairing?
I'm fairly certain this is normal, but figured I'd run this by the world... Is it normal to have alllllllll these damaged permissions? Repairing permissions for “Macintosh HD” Permissions differ on "System/Library/Java/JavaVirtualMachines/1.6.0.jdk/
-
Netra t1 with solaris 9 fresh install on/standby switch
Hi, I recently installed solaris 9 onto my Netra 105 box. For the likes off me I can't figure out why the on/standby switch simply does not work since reinstalling. The only way I can power down the box is by literally unplugging it from the wall. I
-
2 fields with text on the same line
Hi everyone, Could anyone let me know how I can get 2 fields with their text values in the same line. For eg: weight ______ unit of weight _____ i need both of the fields and text to be visible. Thanks/ Message was edited by: Raj Kali
-
In "my Documents" my back/forward buttons are grey, & inactive. Joe
when I open "My Documents" the back/forward buttons are are "grey" & non-responsive, how can I retreive them? Joe
-
How can I make it so my Macbook will receive iMessages every time my iPhone does?
Same thing goes for when i update my notes on my phone.