Distributable java files only work in dev machine - java

I created a Java application in NetBeans. That Java application contains a GUI and some external Jars.
In the dist folder I can see a Jar file and a folder called lib where are the jars I use in the project. If I execute the Jar file the application works as expected. If I change the name of the lib folder the application does not work (meaning that the Jar is using the correct files). But when I copy the dist folder to a different machine (with the same Java version) the application does not work as expected. The user interface is shown but the functionalities does not work (the functionalities are in the external Jars i mentioned in the beginning). Can anyone help?
EDIT:
I checked the class-path in the MANIFEST file and everything is ok.

Related

Running an external program from an executable Jar

I have created a simple SWING gui for a cmd program someone else developed. To run this program I execute this line:
Process convertProcess = Runtime.getRuntime().exec("jlyt\\prog\\com_win\\jlyt.bat " + selectedFiles.getName());
The jlyt folder is in the same folder as my src folder (I am using IntelliJ).
When running via IDE everything works great, but not when I run the jar I created. I have tried running it from the directory it was saved to by IntelliJ as well as from the directory of the jlyt folder.
I did not add the external program (inside the jlyt folder) to my jar since it is very heavy. I want my jar to be distributed along side the original program and not to contain everything.
Any idea how I should build my jar?
Thanks.
I see why you put /jlyt in /src, it serves as a convenience within the IDE. /jlyt will get copied as a resource into /bin/classes, or whatever the IDE target is, and that allows everything to work from the IDE.
When you JAR your application, /jlyt is typically added to the JAR; however, it is not accessible to Windows. I assume you are placing a copy of /jlyt in the same folder as the JAR when you attempt to run.
As a first step, in a terminal, set the current directory to the folder containing the JAR and /jlyt. Since you are specifying a relative path in your exec(), that should be sufficient for everything to run.
You can also try creating a shortcut to the JAR, since it is executable, and set the working directory to the folder containing the JAR.
You have to use a relative path based on the place where jlyt.bat is according to your jar file.
e.g. use "./" or "../" to navigate the directory tree up.
The location of the JAR file is only relevant for starting the JAR. The working directory must be the directory that contains the jlyt folder, since you are using 'jlyt\...' as path to the executable.
Example, lets suppose following directory structure:
somewhere
project
gui
appl.jar
jlyt
prog
...
working directory must be 'project' and the JAR will then be referenced as 'gui\appl.jar
C:\somewhere> cd project
C:\somewhere\project> java -jar gui\appl.jar
Also be sure to wait for the convert process to terminate (e.g. convertProcess.waitFor()) before exiting your application/java - I believe that any running external process is killed when the Java Virtual Machine is closed!
Hint in documentation of Process:
As of 1.5, ProcessBuilder.start() is the preferred way to create a Process.

My Java Slick game Jar file will not open

I have made a simple GUI using slick and lwjgl, I am using eclipse and have imported my jars, natives and images to my Java build path, I export my code as a jar file and get a jar file on my desktop, I try open it with SE Binary and it loads for a little while then nothing happens.
It may have something to do with how I have imported them into my build path, I will try explain, I have inside my Java project two additional folders that I have created, they are lib and res, res holds all of my images and lib holds two other folders called jars and natives, jars holds two jar files (slick and lwjgl) and natives holds 4 other folders (windows, linux, mac, solaris natives), I imported my 2 jar files into the build path using the add jars option, I then imported my res file using the add class folder option and after that using the same option I imported the lib folder, I then exported it and now it doesn't open.
It may be worth mentioning that before I export my jar files from eclipse I get the following pop up message: The operation repacks referenced libraries.
It seems that lwjgl can't use the native libraries when they are inside a jar archive.
The solution is to unpack your natives folder either into some install directory or a temporary directory. Then, before you use any lwjgl function, you need to set the new path to the natives. For example with the files unpacked in "lib/native" I use
System.setProperty("org.lwjgl.librarypath", (new File("lib/native")).getAbsolutePath());
System.setProperty("java.librarypath", (new File("lib/native")).getAbsolutePath());
System.setProperty("net.java.games.input.librarypath", (new File("lib/native")).getAbsolutePath());
This seems to be the easiest solution. For example minecraft (which uses lwjgl) unpacks the natives to "%appdata%.minecraft\bin\natives" (on windows, ~ instead of %appdata% on linux).
Edit: a very related wiki page: http://lwjgl.com/wiki/index.php?title=Distributing_Your_LWJGL_Application

How to customize java options of a independent netbeans application?

i'm developing an application using Netbeans RCP. I have added an option to add a jar to my class path in the project.properties file of my platform:
run.args.extra=-cp:a ./appclient/glassfish/lib/gf-client.jar
The problem i encounter is that is does work when i run it from the Netbeans IDE but not when i try to create a independent application (build for Mac OSX for instance). I hear that the project.properties is no longer taken in account when you run an independent application and of course my appclient directory containing the jars does not exist anymore in the application package (so my jar is not added to class path).
How can i make this -cp option works for my independent Mac OSX application?
EDIT: i was able to create a custom conf file for my independent platform but i can't find a way to add my jar to the class path, i don't know what options to use.
EDIT: i found that i need to you endorsed mechanism to achieve it. So i have added the following command to my app.conf file:
J-Djava.endorsed.dirs=/Users/altanis/appclient/glassfish/lib/gf-client.jar
But when i run the .app (mac application), i get this error:
-J-Djava.endorsed.dirs=/Users/altanis/appclient/glassfish/lib/gf-client.jar: No such file or directory
The path is correct. Do i need to make something special to make the JVM aware of this? I followed this tutorial and somewhere in the comments the author says:
Right, but the package-appclient copies everything for you and you
should be able to put it on the classpath using the endorsed
mechanism. Unpack the jar created by that and add everything you need
from there (the jars) to your application installer. Then you can use
the endorsed (-J-Djava.endorsed.dirs=${GFCLIENT_PATH}) mechanism in
your app.conf to put it on the application classpath. This way you
should be able to deploy it together with your client.
I think, that create a new library is the better way.
Create module type library with required jars
In your module add dependency to created module (type wrapped library)
You must add entry Class-path to you application's MANIFEST.MF
For example
Class-Path: apache-commons-2.1.jar ejb-api-3.0.jar
all this jars should be in the root directory of your application
Your appliction should have next structure
MyApplication.jar
/META-INF
/META-INF/MANIFEST.MF
/apache-commons-2.1.jar
/ejb-api-3.0.jar
/com/package/classes
or you can use jar tool of JDK to create a jar
read more here Oracle doc

Java DB Derby and Netbeans 7.1 Build Problems

My application accesses a Derby Database and I have added the Derby.jar to the libraries in the project. It runs fine when compiled and runs perfectly inside the Netbeans environment but once I Build the project, only my application.jar file is in the dist folder. The program will still run but once I try doing anything with the database it hangs.
I tried adding the lib folder containing Derby.jar to the home directory of the application.jar but I still get the same problem.
I'm new to Derby and I'm confused by this, any suggestions?
The answer 1 above does not address the fact the you need to first connect to the database in the service tab of the IDE before you can run your application and that is not possible when you run your application outside the IDE.
This is because you don't also add the project external jar dependencies (such as Derby's jar) to the classpath of your project's executable jar. Basicly, try following their tutorial here:
http://netbeans.org/kb/articles/javase-deploy.html#Exercise_1
the chapter entitled "Running the Application Outside of the IDE" and the one after that.
I haven't used Netbeans in a long while so I don't know if they added this functionality to it now, but with Eclipse you can also make a "fat" executable jar, where all the external jars are packed inside that executable jar, and Eclipse adds a special classloader which makes all this work. It's the option called "Package required libraries into generated jar". Maybe Netbeans lets you do that now too, via some export function or something similar.

Where is the debug class path in netbeans / where does the spring config XML file go?

I have a java desktop app (main project) and another project with a series of packages in NetBeans. Some of the packages use spring for JDBC and IOC.
I am getting the following error when running in debug:
Caused by: java.io.FileNotFoundException: class path resource [config.xml] cannot be opened because it does not exist
Where is the config file supposed to go? Where exactly is the class path? Is it in dist, build, in the root of the project that calls spring, or the main project (the desktop app)?
confused ..
Your classpath is defined when you run your app using the java command. You can specify it using:
java -cp $path my.Main
where $path is your classpath. It is a :-separated (; on windows) list of JAR files and/or directories containing compiled .class files.
If you run your program like:
java -cp configdir my.Main
And put your spring config in configdir (the fully-qualified path) then that should be discovered.
NetBeans: whilst I'm not a netbeans user, it probably offers a number of ways for you to complete the task you want:
In your run configuration (i.e. where you define what class is being run, what the command-line parameters are etc), you will probably be able to add items to the classpath. These might be directories or individual files
In your compiler settings, you can probably tell NetBeans to automatically copy files of a certain type (like properties files, XML config files) from your source locations to where NetBeans puts your class files.
If you put your config.xml file in the directory where NetBeans is compiling your .class files to
Put it in the root folder of you application
if you created your application in a folder called Spring then you should put your file in that folder
Disregard the answer by oxbow_lakes. NetBeans modifies CLASSPATH, so what it is outside the IDE is no measure of what it is inside the IDE.

Categories

Resources