I was building a jar file featuring one class, Main. It was based off Main.java:
public class Main {
public static void main(String[] args) {
System.out.println("hello jar file:-/");
}
}
I compiled with this command:
javac Main.java
Made this manifest.txt:
Main-Class: Main
And finished with:
jar cfm myfirstjarfile.jar manifest.txt Main.class
When I run java -jar myfirstjarfile.jar I get this error:
no main manifest attribute, in myfirstjarfile.jar
I was then told to put manifest in META-INF and call it MANIFEST.MF but I can't compile it, so what command do I use to turn this into a jar?
Anyone know why? Thank you!
I figured it out.
jar cvfe main.jar Main Main.class
Related
I've been trying to run a app from a Jarfile, but it keeps printing out:
"cannot find or load main class ...".
I tried to solve this problem using infos from this thread but all seemed to be useless. To be honest, I'm getting desperate because of the fact that this is such a trivial problem.
Anyways, what I did:
Main-Class: com.test.Test
my manifest attribute:
jar cfm test.jar manifest.txt <full_path>/out/com/test/*.class
which is what's packaged into the jar file (the Test.class file)
The Test class:
package com.test;
public class Test {
public static void main(String[] args) {
System.out.println("Hello World");
}
}
You should package the class correctly. Do
jar cfm test.jar manifest.txt com/test/*.class
in the parent folder of the folder com. By providing the absolute path (the way you did), the class file is packaged incorrectly.
Firstly, just in case you are using Eclipse IDE, there is some tools like the Fat Jar Plugin which are able to help you to package your build.
Secondly, there is Maven, to handle your dependencies, and build the package you need with everything ok. In your case, I will look for the Apache Maven Jar Plugin.
Finally, the old school way to go with the commandline, as you tried to do.
As Eliott Frisch has said in your question comments, you don't need to provide the fullpath to the mainclass inside your jar cfm test.jar manifest.txt <full_path>/out/.
And what's because the manifest.txt already give the package information!
I'm trying to run a jar file from command line on Windows using:
java -cp .;C:\java\empacotadoJars\Empac.jar;C:\java\empacotadoJars\ClienteEmpacotado.jar ClienteEmpacotado
It raises the exception:
Could not find or load main class ClienteEmpacotado
The classes are:
public class Empacotado{
public static void escrever(){
System.out.println("Chamndo metodo de classe Empacotado!");
}
}
public class ClienteEmpacotado{
public static void main(String args[]){
Empacotado.escrever();
}
}
Empacotado.class is inside Empac.jar and ClienteEmpacotado.class is inside ClienteEmpacotado.jar. I first zipped each one and then renamed to jar extension. Inside ClienteEmpacotado.jar I created META-INF folder with MANIFEST.MF file, which contains:
Manifest-Version: 1.0
Main-Class: ClienteEmpacotado
What might be wrong?
The problem was that I had used Winrar to make the zip files (then I renamed to jar extension). Using "jar cvf Package.jar Arq.class" to make the jars solved.
I'm trying to figure out how an existing Java program (I did not make myself ofcourse) was compiled with existing jars
I have Test.java (original source file):
package Demo;
//import classes from jars here etc...
public class Test {
public static void main(String args[]) {
etc...
}
}
Now I have two other jars:
file1.jar
file2.jar
Demo.jar
There is a batch script to run it:
#echo off
set CLASSPATH="file1.jar";"file2.jar";"Demo.jar"
java -cp %CLASSPATH% Demo.Test
This WORKS, but now I need to change the source file Test.java, recompile and run with the jars class dependencies. (sorry if I'm not making sense)
Now, I have tried to recompile this to reproduce same results with no luck:
javac -cp file1.jar;file2.jar;Demo.jar Test.java
defined manifest:
manifest.mf
Main-class: Demo.Test
Created directory "store" for class files and moved class files there
Ran:
jar -cmf manifest.mf Demo.jar store
Which created the "Demo.jar"
Then I ran the run the batch script above but not the same results (doesn't work at all)
Any help would be appreciated. Thank you!
It is difficult to create true executable jars as soon as you rely on external jars.
The only solution here is to :
put all jars in the same folder : yours and its dependencies
add a classpath entry inside your manifest
launch the jar using java -jar Demo.jar
The manifest will have to look like :
manifest.mf
Main-class: Demo.Test
Class-Path: file1.jar file2.jar
I have just started my first Java program. I have created a batch file to run my command line arguments. In this program I am writing a "HelloWorld" string with HelloWorld.java
class HelloWorld
{
public static void main(String [] args)
{
System.out.println("HelloWorld!!");
}
}
Now after I added java to my path I use the following in the .bat file.
javac HelloWorld.java //to complie
java HelloWorld //toRun
jar -cvf HelloWorld.jar HelloWorld.class //creating a archive
jar -xf HelloWorld.jar //Extracting and finding the manefest
From my jar file I now have to edit the manifest file and set the class I have just written as my main class for the jar file and I have done it as follow:
jar -cmf HelloWorld.jar Manifest.txt HelloWorld.class //Here is the problem
java -jar HelloWorld.jar //here I am trying to run it
pause
I cant seem to edit the manifest. Do I have a Command prompt error or did I mistype something?
The Manifest is a file named "MANIFEST.MF" in the META-INF directory. So, you need to have your final code look like this on the filesystem:
HelloWorld.class
WEB-INF
MANIFEST.MF
Then jar that up in its entirety.
Finally, you should also put your HelloWorld.class in to an actual package:
package pkg;
public class HelloWorld {
public static void main(String args[]) {
System.out.println("Hello World!");
}
}
Then your final product will look like:
pkg
HellowWorld.class
META-INF
MANIFEST.MF
And your final class name will be pkg.HelloWorld.
Things simply work better with actual packages in Java, it's best to use them right away and avoid trouble.
Note: Oh my mistake, I didn't catch the use of the -m flag -- So used to larger projects where the Manifest is just part of the build artifact.
I am importing one class(it is not there in the default jvm) from one jar package and using it in another package. like I have a class program1.class in package package1 and I am importing this class from another programme program2 in package package2.
package package1;
public class Program1
{
public String sayHello()
{
return "Hello world";
}
}
and importing this class in
package package2;
import package1.Program1;
public class Program2
{
public static void main(String args[])
{
System.out.println("I am in programme2");
System.out.println("I am in programme 1"+new Program1().sayHello());
}
}
I have compiled the program1 and packaged it by
javac -d . Program1.java
jar cf Pack1.jar package1
and second programme by
javac -d . Program2.java
jar cfm Pack2.jar Manifest.txt package2
my manifest file is
Main-Class: package2.Program2
now I am running the programme as
java -classpath path/to/Pack1.jar -jar Pack2.jar
it is giving me error as:
I am in Program2
No class def found error Package1/Program1
If I am running it by specifying the Class as
java -classpath path/to/Pack1.jar;path/to/Pack2(UnJar'ed) Pack2.program2
it is working which is very strange
Means there is a different between specifying the class file containing the main and specifying the jar file of the program.
I have already made sure that I have set Main-Class is set correctly in Manifest.mf, moreover the classpath for pack2 is also specified
So why this error
When you use the -jar option, all other classpath options are ignored. Thats why you are getting a NoClassDefFoundException.
See link here.
Here is the relevant note from that link --
-jar Execute a program encapsulated in a JAR file. The first argument is the name of a JAR file instead of a startup class name. In order for this option to work, the manifest of the JAR file must contain a line of the form Main-Class: classname. Here, classname identifies the class having the public static void main(String[] args) method that serves as your application's starting point.
See the Jar tool reference page and the Jar trail of the Java Tutorial for information about working with Jar files and Jar-file manifests. When you use this option, the JAR file is the source of all user classes, and other user class path settings are ignored.
The fix would be to include the Class-Path in your jar manifest.