Previously, I have no problem in running the JAR file generated by NetBeans.
However, I encountered the problem now all of a sudden. When I click on the jar, it did not launch the application as if nothing is clocked.
But, it can be run from the project. And also, the size of the Jframe for desktop Java application cannot be set from the NetBeans code also. When it runs, the size of the window different from in the designer.
Any help please.
Check your META-INF/MANIFEST.MF file. It should contain a Main-Class: attribute pointing to the class to run initially.
Failing that, what happens if you run it from the console e.g.
java -jar yourjarname.jar
That should give you a much better indication of what's going on.
try to check your JAVA_HOME environment variable, and try to right click on the jar and do open with and check what application is used to open it.
Associate jar files with java instead of javaw. Relaunch. See the error message.
Related
Running in intelij
When i try to run the .jar
Edit environment variable path
I've written my first small program in java, and want to make it into an executeable file.
I used InteliJ to generate a .Jar file out of the Main class. When i Run the Jar in Intelij it seems to work fine. However once i try to open it from my windows i get
error a jni error has occurred please check your installation and try again
Some googling told me that my JDK might not be properly set in my system properties, so i added the path to my JDK, however the error remains. Anyone have any idea what's up with this? I believe that I added my JDK through inteliJ. Why is it so difficult to simply run the program i made? Is there something wrong with my JDK? is my .jar corrupt?
It looks like this is a common problem, but none of the previous posts seem to address my issue.
I believe I've narrowed it down to one problem. Any application that uses an InputStream will not open, but all my other applications run fine.
The application runs fine in Eclipse, but the window won't even open when I try to run the jar file.
Task manager shows it pop up for about a second or two, and then disappears.
I have tried all three options for the Library handling upon exporting and none of them fix the issue.
Can anyone explain this?
Run it from the command line. This will allow you to see the exception that's thrown that's preventing your program from progressing.
java -jar YourJar.jar
I know I'm late, but I want to prevent people having to search hours for the same Error I just did. In Eclipse the path-String isn't case-sensitive. In the exported Runnable Jar File it is. So make sure all the pathsnames have the capital letters in the right positions, or to be save don't have capital letters at all.
Setup Your Manifest
If I had to guess, your manifest doesn't contain your main class OR the classpath is not defined.
Main-Class: https://docs.oracle.com/javase/tutorial/deployment/jar/appman.html
Class-Path: https://docs.oracle.com/javase/tutorial/deployment/jar/downman.html
If you're using a build tool like maven, it needs to be configured to add these properties: https://maven.apache.org/shared/maven-archiver/examples/classpath.html
Execute Your Jar
There are two ways to run an executable jar. As an executable jar you need to define the Main-Class and Class-Path in your manifest:
java -jar YourJar.jar
You can also skip setting up your manifest and define your classpath and main class through the JVM's parameters.
java -cp=${PATH_TO_JAR} main.package.MainClass
I'm at my wits end. I can't create a jar file (manually or through Eclipse). I've followed instructions for creating the manifest and jar file manually. The end result is that I get an error saying "The JavaJAR file 'widgetSamples.jar' could not be launched. Please check the console for possible errors." The console doesn't register anything when I launch the jar file or when the error message pops.
In eclipse when I create the jar I do get warnings:
Exported with compile warnings: SWT Resources/src/widgetSamples.java
I created a separate simple program to see if it might have been something with my code but I can't even get a simple SWT display to open; I get the same error. The code executes properly in Eclipse.
I have the 64 bit SWT installed as far as I know. Any help is much appreciated. Thanks.
Creating jar file using eclipse IDE
http://www.tutorialspoint.com/eclipse/eclipse_create_jar_files.htm
Creating jar using CMD
http://www.wikihow.com/Create-JAR-File
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.
So I've been writing a simple 3D GUI application that I intended for users to use simply by double-clicking on the JAR file. I got it working perfectly before putting it into the JAR file, and I got it working perfectly IN the JAR file while running from command prompt (typing "java -jar Modeler.jar" while in the directory of the jar file). However, when I double-click it, nothing happens. It runs perfectly fine with no errors from command prompt. I know from experience that crash reports on start-up are not shown because the console doesn't appear (or it disappears too fast), but when running from the command prompt there are no crash reports. Any ideas as to why it won't work? I'm running Windows 7 Home Premium. Here are the contents of the JAR file if it helps:
Modeler.jar
|
+--*all the class files necessary*
|
+--META-INF
|
+--MANIFEST.MF
Contents of MANIFEST.MF:
Manifest-Version: 1.0
Built-By: AnonymousJohn
Class-Path: bin/j3dcore.jar bin/j3dutils.jar bin/vecmath.jar
Created-By: 1.6.0_16 (Sun Microsystems Inc.)
Main-Class: Start
EDIT: So after messing with the file associations to use java.exe instead of javaw.exe (thereby providing a window for print-outs), then modifying the startup mechanism a little to print out the current working directory, I discovered that the jar is running from "C:\Windows\system32" instead of the folder on my desktop I put it in. Go figure. However, moving the necessary outside files there doesn't help anything.
EDIT 2: I tried making another JAR file, this time with a simple JFrame with a button in it that tells you the current working directory. Press the button and it opens a (useless) JFileChooser. This worked on double-click no matter where I put it in my computer. So there must be something wrong with my JAR file. I'll start troubleshooting my program again.
EDIT 3: The problem is just what I thought it was: it's not loading libraries correctly when I double click on it. The weird part is that in my tests where I display the current path and library path, the output is exactly the same whether I run it via command prompt or via double-clicking on it. Here's the stack trace:
java.lang.UnsatisfiedLinkError: no j3dcore-d3d in java.library.path
at java.lang.ClassLoader.loadLibrary(Unknown Source)
at java.lang.Runtime.loadLibrary0(Unknown Source)
at java.lang.System.loadLibrary(Unknown Source)
at javax.media.j3d.NativePipeline$1.run(NativePipeline.java:231)
at java.security.AccessController.doPrivileged(Native Method)
at javax.media.j3d.NativePipeline.loadLibrary(NativePipeline.java:200)
at javax.media.j3d.NativePipeline.loadLibraries(NativePipeline.java:157)
at javax.media.j3d.MasterControl.loadLibraries(MasterControl.java:987)
at javax.media.j3d.VirtualUniverse<clinit>(VirtualUniverse.java:299)
at javax.media.j3d.Canvas3D.<clinit>(Canvas3D.java:3881)
at ModelPreview.<init>(ModelPreview.java:51)
at Modeler.<init>(Modeler.java:76)
at Modeler.main(Modeler.java:1227)
at Start.main(Start.java:92)
Only problem is that it IS in the library path. I specifically set it in the program. Now that I think about it that may be the problem. I set it like so (this was a method I found somewhere on the internet. I don't remember where):
//above was code to get newPath based on the Operating System.
//all this code is set in a try-catch phrase.
//reset the library path
System.setProperty("java.library.path", ".\\bin\\natives" + newPath + ";");
//make sure the ClassLoader rereads the NEW path.
Field f = ClassLoader.class.getDeclaredField("sys_paths");
f.setAccessible( true );
f.set(null, null); //ClassLoader will automatically reread the path when it sees that it is null.
EDIT FINAL: Well, after looking and relooking at my code, I discovered the problem was in some BS'ery involving the detection of 64-bit systems where it was loading the wrong dll's. Why it worked from the command-line and not via double-click I don't know and will probably never know, but it works via double-click now, so I'm happy. Sorry about the troubles.
Ok, so I was stuck on this exact issue for over a week now (it has been for a side project for which I could only devote a few hours a day).
This happened on my desktop, but for some reason wouldn't happen on the laptop.
After looking around, I found this answer and I thought I'd share it for people who are like me, found nothing useful in the accepted answer here. Credits go to anonymous Stack Overflow user, whom username I've lost in all the excitement.
As mentioned by some other answer somewhere to an seeming unrelated issue, use this little program to associate your JAR files to the 64 bit version of java:
http://johann.loefflmann.net/en/software/jarfix/index.html
Save the program somewhere and run it from command line with the parameter /64:
c://path//jarfix.exe /64
Nothing else worked for me, but this was like magic. :)
The JAR is executable from CMD. That means the JAR itself is formed correctly. Good.
The only reason to fail now is that double-click produces not a right command. Expected command, as you correctly said, is
java -jar Modeler.jar
But when you associate javaw.exe with a JAR extension, I suspect it executes
javaw Modeler.jar
It is easy to check: make a javajar.cmd file, containing the following
javaw -jar %*
and associate it with JAR. If you app starts OK, I'm right. Otherwise, sorry.
I got fed up with not being able to modify file associations in Windows 7 Control Panel and edited the registry (NOTE: it's not a bad idea at all to set a restore point before proceeding if you think there's ANY chance you may screw up)(I neither set restore point nor screwed up):
Windows Registry Editor Version 5.00
[HKEY_CLASSES_ROOT\jar_auto_file]
#=""
"EditFlags"=hex:00,00,00,00
[HKEY_CLASSES_ROOT\jar_auto_file\shell]
[HKEY_CLASSES_ROOT\jar_auto_file\shell\open]
[HKEY_CLASSES_ROOT\jar_auto_file\shell\open\command]
#="\"C:\\Program Files\\Java\\jdk1.7.0_60\\bin\\javaw.exe\" -jar \"%1\" %*"
This assumes that .jar files have (Default) value jar_auto_file. If not do this:
Windows Registry Editor Version 5.00
[HKEY_CLASSES_ROOT\.jar]
#="jar_auto_file"
p.s. One problem I found in the registry was that jarfile was associated with Netbeans, Chrome, and java.exe as well as with Applications\java.exe. I deleted that entire node (after exporting it, just so I could put it back if it was essential; it wasn't). Now there is NO jarfile in my registry and all .jar files execute as before.
This manifests itself by showing more than one file type associated with .jar files in Control Panel. You want this:
If there's more than one item shown in Recommended programs, search registry for jarfile and (export first, then) delete that node:
HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Explorer\FileExts\.jar
Prerequisite: make sure JRE(that is included in JDK) is installed.
Solution
Open regedit and navigate to Computer\HKEY_CLASSES_ROOT\jar_auto_file\shell\open\command
Add "-jar" to value data. For example, change from "C:\Program Files\Java\jdk-15.0.1\bin\java.exe" "%1" to "C:\Program Files\Java\jdk-15.0.1\bin\java.exe" "-jar" "%1"
Done
If problem still occurs, make sure you configure correct main class in project property.
Just to put my two cents in, I had this problem and it was caused by something-or-other in the manifest, I suppose. I resolved it by generating the executable JAR in a different manner:
Previous developer generated the Runnable JAR from Eclipse (right-click project -> Export... -> Runnable JAR) and this worked fine for everyone. Then I came along and generated the runnable JAR by configuring a Maven plugin ("assembly") in the pom.xml and so a classic Maven build would produce the runnable JAR.
This worked nicely for me (from cmd and by double-clicking) but not for the end-users (who could still run the tool from cmd, but not by double-clicking the JAR).
I generated the runnable JAR from Eclipse and now everyone is happy again. Not sure why the maven-generated JAR wasn't ok to run by double-clicking, but I can't be bothered to investigate further now that the job is done. Hope this half-advice helps someone.
I had same issue with .jar files after trying everything i realized that I have more than one versions of Java installed. Removing the unnecessary versions solved my issue.
May be because the .jar was confused between different versions of JRE.
Just so that the answer to the question is clear to anyone passing by, I'll put my solution here (I couldn't before because of the 8-hour rule):
Well, after looking and relooking at
my code, I discovered the problem was
in some BS'ery involving the detection
of 64-bit systems where it was loading
the wrong dll's. Why it worked from
the command-line and not via
double-click I don't know and will
probably never know, but it works via
double-click now, so I'm happy. Sorry
about the troubles.