Jar file execution error - jar

I wrote a program that runs fine, however packaging the file into a jar file and trying to execute that fails.
The project consists of three files :
CinReader.java --> CinReader.class
PageSource.java --> PageSource.class
LocalBot.java --> LocalBot.class (main class)
The contents of manifest.txt are :
Main-Class: LocalBot.class
I created the jar file with the command :
C:\Users\ioil\Desktop\CSCI20\localBot>jar cvfm LocalBot.jar manifest.txt *.class
added manifest
adding: CinReader.class(in = 5908) (out= 3027)(deflated 48%)
adding: LocalBot.class(in = 362) (out= 272)(deflated 24%)
adding: PageSource.class(in = 5293) (out= 2560)(deflated 51%)
C:\Users\ioil\Desktop\CSCI20\localBot>
Now when I enter the command : "java -jar LocalBot.jar" or "LocalBot.jar" the Java Virtual Machine Launcher returns the error message : "Could not find the main class: LocalBot. Program will exit"
Am I doing anything obviously wrong . . .

Take a look at Understanding the Manifest
When you create a JAR file, it automatically receives a default
manifest file. There can be only one
manifest file in an archive, and it
always has the pathname
META-INF/MANIFEST.MF

Related

Why does `getResourceAsStream` sometimes load a resource even when there is a typo in the resource path?

I have a Jar (we'll call it a.jar) with a resource in it at path foo/bar.txt and a function as follows:
object FooBarLoader {
fun loadFooBarText() = javaClass.getResourceAsStream("foo//bar.txt")
?.bufferedReader()
?.readLines()
?.joinToString("\n")
}
When I test the function in a unit test (JUnit 4, running with Gradle 6), it loads the text from the resource file despite the obvious typo (the // in the middle of the resource path).
I also have a CLI application (in b.jar) that has a dependency on a.jar. When the CLI application calls loadFooBarText(), it got a null result due to the resource not being found. This was fixed by fixing the typo (// -> /) in the function in a.jar. No other changes were needed to fix it.
So, my question is why did the wrong path work in one situation (unit tests of a.jar) and not the other (call from b.jar)?
How do you run the unit test with a.jar ? Just run it in your IDE or use command java -jar a.jar ?
If you ran it just in IDE,I think difference is the search path between local files and zip files .
Your first application searches the file in your target directory and the second application searches it in the jar which is a compressed file.
When searching files in local path, command will be changed to right one by system.
The two commands below are the same in both Windows/Linux.
cd work//abc/ddd
cd work/abc/ddd
But when searching files in a jar file which is actually compressed zip file, path should be a restrict written or else the program will find nothing.

Error while running JavaFX from terminal - Could not find or load main class

Need some clever advice to solve this - well, some kind of problem while using simple JavaFX app.
However, there was some problems while all is set-up (module path, for example), but what now when I just cannot run .jar file from terminal? Ok, start talk about mission-impossible-with-javafx:
I got Apache NetBeans 12 installed on Linux Mint 20. JDK is already installed, so 'java -version' returns 'openjdk version "11.0.7" 2020-04-14'...
When start project there is no problem at all - JavaFX app works.
I need this app to work on Windows 8.1, but before that I tried to run this .jar file on my linux terminal using java -jar MyFile.jar.
Here the problem starts: it said no main manifest attribute, in MyFile.jar.
Then I decide to set the main class directly in jar file (opened using Archive Manager):
MyFile.jar -> META-INF -> MANIFEST.MF and add on last line: Main-Class: mypackage.MyClass
Now I have: Error: Could not find or load main class mypackage.MyClass Caused by: java.lang.NoClassDefFoundError: javafx/application/Application
Ok, how and why?
Just to said that in NetBeans I also set main class in:
Project -> Properties -> Run -> Main class: mypackage.MyClass
and in VM options I added:
--module-path "/locationToJavaFX/javafx-sdk-11.0.2/lib" --add-modules javafx.controls,javafx.fxml
In pom file, main class is also set (generated by IDE):
// other lines
<groupId>org.openjfx</groupId>
<artifactId>javafx-maven-plugin</artifactId>
<version>0.0.4</version>
<configuration>
<mainClass>mypackage.MyClass</mainClass>
</configuration>
// other lines
and module-info looks like:
module mypackage {
requires javafx.controls;
requires javafx.fxml;
requires org.jsoup;
opens mypackage to javafx.fxml;
exports mypackage; // some people advice without this line, I tried but same problem
}
Just want to be clear - I tried probably everything (I don't count) that is available here on stackoverflow. I try to fix this yesterday and today (almost 2 whole days) and I can't.
Just a thought: Java programming is not so difficult as configuration is.
Cheers and thanks for helping!
EDIT:
Just to said that I tried what José suggested : first creating launcher class, follow 'fat jar' step-by-step then run 'semi-fat jar' command which he added to the post. Interestingly enough, this return me: Error occurred during initialization of boot layer java.lang.module.FindException: Module javafx-fxml not found. Why not found javafx-fxml if I aready defined it as said above in question? Also, why Netbeans doesn't provide me the main class in manifest (in jar) file? When I add main class in manifest, I get this:
Exception in thread "main" java.lang.NoClassDefFoundError: javafx/application/Application
at java.base/java.lang.ClassLoader.defineClass1(Native Method)
at java.base/java.lang.ClassLoader.defineClass(ClassLoader.java:1017)
at java.base/java.security.SecureClassLoader.defineClass(SecureClassLoader.java:174)
at java.base/jdk.internal.loader.BuiltinClassLoader.defineClass(BuiltinClassLoader.java:800)
at java.base/jdk.internal.loader.BuiltinClassLoader.findClassOnClassPathOrNull(BuiltinClassLoader.java:698)
at java.base/jdk.internal.loader.BuiltinClassLoader.loadClassOrNull(BuiltinClassLoader.java:621)
at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(BuiltinClassLoader.java:579)
at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(ClassLoaders.java:178)
at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:522)
at Main.main(Main.java:5) Caused by: java.lang.ClassNotFoundException: javafx.application.Application
at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(BuiltinClassLoader.java:581)
at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(ClassLoaders.java:178)
at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:522)
... 10 more
Ok, how Application does not found? :)
EDIT 2:
Yeap, mister José has a right. This command in his 'semi-fat jar' post actually works BUT not for Maven (I use it on same Ant project and it works). Maven somehow cannot recognize JSoup class (even I add it in dependency) and java returns:
Exception in Application start method Exception in thread "main" java.lang.RuntimeException: Exception in Application start method
at javafx.graphics/com.sun.javafx.application.LauncherImpl.launchApplication1(LauncherImpl.java:900)
at javafx.graphics/com.sun.javafx.application.LauncherImpl.lambda$launchApplication$2(LauncherImpl.java:195)
at java.base/java.lang.Thread.run(Thread.java:834) Caused by: java.lang.NoClassDefFoundError: org/jsoup/Jsoup
at p.eis.GetData1.startIt(GetData1.java:34)
at p.eis.GetData1.<init>(GetData1.java:28)
at p.eis.SampleController.initialize(SampleController.java:37)
at javafx.fxml/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2573)
at javafx.fxml/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2466)
at javafx.fxml/javafx.fxml.FXMLLoader.load(FXMLLoader.java:2435)
at p.eis.Start.loadFXML(Start.java:32)
at p.eis.Start.start(Start.java:19)
at javafx.graphics/com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$9(LauncherImpl.java:846)
at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runAndWait$12(PlatformImpl.java:455)
at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runLater$10(PlatformImpl.java:428)
at java.base/java.security.AccessController.doPrivileged(Native Method)
at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runLater$11(PlatformImpl.java:427)
at javafx.graphics/com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:96)
at javafx.graphics/com.sun.glass.ui.gtk.GtkApplication._runLoop(Native Method)
at javafx.graphics/com.sun.glass.ui.gtk.GtkApplication.lambda$runLoop$11(GtkApplication.java:277)
... 1 more Caused by: java.lang.ClassNotFoundException: org.jsoup.Jsoup
at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(BuiltinClassLoader.java:581)
at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(ClassLoaders.java:178)
at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:522)
... 17 more
Hope I'll find solution for Maven projects in the near future.
Yeah I noticed you use maven but it's not fault of maven, I know why this happen and i have solution about it so in my solution there is magic and work fine because i use my solution to make jar and you know my jar file run without terminal(means double clicking on jar file)
Solution
you need winRar because i use it for info read my solution!
Just follow below step with images to run jar(both maven and gradle project)
solution of Error: Could not find or load main class mypackage. this line
you created main class at this location /src/main/java/YOUR_PACKAGE_NAME/Main.java so while creating jar file intellij couldn't care your main class if you create main class at above location and it doesn't add Main-Class: Main this line in Manifest File Solution is that create one more main class(don't delete Main.Java class we use this class in new one main class) at this location /src/main/java/EntryPoint.java see below image and code what i did
EntryPoint.java is new main class (IMAGE)
public class EntryPoint {
public static void main(String[] args) {
smartSeizeing.Main.main(args);
}
}
Main.java Image
Main.Java (IMAGE)
After creating EntryPoint.java(New Main Class) Manifest File For Jar File, clicking Cntr+Alt+Shift+S to create Manifest File | Jar File, during creating jar file(before build jar) Just select EntryPoint.Java As Main Class Below Image
Main Class : EntryPoint (IMAGE)
Before click Ok button change the location of manifest file create it at this location /src/META_INF/manifest.mf(Wrong location : /src/main/META_INF/menifest.mf) and click Apply button to save changes
Location Of Manifest File like below image
Just Manifest.MF File (IMAGE)
Build Jar file go to Option[Build->Build-Artifacts...] and take 3-5 min to create jar.
After Creating Jar file Please Read clearly this main step means
Means Solution Of This line of NOT FOUND FXML FILE this is tricky because all fxml file are stored at resource directory so while creating jar so intellij doesn't add the any file from resource directory again not include fxml file while creating jar
Solution Of How to include FXML FILE NOW WE HAVE TO USE WIN_RAR Software To Add This File And Believe Me Its work and not affect any file and work file Just Follow below step to add FXML FILE
Adding FXML FILE STEP
close Intellij
Go to your project Directory in my case C:\Users\ADMIN\IdeaProjects\YOUR_PROJECT_NAME and Go to YOUR_PROJECT_NAME\out\artifacts\YOUR_PROJECT_NAME\YOUR_PROJECT_NAME.Jar so u find jar file of your project
Just click right key of Mouse for option, select option of EXTRACT HERE(WinRar Option)
JUST I AM ExtractING MY JAR (IMAGE)
DELETE ONLY JAR FILE AFTER EXTRACTING Like my file name smartSeizeing.jar i delete it (IMAGE)
After extracting and deleting jar(only jar file) file so OPEN YOUR_PACKAGE_NAME File in My case C:\Users\ADMIN\IdeaProjects\SmartSeizeing\out\artifacts\SmartSeizeing_jar\smartSeizeing
Keep Open It And Open Your Project directory and copy all file(including all file like fxml, css, html, hibernet file if u have other file to with fxml) from resource directory and Paste all file(copied Files) in that previously open folder(extract jar folder like IdeaProjects\YOUR_PROJECT_NAME\out\artifacts\YOUR_PROJECT_NAME_jar\YOUR_PACKAGE_NAME) And after coping file so and just make jar file select all file(IdeaProjects\YOUR_PROJECT_NAME\out\artifacts\YOUR_PROJECT_NAME_jar) and click right key of mouse, select add to archive(WinRar) and after creating YOUR_PACKAGE_NAME.jar file just double click that file and see the magic
How to Add fxml and how to create again jar(without any curreption) i mentioned in below image so u can easily understand what i did and also image of my OUTPUT
i pasted all file of resource directory of my project (IMAGE)
Please see below answer too because i can't add more link for step of how to make jar using winRar and that jar work fine and also my output
Best Of Luck
and You wanna video just tell me i make video about it How to make runnable jar without any exception or error!
this is because i can't add more links that's why i again wrote this
WinRar to create Jar File
Continue From Previous Answer and image that is https://i.stack.imgur.com/fY1tw.png
this is what i copy from Resource folder of my project
just paste of all file of resource directory at (in my case) C:\Users\ADMIN\IdeaProjects\SmartSeizeing\out\artifacts\SmartSeizeing_jar\smartSeizeing
WinRar to make Jar follow bellow step (IMAGE)
Step 1
Step 2 select zip option then change extension of your file .zip to .jar
Step 3
This is what i am trying to explain to you and this is my Output
i hope you understand what i do for creating jar file

Some questions about scripts in Gradle

I have to write the script in Gradle and I therefore have some questions.
The script must unpack the .ear file, and then included in the jar, then edit the content and save as ear (text file> jar> ear.)
1) Firstly, I would like to learn how to save a file to the current directory.
(The following code saves the file to another folder.)
task unzip(type: Copy) {
def zipFile = file('C:/Test/file.ear')
def outputDir = file('jar')
from zipTree(zipFile)
into getDestDir()
}
Phrases such as '.', '/' don't work.
2)Secondly, I would like to ask how to unpack the jar file because I can't unpack the above method. (It works only to the EAR)
3)At the end I would ask how you can convert the edited text file on the jar, and then on ear (without dependencies and manifest).
Because the resulting file do I have to file .ear
Thank you in advance for your answer.
You don't want to be writing files into your working directory. All the work should be done under the $buildDir.
A standard method is to set your into directory to a temporary location:
task myTask(type: Copy) {
from 'my/dir/'
into temporaryDir
}
You can unpack a JAR or ZIP file like so:
copy {
from zipTree('path/tozip.zip')
into temporaryDir
}

executing jar file

I'm trying to get my class "lzw.java" deployed as a jar file ,
I made a manifest.txt , compiled and got all .class files ,put the manifest file along with the created .class files , and entered the command
jar -cvmf manifest.txt lzw.jar *.class
a .jar was created , when I double click it , it gives me "Java Exception error has occured",
having tried to execute it through cmd it gave me " java.lang.NoClassDefFoundError
note : the manifest file contains just a "Main-Class: LZW" followed by new line character
Since you use lzw.java, class and LZW interchanging, I suspect it might be a spelling problem.
lzw.java is the source file, not a class. The class should have the name lzw in this case.
The class file should be lzw.class - if it is not, the name of your class will be like the file-name without dot-class. Of course you choose the right class name in the manifest file.
Post the whole errormessage, not just parts, if this isn't the source of your problem.
Btw.: I suspect you didn't use a package name for your class? And do you know the convention, to user Uperrcase for class names, which would be Lzw in your case - maybe LZW if it is a well established acronym.
This exception means that the JRE is unable to locate the main Java class you are trying to reach. Make sure you are providing the correct class path when running the command.

how to check the version of jar file?

I am currently working on a J2ME polish application, just enhancing it. I am finding difficulties to get the exact version of the jar file.
Is there any way to find the version of the jar file for the imports done in the class? I mean if you have some thing, import x.y.z; can we know the version of the jar x.y package belongs to?
Decompress the JAR file and look for the manifest file (META-INF\MANIFEST.MF). The manifest file of JAR file might contain a version number (but not always a version is specified).
You need to unzip it and check its META-INF/MANIFEST.MF file, e.g.
unzip -p file.jar | head
or more specific:
unzip -p file.jar META-INF/MANIFEST.MF
Just to expand on the answers above, inside the META-INF/MANIFEST.MF file in the JAR, you will likely see a line: Manifest-Version: 1.0 ← This is NOT the jar versions number!
You need to look for Implementation-Version which, if present, is a free-text string so entirely up to the JAR's author as to what you'll find in there.
See also Oracle docs and Package Version specificaion
Just to complete the above answer.
Manifest file is located inside jar at META-INF\MANIFEST.MF path.
You can examine jar's contents in any archiver that supports zip.
Each jar version has a unique checksum. You can calculate the checksum for you jar (that had no version info) and compare it with the different versions of the jar. We can also search a jar using checksum.
Refer this Question to calculate checksum:
What is the best way to calculate a checksum for a file that is on my machine?
Basically you should use the java.lang.Package class which use the classloader to give you informations about your classes.
example:
String.class.getPackage().getImplementationVersion();
Package.getPackage(this).getImplementationVersion();
Package.getPackage("java.lang.String").getImplementationVersion();
I think logback is known to use this feature to trace the JAR name/version of each class in its produced stacktraces.
see also http://docs.oracle.com/javase/8/docs/technotes/guides/versioning/spec/versioning2.html#wp90779
Thought I would give a more recent answer as this question still comes up pretty high on searches.
Checking CLi JAR Version:
Run the following on the CLi jar file:
unzip -p jenkins-cli.jar META-INF/MANIFEST.MF
Example Output:
Manifest-Version: 1.0
Built-By: kohsuke
Jenkins-CLI-Version: 2.210 <--- Jenkins CLI Version
Created-By: Apache Maven 3.6.1
Build-Jdk: 1.8.0_144
Main-Class: hudson.cli.CLI
The CLi version is listed above.
To get the Server Version, run the following:
java -jar ./jenkins-cli.jar -s https://<Server_URL> -auth <email>#<domain>.com:<API Token> version
(the above will vary based on your implementation of authentication, please change accordingly)
Example Output:
Dec 23, 2019 4:42:55 PM org.apache.sshd.common.util.security.AbstractSecurityProviderRegistrar getOrCreateProvider
INFO: getOrCreateProvider(EdDSA) created instance of net.i2p.crypto.eddsa.EdDSASecurityProvider
2.210 <-- Jenkins Server Version
This simple program will list all the cases for version of jar namely
Version found in Manifest file
No version found in Manifest and even from jar name
Manifest file not found
Map<String, String> jarsWithVersionFound = new LinkedHashMap<String, String>();
List<String> jarsWithNoManifest = new LinkedList<String>();
List<String> jarsWithNoVersionFound = new LinkedList<String>();
//loop through the files in lib folder
//pick a jar one by one and getVersion()
//print in console..save to file(?)..maybe later
File[] files = new File("path_to_jar_folder").listFiles();
for(File file : files)
{
String fileName = file.getName();
try
{
String jarVersion = new Jar(file).getVersion();
if(jarVersion == null)
jarsWithNoVersionFound.add(fileName);
else
jarsWithVersionFound.put(fileName, jarVersion);
}
catch(Exception ex)
{
jarsWithNoManifest.add(fileName);
}
}
System.out.println("******* JARs with versions found *******");
for(Entry<String, String> jarName : jarsWithVersionFound.entrySet())
System.out.println(jarName.getKey() + " : " + jarName.getValue());
System.out.println("\n \n ******* JARs with no versions found *******");
for(String jarName : jarsWithNoVersionFound)
System.out.println(jarName);
System.out.println("\n \n ******* JARs with no manifest found *******");
for(String jarName : jarsWithNoManifest)
System.out.println(jarName);
It uses the javaxt-core jar which can be downloaded from http://www.javaxt.com/downloads/
I'm late this but you can try the following two methods
using these needed classes
import java.util.jar.Attributes;
import java.util.jar.Manifest;
These methods let me access the jar attributes. I like being backwards compatible and use the latest. So I used this
public Attributes detectClassBuildInfoAttributes(Class sourceClass) throws MalformedURLException, IOException {
String className = sourceClass.getSimpleName() + ".class";
String classPath = sourceClass.getResource(className).toString();
if (!classPath.startsWith("jar")) {
// Class not from JAR
return null;
}
String manifestPath = classPath.substring(0, classPath.lastIndexOf("!") + 1) +
"/META-INF/MANIFEST.MF";
Manifest manifest = new Manifest(new URL(manifestPath).openStream());
return manifest.getEntries().get("Build-Info");
}
public String retrieveClassInfoAttribute(Class sourceClass, String attributeName) throws MalformedURLException, IOException {
Attributes version_attr = detectClassBuildInfoAttributes(sourceClass);
String attribute = version_attr.getValue(attributeName);
return attribute;
}
This works well when you are using maven and need pom details for known classes. Hope this helps.
For Linux, try following:
find . -name "YOUR_JAR_FILE.jar" -exec zipgrep "Implementation-Version:" '{}' \;|awk -F ': ' '{print $2}'
If you have winrar, open the jar with winrar, double-click to open folder META-INF. Extract MANIFEST.MF and CHANGES files to any location (say desktop).
Open the extracted files in a text editor: You will see Implementation-Version or release version.
You can filter version from the MANIFEST file using
unzip -p my.jar META-INF/MANIFEST.MF | grep 'Bundle-Version'
best solution that does not involve extracting the jar files is to run the following command. If the jar file does not contain a manifest file you will get a "WARNING: Manifest file not found"
java -jar file.jar -v
Just rename the extension with .zip instead of .jar. Then go to META-INF/MANIFEST.MF and open the MANIFEST.MF file with notepad. You can find the implementation version there.
It can be checked with a command java -jar jarname

Resources