File missing when packaging to jar file - jar

I have a problem regarding exporting my Java project into a jar file.
I have this file (application.properties) which contains some database information and located under the project root directory.
There is no problem when running this project on Eclipse. But after exporting into a jar(Runnable Jar file) file, application.properties will not be included in the packaging and that cause the error.
Any suggestion how to fix this problem?

Do you want to have the application.properties reside outside the jar (for writing), or is it read-only? When outside you can have an initial template file in the jar, and copy it to some location.
//String applicationWorkingDir = System.getProperty("user.dir"); // The current constellation.
String userHomeDir = System.getProperty("user.home");
File myApplicationDataDir = new File(userHomeDir + "/.MyApplication");
myApplicationDataDir.mkdir();
File propertiesFile = new File(myApplicationDataDir, "application.properties");
Also using the Preferences API instead of a properties would be a solution.

Related

How to update a JAR file with a modfied Java file

I have one JAR named as abc.jar with 15 class files. In JAR file I need to modify xyz.class, so I decompiled to xyz.java and modified it.
How can I update that JAR with the modified Java file? While trying to create (compile) the JAR file I am getting errors.
You can add your newly compiled class to the jar and overwrite the old one, here is how to add xyz.class and xyz.java to the jar :
jar uf abc.jar xyz.class xyz.java
See here for more about updating a jar file.
Or simply you can use any archiving tool, like winrar to open and add files to the jar.
Compile the xyz.java and replace the xyz.class file directly in jar without building the jar again, if the single class has been modified.

File in executable jar cannot be found when running on AWS EC2

I have a .jar file executing on a aws ec2 instance which contains the following code:
List<String> lines = FileUtils.readLines(new File("googlebooks-eng-all-1gram-20120701-k"));
the file exists in projectname/res and also in /projectname directly. I included /res in the build path. Also I see that the file exists inside the jar file at the root if I export the .java file in eclipse.
If I run the jar localy on my pc it works fine. But if I run it on a ec2 instance it says:
java.io.FileNotFoundException: File 'googlebooks-eng-all-1gram-20120701-k' does not exist
How can that be?
On your PC it is reading from the actual file on the filesystem - that is what new File means - a file on the filesystem.
To access a resource in a jar file you need to call getResourceAsStream or something similar instead.

Qt QPixmap constructing with qstring filename

I need to initialize a qpixmap object with its file directory
It works if I do the following:
image = new QPixmap("C:/Users/Administrator/Desktop/maze/HTetris-build-Desktop_Qt_5_0_1_MinGW_32bit-Debug/untitled/c12.bmp");
and it works with as well:
image = new QPixmap("/Users/Administrator/Desktop/maze/HTetris-build-Desktop_Qt_5_0_1_MinGW_32bit-Debug/untitled/c12.bmp");
But this is just too long and too specific.
I have put c12.bmp in the same file as the project itself. But
image = new QPixmap("c12.bmp");
simply does not work.
Since this project needs to be portal, how can I do!!!!!!!
Thanks guys for help
Consider to put your file into resource file (qrc) or put your file near your executable file, because your "c12.bmp" is searched in runtime and must be in the same with executable directory.
You file must be in the file where your application is executed (runtime directory) that can be different than the directory of the executable.
You can use resource files for the portablity (your pixmap will be "embedded" in your binary).
http://qt-project.org/doc/qt-4.8/resources.html

How to give the path to run a jar file from a servlet?

I am facing a problem in running a jar file from my servlet.
I have to run a jar file of another java application with arguments in a separate sevlet in web project in Eclipse.
I use the code as
Runtime.getRuntime().exec("java -jar myproject.jar param1 param2 abc.xml");
In above code, on running the above jar file with parameters param1 and param2 , it will store output in abc.xml.
But the problem is that i dont know how to give the path of myproject.jar in code.
I copied myproject.jar to the project directory and it does not give any output.
I have tried to use
I dont know how to give path of jar file and where to put the jar file and where the out put file will come .
This code works fine with simple core java application where i put my myproject.jar file in the core java project directory and the output file abc.xml is created in the core java project directory.
Please guide me.
Thanks in advance.
This is not the right way of invoking Java code contained in some JAR in the classpath (I assume that you have placed it in /WEB-INF/lib). Just import and invoke the code contained in that JAR file directly.
import com.packagefromjar.SomeMainClassInJar;
// ...
SomeMainClassInJar.main(new String[] { "param1", "param2", "/path/to/abc.xml" });
Or if that API is well designed, then just use its entry classes directly instead of calling a main() method.

Why does my JAR file not create a serialization?

I created a jar file from a Java project. I can even run it now, but it does not save the serialization file that is stored correctly when I run the program from normal binary code outside of the JAR file.
Can’t you create new files when you have a jar file?
This is how I write the file:
FileOutputStream fileOut = new FileOutputStream("data/vocabulary.ser");
ObjectOutputStream out = new ObjectOutputStream(fileOut);
out.writeObject(this);
out.close();
fileOut.close();
My JAR file looks like this:
META-INF
MANIFEST.MF
name
stefankoch
…
where name/stefankoch/… is my project namespace.
I tried creating a data directory in the jar-file (root), but it did not work either. There also is a data directory within the directory the jar-file resides in, but that one is not taken either.
How can I allow jar files to create files?
basically, you should not add information entered by the user into a JAR file, they belong to the user’s home directory.
This can be read with
System.getProperty("user.home")
And files within the user home directory can also be used from within a JAR file.

Resources