Export to JAR with external JavaPOS Files - java

I've currently finished my project, but can't get it to work when it is exported. I use JAXB to read and write XML Files and also have dependencies on other external Folders, which are needed to use a POS-Printer.
I've managed to link my external XML Save-Files with absolute paths, but not with relative paths. So that worked, although not the way i wanted. Yet, using the external class folder for the printer didn't work at all.
This means, that in my Eclipse Project Build Path i've added a class folder, which contains all of these needed files (which are not only jars, so adding them one by one wouldnt work). So exporting my project to a jar either includes all the files into the jar itself, or doesnt include them at all.
Everything works perfectly in Eclipse, but not when i export it.
My folder structure looks like this:
src
/model
/view
/control
data
/articles.xml
/...
JavaPOS <--- needed folder with all its files
/jpos.xml
/xerxers.jar
/swt-..-.dll
I've tried:
InputStreams is = getClass().getResourceAsStream(url);
absolute paths
manipulating the manifest file and/or jar structure
runnable and non runnable jars with nearly every combination of options
putting the files inside the library "by hand"
changing the build path of the project
My Question is:
How do i get my jar-file to know where these files are?
EDIT:
Do you think Maven or an Ant file could solve my problems? I don't have any experience with those.

The Problem was, that i had more than one JRE installed and that the one eclipse was using, had all the dll files, but the other ones didnt have it. So i had to add them manually, because reinstalling the drivers of the printer didnt change anything. Gotta fix that somehow, but right now it works and that is all i wanted.
Turns out i didn't even need that Folder, just needed one file out of it and the missing dlls.

Related

BlueJ Importing custom class library

Ok the deal is, I've made a small class library (*.Jar file) to help make a more visually organized code and to help with some common tasks.
Before you tell me to go to: Tools > Preferences > Libraries.
I have done that, over and over and over again.. I also tried to manually add it to: C:\Program Files\BlueJ\lib\userlib\"jar file here"
And yes, i did restart BlueJ to load the newly added libraries.
No matter what i seem to try it won't let me import it to my project using the import command, but without it, it just won't compile.
I know i can just add the classes manually, but as the class library slowly grow, so does the effort of adding all the classes in that library.
In advance, thanks for your help and consideration.
If adding your jar file in the Preferences > Libraries tab doesn't work, you can try adding the jar to the "userlib" directory at <bluej-dir>/lib/userlib (where <bluej-dir> is the folder where BlueJ was installed).
A third option you can try is adding your jar file directly to a project by creating a +libs directory inside your project directory and copying the jar to that directory. This has the drawback that you have to manually copy the jar to each project +libs subfolder that needs your library. On the plus side, it allows you to distribute your project (library jars included) just by zipping up the root project directory.
On Windows I create directory named "+libs" on my blueJ project directory,
then add .jar to that directory, and restart blueJ ,
that´s help me, and import after that work fine
Despite the post's oldness for who stumble upon i can say with the newest version of BlueJ (actually 4.1.3) in an old Ubuntu distro(Lucid Lynx aka 10.04LTS) all work as intended per default without any hack

getResourceAsStream works in Eclipse IDE but not in JAR format

This is a really common error, because there are tons of threads about it, but I'm not sure if since my situation is slightly different from all of them the solutions don't work?
Basically, I'm in eclipse. I have a source folder called src, then I have a package that goes down three folders, then the class in question. The class uses the code:
BufferedImage im = ImageIO.read(Thread.currentThread().getContextClassLoader().getResourceAsStream(filenames[x].concat(extension)));
surrounded by a try/catch. filenames is an array of all the file names I am loading (this code is run multiple times in a for loop) and extension is ".PNG". The pictures are located in another source folder called EngineTextures.
Running this program works fine in Eclipse! All textures are loaded and all my other code runs! However, I export it as a runnable jar and run it in command prompt to recieve input == null errors on all of them, pointing to the line that has ImageIO.read(Thread.currentThread() in it.
The kicker is this whole thing worked in a separate project before, and when I even tried re-exporting that project, I recieve the same errors on completely unchanged code. This leads me to believe I have some obscure Eclipse setting changed wrongly.
Opening the jar, my MANIFEST.MF has a version of 1.0 and a classpath of just plain ., which I thought was correct for this kind of thing? The Main-Class points to the right place, and all my pictures are right there next to the META-INF folder.
Solutions I've looked at unsuccessfully:
getResourceAsStream working in eclipse, but not when run as applet in browser
Why does getResourceAsStream() work in the IDE but not the JAR?
Java IDE - Eclipse, Importing resources
Audio file in jar made by Eclipse IDE
getResourceAsStream() returning null in jar but fine in eclipse
Additionally, I completely deleted the workspace and recopied my pictures and code into the same state, thinking maybe some .metadata thing was wrong, to no avail.
Thank you, in advance, for any and all help. I hate to make a repeat like this but no solutions have worked thus far. Please let me know if I have not given any crucial information.
Opening the jar, my MANIFEST.MF has a version of 1.0 and a classpath of just plain ., which I thought was correct for this kind of thing?
No. The Class-Path entry in a JAR file names other JAR files, relative to the location of this jar file. It doesn't name directories:
"The value of this attribute specifies the relative URLs of the extensions or libraries that this application or extension needs."
That in turn implies that resources to be loaded via getResourceAsStream() must be in JAR files.

Having trouble compiling and recreating a JAR file using Eclipse

I have a Java JAR file that was developed to run in the browser. It works fine. There doesn't appear to be any sort of build file associated with the source code I've been given and I'm attempting to create a project and/or build script.
The source code references some third party code. What I've done is unzipped the JAR file into a folder that has this structure:
\App\src\com
\App\src\META-INF
\App\src\applet
The "com" folder contains subfolders which contain the .CLASS files for the third party libraries. "applet" contains the .JAVA source code files (one folder, maybe 15 files).
The code appears to be written in 1.4 syntax (I manually attempted to compile some of the .JAVA files and it complained that some things were deprecated and/or required the older version).
I created a Java Project in Eclipse and pointed it to the \App folder. Then I went into the Properties for the project and went to Libraries and and clicked on "Add Class Folder" and added the \App\src\com folder. Then I went to "Order and Export" and moved this to the top. I also set the Java Compiler version to 1.4.
I am getting a large number of compiler errors, all of which seem to stem from the fact that it says it can't resolve an import. This import is the set of .CLASS files contained in the "com" subfolders. The namespace matches the directory structure.
This seems like it should be really straightforward, but I've tried various things and don't seem to be making any progress. Based on what I've read I'm sure it has something to do with the pathing but I'm at a loss at this point on how to fix it.
(I'm sure it goes without saying that I'm not a Java dev.)
Any thoughts or ideas as to where I'm going wrong?
Can you please try to right click on Eclipse's Navigator. Select Import>General>archive file> (Jar file that you want to import).
You should be getting the jar file correctly imported at this point. Do tell me if you face any problems.

Export a java library in a .jar file

I'm still pretty new to java and I'm VERY new to exporting .jar files. I've got a little game that I want to send to some friends and I was told in another question that I should export it to an executable jar file. Well I finally got that working on my computer but when I send it to other people it doesn't work because they don't have the library.
I'm importing the objectdraw library and without that my program won't run at all!
So basically I need to find a way to export the object draw library as part of my .jar file so that they can use it too. Do I simply include it in the included files part of the jar command?
ex: jar cmf MANIFEST.mf Archery.jar * /System/Library/Java/Extensions/objectdraw.jar
or what? I'm working out of the command line right now.
The simplest way is to send the JAR library file too and add a Class-Path entry to the manifest. This entry would look like:
Class-Path: objectdraw.jar
You could also set the CLASSPATH environment variable manually.
Alternatively, you can unpack the library and add all (or just the required files) to your final jar. This doesn't always work though, because some libraries rely on the integrity of teir JAR file.
Finally, it is possible to include the dependency in the main JAR, but it would require a custom class loader.
Turns out the best way I've found to do this is to unpack the library and then put all the resulting files in with your final archive. This way it actually works on other computers.
jar xf library_wanted.jar; jar cvmf MANIFEST.mf end_result.jar *.class library_wanted/

How to use jardesc in Eclipse?

I want to create an executable JAR-file from my eclipse project. It references other projects, which also reference other projects (and so on) and some JAR files.
According to this answer, everything should go "within 2 clicks". But not for me:
It reports a warning:
Problem writing mg/build/classes/META-INF/MANIFEST.MF to JAR: duplicate entry: META-INF/MANIFEST.MF duplicate entry: META-INF/MANIFEST.MF
It looks like it tries to include existing manifests from all projects, which simply doesn't make sense. I actually do not want to include any of them, just to generate a new one. I haven't found any way to switch it off. OK, it's just a warning.
It looks like I need to manually take care of all the referenced jar files... actually, Eclipse knows them, I do not.
I need to either include the content of all the referenced jar files or to copy all of them into the target folder and list them in the Manifest. I've got no idea how to do it.
I also wonder if the jardesc file is usable from ant build scripts.
I got the same error message ("duplicate entry") when, in my case
I checked the option to include an existing manifest file.
AND I specified to include MANIFEST.MF among the files to be included in the .jar.
Once I excluded MANIFEST.MF in the "Select the resources to export:" file tree of the jar generation wizard, the warning disappeared.
As for including referenced projects with your jar, I recommend making them into jar files as well, and including those in the project you want to make a jar of.
Then, make sure to set the manifest's classpath correctly.

Categories