I have a project where I open some XML files to read and edit information.
This works fine when running in the IDE (Netbeans) but fails when I am building the project and running the .jar file.
Is there a reason why this might not work?
The path of the project in Netbeans is
NetBeansProjects\project\src\data
but when I am running the jar file, this path is
NetBeansProjects\project\build\classes
However, when I create the files in this folder, they still cannot be opened.
The XML files need to be in the classpath when the jar file is executed.
You can give the classpath using -cp option when executing the jar file. Similar question was answered here as well - Run a JAR file from the command line and specify classpath
Related
My LibGDX project runs in Eclipse but when I export it to an executable JAR file I get "Couldn't load file: play_button_inactive.png" and "File not found: play_button_inactive.png (Internal)".
I refreshed and cleaned the project. I deleted my assets link in the Desktop Project and re-loaded it in the Build Path but the same errors persisted.
I looked inside the jar file and Eclipse packaged in the assets separately into the Jar file so I put them all into a new "assets" folder manually but I got the same errors when I ran it. I used the command
java -jar fileName.jar
Here is my Build Path.
Thanks for any suggestions as to what I am doing wrong.
UPDATE: After reading around I found a solution which is to put all the assets into the same directory as the jar file. Is there any way to package them into the jar file?
I fixed the problem by not using Eclipse's jar file building tools and used gradlew desktop:dist instead.
This works perfectly and packages all resources and files into the generated jar file.
I just need a simple environment of .class and .java files in a single folder so that I can execute java files later on using "java xx" in command line without adding any extra syntax. I'm not planning to use packages or sub-directories in my project and I won't be executing any files directly from eclipse either.
You could create a executable jar file and run it from command line using:
java -jar MyApplication.jar
Here is the link to create an executable jar file from eclipse:
How to create an Executable jar file
I think you are looking for this:
Create new project->Java Project->In the Project Layout section change to 'User project folder as root for sources and class files' (using Eclipse Kepler, it should be similar in other versions of Eclipse).
But keep in mind that it might become a mess after some time.
I have a java desktop application in netbeans. I have created an executable jar file for the project using clean and build command provided by the netbeans. By using this command the executable jar file gets created under netbeansProjects//dist/.jar. I am able to execute this jar file from command line using java -jar .jar from within project path. But the problem is that when i move this jar outside of netbeans projects folder, say to desktop and run the jar file, it is giving error of type "Exception in thread "main" java.lang.NoClassDefFoundError". How to solve this problem and make the jar file executable from any location of the system.
Complete instructions may be found in dist/README.TXT:
To distribute this project, zip up the dist folder (including the lib folder)
and distribute the ZIP file.
Ensure that the manifest inside of the jar file contains the necessary classpaths. If you are unfamiliar with the concept, go here: http://docs.oracle.com/javase/tutorial/deployment/jar/downman.html
Netbeans has probably included any external projects/libraries/Jars in the dist/lib folder.
In order to run the application, you must include all the files in the dist folder when you copy the application
Check if in your projects Manifest.mf file has the Attribute
"Main-Class" set to your projects current main Class file.
I have a Java project that I'm working on in Eclipse. I have all my code in a src. Eclipse automatically compiles my .java files and stores the .class files in a directory called tmp.
Earlier, I was suing Ant to run my project. My ant script would would compile my Java code, and Jar all the .class files into a .jar and store it into a jar directory. In this jar directory I have some other files e.g. settings.cfg and data.lst. My application needs these files to run but they should not be included in the Jar. They will be deployed along with my JAR and should reside in the same directory. Currently while developing, I have modified and customised my settings and properties files to contain additional information. I don't want these files replaced every time I have a new build as I would like the data to persist across builds. I'm just looking to get the built JAR into the jar directory and execute that. The rest of the files stay the same.
How can I configure my Eclipse so that it always compiles my code, JARs it to the jar directory and executes that JAR. That way, my "extra files" are always in the same place as the JAR.
I'm having a lot of trouble figuring out how to accomplish this.
If you need this feature for fast running/debuging, create new Run Configuration and in the Arguments tab, Working Directory specify the "jar" directory.
The answer was found on another SO post. It was about creating a new "Run Configuration", removing the default classpath entries and adding a the folder containing the JAR i.e. the jar directory and also the JAR file itself.
Here: https://stackoverflow.com/a/1732316/304151
I also had to modify the build configuration a bit. For automatic builds, it used the Java Builder to compile all the .java files into .class files and store it in the tmp directory. I used a custom Ant task to package all the .class files from the tmp directory into a JAR and store it in the jar directory. This is the file that Eclipse executes.
In your project, right click Properties, in "Java build path" menu you can set "default output directory" in the Source tab and make it point to jar directory:
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
How do I create executable Java program?
I had implemented some speech recognition application in java.its working well with the eclipse when i run from eclipse..but my code not running when i m trying to run from command line ..
i had included some jar from the sphinx ..like sphinx.jar,jsapi.jar,wsj_dictionary.jar,my directory structure is as follows :
So can anyone suggest me how to make .exe or a executable jar file that can be run directly?
thanks in advance..
In Eclipse, you can export your project into an executable JAR file by right-clicking on your source folder, selecting "Export...", selecting "Java->Executable JAR File". Eclipse will then offer the option to either extract your external JAR dependencies into the final jar file, or place them into a folder with the final jar's manifest classpath referencing them.
The MANIFEST.MF file in your jar should specify the Main-Class and the classpath. Something like this:
Main-Class: full.package.path.to.your.main.class
Class-Path: space separated list of jar files
You would probably want to use an automatic build tool like Ant, Maven or Gradle in order to generate the MANIFEST.MF file in your jar that way.
Use Launch4J for wrapping jars in Windows executables.