I have made a program that parses a text file and then works with the reuslt. I now need to compile this so I can deploy it on a server. 2 things.
1) I want to be able to update the text file regularly...is this possible without recompiling every time?
2) when I compile it with eclipse, I get a jar. If I click it nothing seems to happen. Any advice?
Thanks in advance!
Yes. You can do it without recompiling the program every time.
Create an executable jar from eclipse.
Select your project> File> Export > Java> Runable Jar File.
Please specify the main class where the main method exist.
Now it you click on this jar file , your program will be executed.
Note: If your program does not have any user interface, you will not see anything. When you will click on the jar program will be executed and immediate finished.
Related
i have a java project where i am accepting input from user and displaying output in console. I want to create a jar file so that other users can also run and use it. Can someone help me out with the steps. I am exporting jar file and running it but getting error " Could not find or load main class" while running.
If you use Netbean, it can build the jar file for you automatically. Just set your main class then click on clean and build.
Are you doing it through eclipse?
If yes, then follow below.
Make sure while exporting a jar to select an option under java as Runnable Jar file. Press next
Make sure you select Launch configuration, the one by which you run the project contains main method.
Hope it helps.
I'm using IntelliJ to create JAR file, and it executes normally when run from command line, but on double clicking it, nothing happens. The JAR I export from eclipse work normally, and since I'm a beginner using IntelliJ my guess is that I am doing something wrong.
I'm exporting the JAR in this way:
Project structure -> jar -> from modules and dependencies then use this
configuration to build.
Not sure why everyone is playing dumb about self executing jars.
Just add a MANIFEST.MF file and specify the fully qualified main class as Main-Class: my.package.MyClass
Also, make sure your executable type for .jar file type is Java
Once you do this you can just double click the jar to execute it.
Intellij will not execute a JAR for you. Remember JAR is a container, Is the same as if you were asking to execute a ZIP file.
What I am sure you want is to execute a class inside your jar file. Which would be the class which has the:
public static void main(String[] args) { ... }
You have two options:
1) execute the jar from command Line
2) right click on the "main" word I wrote above and then press RUN.. You will see a small green arrow (play button). That is the only way of running a class inside Intellij.
Hope it helps
If your JAR starts from the command line using jar -jar <file>, that's as executable as a JAR file gets (in other words, metadata specifies where the main class is). You can if you want use the OS provided tools to specify that when you double click on a .jar file, it should execute java -jar <file>, but that's beyond the scope of JAR files. If you're running Windows for example, you should be able to achieve this by following the procedure outlined here. Again, this is OS specific and may even be specific to which version you're running, and is essentially just a fancy wrapper around the double-click event to execute the java -jar command.
Make sure that the x-flag is set for the jar.
java -jar xyz.jar
does not need that, but double click need it:
ls -l xyz.jar
chmod 755 xyz.jar
I have a project I'm working on and planning to export as a .jar to be released to public. However a major problem I have with Java is that some things work in Eclipse, but won't as a .jar file (ie directory problems: sometimes the directory is right in Eclipse, but when exported it will no longer work). I would like to know if there is a way to check for errors in a jar file directly in Eclipse. I know you can import the new jar file as an external jar file and run it that way but I don't like having to reimport a new jar file every single time a create a new version of one. What can I do?
You can write logs in your code and see that logs in java console when your jar start executing.
how exactly you are building jar ? using command or anything else.?
I have a java project that works perfectly fine when running it from within Eclipse. When I try to export it to either a "JAR file" or "Runnable JAR file" the .jar file is created, but when I double click on it to try to run the program it gives me an error that says
"Could not find the main class: package.MainClassName. Program will exit."
As I mentioned, I tried exporting to both JAR options, I specified the correct class that the main method is in, and when I look through the actual files in the .jar file everything seems to be in order -- the manifest looks something like:
Manifest-Version: 1.0
Main-Class: package.MainClassName
(blank line)
and is in the META-INF folder. There is a folder with my package name, which contains all the .class files, including the class that contains the main method. A few image and text files that I use also appear in the jar file.
The actual program isn't anything too complicated -- it's a simple "snake" game using Swing (plus the code all works when run from inside Eclipse).
Any ideas what is causing this error and how I can fix it? Let me know if there's any other information I should provide.
Verify that you can start your application like that:
java -cp myjarfile.jar snake.Controller
I just read when I double click on it - this sounds like a configuration issue with your operating system. You're double-clicking the file on a windows explorer window? Try to run it from a console/terminal with the command
java -jar myjarfile.jar
Further Reading
Running JAR file on Windows
The manifest has to end with a new line. Please check your file, a missing new line will cause trouble.
Run it like this on the command line:
java -jar /path/to/your/jar/jarFile.jar
Ok, so I finally got it to work. If I use the JRE 6 instead of 7 everything works great. No idea why, but it works.
Had you tried creating a .jar file manually instead of using Eclipse. Try the following steps, hopefully that might help :
Considering that your directory structure looks like this :
TicTacToe(Your Project Name I mean)
| | |
src bin manifest.txt
| |
icons tictactoe
Now suppose that my main class is BeginGame inside package tictactoe, so I will write inside my manifest.txt file this thing :
Main-Class: tictactoe.BeginGame
Do remember the space between colons : and package name i.e. tictactoe, and immediately after BeginGame press Enter and save the file.
Now on your command prompt go to the location of bin folder, I am describing my side as follows :
C:\Mine\Eclipse\TicTacToe\bin>jar -cfm ..\tictactoe.jar ..\manifest.txt tictactoe icons
Here the first argument i.e. ..\tictactoe.jar is used to tell that create tictactoe.jar one level up, i.e. inside TicTacToe Folder.
Second argument ..\manifest.txt means that the manifest.txt file is located one level up, i.e. inside TicTacToe Folder.
Third and Fourth arguments tictactoe and icons means, that add both these folders to the .jar file, since they are placed inside bin Folder so they are used as it is. Now Press Enter.
Now try to run your .jar file so created inside the Project Folder (TicTacToe Folder, in my case).
Hopefully this will work.
Have you renamed your project/main class (e.g. through refactoring) ? If yes your Launch Configuration might be set up incorrectly (e.g. refering to the old main class or configuration). Even though the project name appears in the 'export runnable jar' dialog, a closer inspection might reveal an unmatched main class name.
Go to 'Properties->Run/Debug Settings' of your project and make sure your Launch Configuration (the same used to export runnable jar) is set to the right name of project AND your main class is set to name.space.of.your.project/YouMainClass.
I ran into the same issues the other day and it took me days to make it work.
The error message was "Could not find the main class", but I can run the executable jar exported from Eclipse in other Windows machines without any problem.
The solution was to install both x64 and x86 version of the same version of JRE. The path environment variable was pointed to the x64 version. No idea why, but it worked for me.
Right click on the project. Go to properties. Click on Run/Debug Settings. Now delete the run config of your main class that you are trying to run.
Now, when you hit run again, things would work just fine.
For netbeans user that having this problem is as simply:
1.Go to your Project and Right Click and Select Properties
2.Click Run and also click browser.
3.Select your frames you want to first appear.
you are just missing static keyword. that's all.
I will try to tell you exactly my problem, because I am stuck for more than 2 days now.
I am trying to make a project in Eclipse from existing source code (a program called sweethome3d) and then modify it. But, the problem even starts from even before modifying the code.
There are the steps that I am following:
File - import - existings projects into workspace.
I select the source code (http://sourceforge.net/projects/sweethome3d/files/SweetHome3D-source/SweetHome3D-3.3-src/SweetHome3D-3.3-src.zip/download)
Then, I try to export it into Runnable jar file
I have also to add some VM arguments in the command line when I try to execute it.
The runnable jar file does not run
in command line
with double click
Do you have any idea what I am doing wrong?
Thanks in advance.
If this is the same as the version of the code you're running, then SweetHome3DBootstrap.java, at line 90, is attempting to load com.eteks.sweethome3d.SweetHome3D dynamically. This class needs the class javax.jnlp.UnavailableServiceException, which is from javaws.jar.
So, when you try to run your JAR, it's missing javaws.jar at runtime. You'll need to either add it to the classpath (specified via the command-line when running, or via the JAR file's 'Class-Path:' manifest header), or re-package the classes from javaws.jar inside the JAR you're creating (like FatJar).
It could be some dependency problem, hard to tell without knowing more. Try using FatJar Eclipse plugin