Trouble executing a jar file - java

I am trying to run a executable jar file I made in Intellij on JDK 11.0.2 that contains a GUI system. I have java 8 and Java SDK 11.0.2 installed. Whenever I double click the jar file I expect the main gui login screen to come up, but nothing happens. I have tried other options such as trying to run it through cmd (it gave me an access error) and the HKEY_CLASSES_ROOT\jarfile has the command: "C:\Program Files\Java\jdk-11.0.2\bin\javaw.exe" -jar "%1" %* already.

Cant comment quite yet on other peoples posts so ill just submit an answer, tho its gonna be more of a swing and miss probably.
Id recommend opening that jar file with a java decompiler (The one i had in mind is jd-gui) and checking the integrity of the code, file system, and, in general, just check arround for common errors when packaging, maybe you included something you shouldnt in the class and hence why the problem, or its crashing on load cause of a missing reference you had linked to with a relative path which got broken upon moving the file, those are the two big ones that come to mind that you should check first upon opening the file, other than that, i cant provide much more insight, godspeed to you!

Related

Java Jar file main class run when runs from command line, but not from Windows batch file

I'm re-using a standalone Swing-based Java class which backs up and restores mysql databases.
I've tested running it from a Windows batch file (.bat) on my dev system, and it works there.
But, if I run the batch file on a different Windows , I get a "main class not found" exception.
However, when I run the command directly on the command line, it works.
The command in the batch file to run it is:
java -cp lda-services.jar;bip-services-1.6.0.0-SNAPSHOT.jar;decryptor-1.6.0.0-SNAPSHOT.jar;slf4j-api-1.7.31.jar;commons-io-2.6.jar com.ilcore.util.SosaMaintenanceJFrame
The SosaMaintenanceJFrame class is contained in the lda-services jar.
Here's the error message:
Error: Could not find or load main class com.ilcore.util.SosaMaintenanceJFrame
Caused by: java.lang.ClassNotFoundException: com.ilcore.util.SosaMaintenanceJFrame
The class is definitely in the jar file, as I've extracted it the file and seen it.
Any thoughts on why this would be happening? I need to run inside a batch file so the user can just click on it to run it.
Most likely explanation
Your paths are relative, which means that the batch file isn't going to work unless you run it from the right place. In general, having a batch file that has an invisible rider stapled to it with: "I break in mysterious ways if not run from the appropriate dir" is a crappy batch file - make it better.
Better solution
Or, even better, get rid of it. You don't need batch files to distribute java programs.
Proper ways to distribute java programs:
The modern take is very very different from what you have here: JREs are dead, you must ship an installer that does the whole thing, notably including a java runtime (no longer called a JRE, and one you ship and keep up to date if relevant). That's perhaps a bridge too far for what you're doing here. Relevant tools include jlink.
A slightly less modern take involves jars with manifests:
Your jar file should contain a manifest. This manifest must contain 2 relevant entries:
Class-Path: lda-services.jar bip-services-1.6.0.0-SNAPSHOT.jar decryptor-1.6.0.0-SNAPSHOT.jar slf4j-api-1.7.31.jar commons-io-2.6.jar
and
Main-Class: com.ilcore.util.SosaMaintenanceJFrame
You can use jar's -m switch, or just include the manifest (it's just a file in the jar): it's at META_INF/MANIFEST.MF and it's a text file, each line is an entry, and an entry consists of a key: value pair.
When a jar contains this, just double clicking the jar and running java -jar thejar.jar will then take care of it all: Java will load the stated jars as part of the classpath, and these, crucially, are resolved as paths relative to the directory the jar is in, so it DOES work when you try to launch them from elsewhere, i.e. if you do:
C:
CD \
java -jar "c:\Program Files\MyApp\myapp.jar"
it works fine, whereas that batch script would fail due to being in the wrong place.
Build systems let you define the manifest too, check your build systems docs for how to do this, it'll be easy, and there are tons of tutorials if you search the web for e.g. 'manifest executable jar maven' or whatnot.
You can consider making a shaded jar. But I wouldn't.
A shaded jar takes all your dependencies and packs them into your main jar, so that there is only one jar. There is now no need for a Class-Path entry (the jar you run is obviously already on the classpath and there's nothing else to include) and your app is shipped as 'just' a single jar file.
But this is mostly a red herring: There are no consumer JREs anymore so you've made the user experience from a D- to a D. If you actually care about giving your users a nice experience, there's no getting around an installation process of some sort and once you have that, having the separate jars is no longer a problem. Separate jars are less hairy when signed jars are involved, are much easier to keep up to date, and have a significantly faster turnaround (when you build your stuff and want to ship what you built, shading takes ages, so it's nice to cut that step out). The faster your CI system tells you about failing tests, the better.
Meet in the middle
You don't have to upgrade to modules and the like. What you can do instead is use something like launch4j. The aim is to end up with a zip file along with the installation instructions: Make a dir somewhere. unzip this zip in it. Doubleclick 'myapp.exe'. Done.
The zip would contain an entire JRE, all your jar file deps, and your main app, and an exe file which launch4j made for you, that launches your app using the JRE packed into the jar. This means you know exactly which JRE is being used, and it'll work even on systems that didn't have one installed yet (which, these days, should be all of them - the notion of 'end user downloads a JRE from oracle and the user + oracle work together to keep that thing up to date and security-issue-free', is dead).
The fact that it's an EXE is nice: Now if the user e.g. alt+tabs through their apps, they get your app, with your name, and your icon, instead of 'javaw.exe' with an ugly coffee mug logo.
But when I try running it from the jar file generated by Maven, however, I get a "class not found" exception.
Even if you didn't get that error, you'd get another one unless you'd used Maven Shade, as that's the only way you're going to run that with a single jar. My guess as to why that particular error occurs is that the app class you're attempting to run is in fact in one of the *SNAPSHOT* jars

Setting up Cloud9 IDE to compile and run Java?

I have tried posting this request in Cloud9's forums, but since it never posts, I am hoping someone here could give me a hand. (The forum keeps saying my question is being submitted for review, but then never shows up).
I can't get Java code to compile and run. I've Googled the issue and read other posts in their limited forums, but I can't get it to compile and run a simple Hello World! program. My .java filename matches the class and the code is fine.
I have tried putting my .java files in the following directories:
test/java/
test/src/
I just get this:
bash: src/HelloWorld.java: No such file or directory
I tried this: http://www.programmingforliving.com/2014/09/java-coding-with-cloud9-ide.html
But I just get:
Running HelloWorld.java
Error: Could not find or load main class HelloWorld
Any suggestions? Given I'm essentially a Java noob I am hoping the solution is fairly simple.
Without you pasting in your code it's tough to tell but make sure your class and filename are both titled HelloWorld. Make sure your src folder is in your main workspace folder so your file should be at /home/ubuntu/workspace/src/HelloWorld.java.
Also, this documentation looks pretty similar to the blog you referenced but still worth a look https://docs.c9.io/docs/custom-runners.
I think that Codenvy is much better in java than Cloud9. It provides easy build & run configuration, code completion, project sharing with permissions or factory (creates clone of your project with one click in minutes).
Take a look at official java spring factory. This link opens temporary(personal) java project ready to code, build & run.
Also Codenvy has project wizard that has a lot of project types including java console app (uses maven).
Ok, decided to screw around with it and figured it out... (then saw that Brady Dowling had posted a link that explained in better detail what needed to be changed).
At any rate, I noticed a few things in Cloud9.
Even after making the changes and saving, it would still not compile correctly. I had to close my session of C9 and go back in. Then it would work.
After compiling, and trying to run the program, it wasn't automatically picking the Java runner I made. So if you are running into issues, make sure the Java (or whatever you decided to name it) runner is selected in the console.
You have to compile and run separately. Kind of annoying, but at least it works. Unlike C/C++ in C9, it won't compile and run by simply hitting 'Run'. If you do, it creates an error saying the .class doesn't exist. Simple as pressing F7 to build, then F5 to run.
Anyway, I took screenshots of what I had to change to get it working. Essentially, just the directories it looks for the .java file in (the right arrows) and the directory it looks for/compiles the .class file to (the left arrows). Since I was putting the .java and .class in the same directory (java), I just changed both to 'java'. In the links Brady and myself posted, they used .bin (for .class) and src for (.java).
1st picture is the Builder file (I called it javac) and the 2nd picture is the Runner file (I called it Java).
Anyway, I hope this helps someone out. I am really liking C9 and it perfectly suits my novice needs at the moment. I like the ability to dabble in multiple languages to see what I like without having to switch applications. The real-time live preview HTML/CSS like Brackets is another huge plus for me.
Regards
For what I understand, C9 workspaces don't come with jdk installed. But I always use these commands:
sudo apt-get update
(wait a while for it to finish processing. Then)
sudo apt-get install default-jdk
(Then It will ask you for some permission. Type "Y")
And then it compiles and runs normally using the terminal.
Sometimes you will also need to install a package to get javac.
Example:
sudo apt-get install openjdk-7-jdk
The java command works right of the box. You may java -version to see that it is Ubuntu running. You can also locate the java binary and see that it is amd64. That is all you need to know to download jdk manually from Oracle.com, like
wget --no-check-certificate --no-cookies --header "Cookie: oraclelicense=accept-securebackup-cookie" http://download.oracle.com/otn-pub/java/jdk/8u92-b14/jdk-8u92-linux-i586.tar.gz
tar xf jdk-8u92-linux-i586.tar.gz
and voila, jdk-1.8.0_92/bin/java(c) can be executed! It allows you to compile java files.

Execute an EXE after being downloaded from JAVA Code

I already have a working code to download certain file from my server. This file is in an executable (a patch installer). I would like to know if it is possible to launch the executable after my java application has downloaded in its own directory. If it is, may I pleas know how?
Also, I know I am asking for too much here, but is it possible to then delete this installer from the computer after it has installed the patch?---I've been working on this but have stumbled upon a lot of nothing...
Thanks a lot in advance!
Look at the java docs for the class RunTime, this should allow you to run an executable providing you have permissions to do so.
Runtime.getRuntime().exec("command") will execute the input as if you entered it in the command line. So, if you know the path of the exe file (which you should, since you downloaded it), you can launch it like this:
Runtime.getRuntime().exec(executablepath);
Normally you can delete a file by using File.delete(), but in this case the command will still be running if you add delete() right after exec() and it won't be able to delete. Hope that helps!

VLCJ: Packaged Jar cant load libvlc library when double-clicked but runs perfectly through console

I have a problem with vlc.I am using Eclipse and have made a small application to play video files using vlcj framework.Inside Eclipse everything works fine but when I make jar executable file through Eclipse and double-clicking it (on Windows 64: Tested only on this platform) nothing happens .Then I'm trying using "cd myjarfilepath" and then "java -jar myjarname.jar" and everything works fine.Why this is happening and how to solve it?
Thanks in advance to stackoverflow's community .
Edit:
In Path Location: C:\Users\user\Documents\31\Latest_Win64\Needed I have the following files:
plugins(directory)
axvlc.dll
axvlc.dll.manifest
libvlc.dll
libvlc.dll.manifest
libvlccore.dll
npvlc.dll
npvlc.dll.manifest
vlc.exe.manifest
In my classpath i have included the following files:
jna-3.5.1.jar
platform-3.5.1.jar
vlcj-2.2.0.jar
vlcj-2.2.0-javadoc.jar
vlcj-2.2.0-sources.jar
This sounds like an issue loading the needed VLCj files. Not only are dlls required but the plugin directory is also required. As the execution location moves around from inside eclipse to back in the file system, the path to these files may be getting set incorrectly.
It sounds like you have it working within eclipse. This would indicate to me that what you have should work. Firstly, I would recommend acquiring the VLC dlls, etc., by providing this as a VM argument such as shown below, rather that setting it in code. Remember to comment out any loading you are doing in your code so that you are relying on the command line argument.
-Djna.library.path={Application Location}/lib
example: -Djna.library.path=C:/myapp/lib
Directory Contents:
lib
-- libblc.dll
-- libvlc5.dll
-- {etc}
/plugins
/3dnow
/access
{etc}
If you are still having issues, add the following argument to put VLCj into debug mode. This can be very helpful to determine where things are failing.
-Dvlcj.log=DEBUG
Hope that helps.

Executable jar file won't run after doing some updates

I am working on Java project (A terraria like sandbox game) and I have come across something that I just don't understand. I have been exporting the project as .jar's as I go along to upload and share with my friends, but the last time I did this, the .jar won't run, but when I run it in eclipse, it runs fine. When I try to run the jar all it does is lose focus on the explorer window for a split second, then goes back. I have tried many things, like changing the main class, changing the META, and moving the files to specific packages, but nothing is working.
The project is 9 classes, so I won't paste all the code for it here, but the latest, non-working source code can be found here (http://www.mediafire.com/?fiw6wq73j7cff4t), the non-working jar here (http://www.mediafire.com/?bhjo162oh3hi2j9), and the working jar here (http://www.mediafire.com/?h918s8xpyxw4psr). If someone with more experience in java could please take a look at this it would be much appreciated.
One of the things I tried was not adding the JPanel (the game), and just leaving the splash screen, and the jar worked. This shows that it must be a problem in the coding somewhere.
Thanks for the time,
I.D.
Couldn't find message comes from your ReadMap class. So check which circumstance leads to this error. Most likely you did not add all classes or other files to your JAR, so file is not found. Check that all needed files are added to exported JAR in Eclipse export dialog. It's also a good idea to provide more debugging info (stacktrace is ok), especially when you are studying. BTW, cool game :)

Categories