Java .jar doesn't work on other computer - java

I made a simple card game using Eclipse Kepler. I then exported it to a runnable .jar file.
When I double click the .jar file, nothing happens. So I made a .bat file:
java -jar CardGame.jar
When I run it, it works fine, but when my friend runs it on his computer (yes, both files in the same directory) the cmd.exe will tell him: 'java' is not recognized as an internal or external command
After some searching I read that I had to add %JAVA_HOME% in the .bat file. This made no difference for me, but it will still not run on my friend's computer.
So my questions are:
- What is wrong with my .jar or .bat file?
- Can I run it without the .bat file, if so how?

He needs to have Java installed in the default path.
The easiest way to get that is to go to http://java.com/en/download/installed.jsp and follow instructions until Java is reported installed.
After that your friend should be able to run it.

Related

Java .jar file Run Differently from Command Line and File Explorer

I recently wrote an application that is packaged as a .jar file. When I run the application from the command line via "java -jar MyProject.jar" everything looks perfect, just as it does when run from within eclipse. However when I go to the file location in the file explorer and double clock the file, it doesn't quite work. All of the functionality is there, but the GUI is not quite right. All text is much larger, despite the application not changing size, when run like this, to the point where much of it is cut off.
Anyone know whats going on? Thanks!
The cause of this issue was that when being run from the command line, the program was being run with JRE 1.8.0_172, however when being run from the file explorer, the program was using JDK 10.0.1

.Jar is not opening

I'm having trouble opening a jar filewith java. I've tried uninstalling java and reinstalling it(and different types of java, JDK) and have tried opening the file with .java, .javaw, and .javaws in both the jre and jdk files in the java folder. When I use the .java it opens a black command console for a split second and then closes. I'm running windows 8. I have ran the same file on other pc, my pc doesn't want to open the .jar file. I am using intellij idea. Thanks in advance!
You get an error running the jar file. It displays for a split second and the it exists.
Try running it from the command line. That will give you more insight in to the error that prevents you from running the jar file.
Go to cmd (press Windows key and type 'cmd', press Enter). Navigate to the directory where you have the jar file and execute
java -jar yourJarName.jar
Substitute 'yourJarName' with the name of your jar file. Now you should be able to see the error, and we can work on it from there.
Have you tried to run it from console
> java -jar your_jar_file.jar
To see what could it be wrong?

.exe from launch4j works, but not on all computers

I wrap my Java project's JAR file to an .exe file using launch4j, which works fine on my dev machines and several other computers. However, one user reported he cannot run the .exe file on his machine, as a popup message tells him
Could not find the main class.
Interestingly, the user is able to run the JAR file directly on the same computer, either by calling it via java -jar from command line, or directly by double-clicking the JAR file in the Windows explorer (or via shortcut). That user has installed latest JRE.
I have seen on other threads here that this problem could occur because of invalid Manifest files, e.g. if the main class is missing there. However, I think this is not the case, because it's working fine if the JAR is called directly as said.
So I assume the problem is somewhere with my launch4j configuration. There I have not specified a custom Manifest file, only the input JAR file and the output .exe file.
I thought my launch4j config would be fine, because the .exe is running on several machines using different Windows versions. This is the first time I encounter that problem, but as said with the .exe file only.
Any ideas?
We just found the problem with that one client machine. It had the 64 bit version of the JRE installed, that was causing the problem. After replacing it with the 32 bit JRE version the .exe file can be correctly started again.
Yes,even I faced this problem. I used jar2Exe instead of launch4j. It's working fine.You better use it.

How to copy my Java Eclipse project to desktop, and then run with the command line?

I have built a java program using Eclipse. Now my teacher wants all separate java files in one folder, and then the teacher will run my program through command line. I've tried copying the java files over to my desktop and running through command line, I get errors.
My program also has two packages as well in it. Anyone know how to fix this? without using any plugins or something, as my teacher will not have them.
Edit: I managed to fix it, it was because of my classpath had wrong directory
Just export your project in an runnable jar file, then go in cmd and execute:
java -jar .jar

Executable JAR on Ubuntu (NetBeans)

I'm writing a simple Swing application in NetBeans and doing so on an Ubuntu machine for the first time.
As many of you know, NetBeans automatically creates executable JARs for projects that are "set as main".
On Windows, you can double-click an executable JAR and it automatically invokes the JRE and runs the app. In Ubuntu, double-clicking the .jar file causes the file to be opened in the archive manager instead. In order to run my JAR, I either have to right-click it and select "Open with OpenJDK Java 6 Runtime" or launch it from the command line.
From the command line I get no problems whatsoever. However, when I try launching it from the right-click menu, I get an error that reads:
The file MySwingApp.jar is not marked as executable...
So I have 2 questions:
What do I have to do to set it as executable? Is this something I can do inside NB or do I have to use the shell? If I have to set permissions via the shell, doesn't that conflict with NB's policy of auto-generating **executable** JARS? And what command would I use to flip the executable bit anyhow?!?!
Is this just a Linux hiccup? I want to send this JAR to friend who run Windows and I'd like for them to be able to just double-click it and have the program launch
Thanks for any helpful suggestions!
You will need to manually tweak your build process to get the jar file marked as executable in Netbeans. Go to your project root and open build.xml. The header has instructions on adding to the build process. There is a target, "-post-jar", that is called after the jar is built. You'll need to make that target and use Ant's chmod task to modify your jar. Once you do that it will occur every time you make a jar file in that project.
It will run fine on your friend's Windows machine, as long as he has a JRE installed.
Here is a thread about running jars using double click in Linux.
You can Java like a native binary on Ubuntu (and other linuxes), it's a feature of the kernel. You need to install the binfmt-support package to give the kernel the hooks to run java in this way.
In Ubuntu open up a terminal and run:
sudo apt-get install binfmt-support
Then make your JAR file executable
chmod a+x yourjar.jar
Then you can run your JAR like any other binary by typing
yourjar.jar
Jar-files aren't first class executables, and they don't become magically executables by changing their executable flag.
If you execute a jar, you run the command
java -jar YOURJAR.jar ...
It's the same, as if you double click a png file, and expect it to run in a painting program
gimp YOUR.png
You don't need to make your png an executable one, and it will not solve a problem.
Instead, you have to tell your desktop environment, what to do when double clicking a jar or png-File, and you have to do it on Linux the same way you do it in Windows - maybe the installer on Windows does it for you, because there is normally just on Desktop Environment (Windows) on the OS (Windows), but Linux has Gnome, KDE, XFCE, LXDE, fluxbox and millions more.
And it isn't so sure what you want to do with it. Since jar-files are a special form of packed zipfiles, usually containing a Manifest and the classes, the Archivmanager isn't a false solution, and it is saver to show the content of the archive, than executing it.
Copying the file to windows has no effect. Windows not even has an executable flag, but you shouldn't fiddle with it though. You change your desktop settings, and those can't be moved to windows, and you will not want to.
And if you have the correct settings in your DE, you don't need to tell Netbeans or any other IDE repeatedly, what to do with jar-files.
I gone through Internet and I came across one article with complete steps to run jar file
http://mlartist.blogspot.in/2012/07/deployment-netbeans-project-in-linux.html
Jar files are basically a zip file, to create an executable, you have several different methods. The Best (in my opinion) is to use ant to create it. Or you can simply echo "Main-Class: YOUR.MAIN.CLASS" >> Manifest and then create your jar by jar -cmf Manifest JARFILENAME.jar INPUTFILES then, to make it executable under linux, right click on it and click on properties. Then click on permission tab and check execute. or you can be a terminal bamf and cd to the jar directory and chmod +x JARFILE.jar
HAPPY NIXING!!

Categories