runnable jar - not writing text file - java

Runnable jar file not producing output to ouput file.
The code works fine when running in eclipse.
This code writes some data to a text file.
Please help.
I created the runnable jar file using export option.
Thanks

If the code is running correctly in Eclipse then there is no problem in code.You have to first build the jar using Ant or Maven.Then you can place all the dependencies(jars) in the appropriate directory by creating a lib folder.Then place your jar in that same directory with all the config files.Then open your terminal(Command), go to the appropriate path and try 'java -jar YourProjctName.jar'.

Related

How do you export multiple java files as a singular runnable jar file in Eclipse?

I have a project with 3 java files:
Title_Screen .java
Game_Screen .java
Game_Code .java
Game_Screen and Game_Code use OpenGL, and Title_Screen opens Game_Code which opens Game_Screen. In eclipse, the program works perfectly, but when I try to export it as a runnable jar file, no matter what I do, it always just exports Title_Screen.java. What am I doing wrong, and what steps do I have to take to export all three java files in one .jar file?
Edit: It seems to only happen to my program, perhaps it's something to do with the OpenGL libraries?
Edit 2: I removed the libraries from my program, same results as exporting it to a jar file. My actual problem is that I can't put in the libraries.
Edit 3: Problem resolved! All I had to do was use jarsplice to create my runnable jar, not Eclipse. Tutorial I used: https://www.youtube.com/watch?v=YqGUk84BmlQ
You can use the following command to build the jar and specify the main class entry point (Main).
jar cfe output.jar Main src/Repository/* src/util/*.class
you can write multiple files when creating the jar
jar cf Output.jar src/util/Main.class src/util/SubMain.class src/Repository/*
Or from eclipse
Put all your files in a folder in your Eclipse project and then:
1.Right click in your folder
2.Export
3.Java -> Runnable JAR File
What steps are you following to export it as runnable jar?
Are you able to run the jar file successfully?
Steps to export Runnable jar is :
Right click on project and select "Export" option.
Provide the destination path where the jar file needs to be store.
Export

Executing a jar file using a .bat file

I need to execute a jar file using a .bat file. The problem occurs here:
"java -Djava.library.path=timer -classpath .;timer\timer.jar"
There is a folder called "timer" within the folder in which I execute the .bat file and which contains the timer.jar. The jar is executed but it isn't using the timer I wanted it to use, instead I get a debug message that it wasn't possible to load the library and the program used the standard timer. I tried to add the library to the classpath manually, but that didn't work either. What am I doing wrong?
the simplest way is to use a manifest file within the main jar to load all libraries.
using ide like netbeans or eclips will help.

Run Java project (built in eclipse) using Batch File

I want to run my java project which i've built using Eclipse IDE. Now my goal is to create a batch file which will execute my project with one click. i referred question , but didn't get any idea. Please give me a solution. Thanks in advance.
Because you are working in eclipse, this makes things easier. First, export the whole program as a executable jar. You can do this by going to Files>Export and then in the pop up go to Java>Runnable Jar. Then follow the steps required to make it. Next you make a .bat file and write the following code.
start javaw -jar NameOfJar.jar
Make sure that you put the file in the same directory as your jar. Now you should be able and click on the .bat and execute the program!
1.Open Notepad & write
#echo off
javac YOUR_JAVA_FILENAME.java
java YOUR_JAVA_FILENAME
2.Save-As executeJavaProgram.bat
Make sure that YOUR_JAVA_FILENAME.java resides with your batch file and java path is set in environment variable.
3 . Double click on batch file.
Follow this tutorial to create a jar file of your eclipse project.
After doing it create a batch file in the same folder where you exported the jar with the command: java -jar yourjar.jar
Create jar file using eclipse i.e right click on your project select export jar file then provide file name to store your jar file. In this file eclipse will keep all .class file only and in META-INF folder main class definition if your are creating executable jar file.

FileNotFoundException while running as Jar - Java

I have written my java codes using eclipse and iam reading a file and processing the codes. The key.txt is present in the same folder as src .
BufferedReader br= new BufferedReader(new FileReader("KEY.txt"));
Now after i compile the jar and place both the jar and file in the same folder and execute iam getting FilenotFoundException. The program works if i run inside Eclipse reading the file perfectly.
The jar file and the key.txt always have to be in the same folder.
Please let me know how can I solve this issue of FilenotFoundException.
The code you have opens a file in the current working directory, which is the directory where you started the Java process from.
This is completely unrelated to where the jar file is located.
How do you execute the jar file?
If the key file is right next to the jar file, this should work:
java -jar theJar.jar
But this will not (because the path to the key is now "test/KEY.txt" ):
java -jar test/theJar.jar
When you run a program in Eclipse, the current working directory is (usually) the project root folder.
An alternative to consider (if the file does not need to be edited by the user) is to put the key file into the jar file. Then it will not get lost, and you can load it via the classloader.
Another good option is to have the user provide the path to the file, via command line parameter or system property.
Or make a batch file / shell script that makes sure that you are always running from the proper directory.
I think you should put these files with class file of the running class, because the current directory is the directory in which the class file is stored, not the source java file.

Making excutable jar file using eclipse

My project runs fine from Eclipse.
But when I tried to make it into a jar file or executable file it doesn't work.
I used the option "Export-Runnable JAR file"
The following message appears just after the eclipse finished the exporting process
JAR export finished with wornings , see details.
the details were ..
Exported with compile warnings:Mario/src/Map.java
and the same for other classes like
Exported with compile warnings:Mario/src/Player.java
and so on.
So that I used the other option "Export - JAR file"
It works fine and nothing appears while exporting it from Eclipse.
But when I try to open the file it gives me
Couldn't find the main class:Frame.Program will exit
Somebody have any idea about what the problem is?
Your MANIFEST.MF file inside the META-INF dir should have a Main-Class attribute pointing to your main class.
The important thing for executable jar is Manifest. Make sure it exists and points to the correct class with main method

Categories

Resources