Editing a manifest With java - java

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.

Related

Jar File: no main manifest attribute, in file.jar

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

Can't find or load main class, in a .jar file

Been looking for around 2 hours for a solution to my problem, tried countless solutions and still the problem remains unsolved.
I have a game project with few packages; geometry, levels, decoration, and game. Inside the game package resides a gameRun.java
package game;
public class gameRun {
/**
* Program entry point.
* #param args the arguments for the levels
*/
public static void main(String args[]) {
... some code
}
}
and I have the following manifest file :
Main-Class: game.gameRun
Class-Path: someExternalGraphicTool.jar
//newline here
I compile my whole game using the following command : (the bin folder already exists).
javac -d bin src/geometry/*.java src/decoration/*.java src/levels/*.java src/game/*.java
I then turn my compiled classes into an executable .jar file using the command :
jar cvfm t.jar manifest.mf bin/geometry/*.class bin/decoration/*.class bin/levels/*.class bin/game/*.class
I then try to run my t.jar using :
java -jar t.jar
//or
java -cp .:t.jar game.gameRun
which both produce the error, "Error: Could not find or load main class game.gameRun"
However, when I unzip the jar file, inside the bin/game directory I can see a gameRun.class file and inside the META-INF\MANIFEST.MF file I can still see that the Main-Class is set to game.gameRun.
Note that I am working on a remote linux server, on a command line, and the jar file doesn't work even if I download it to my windows 10 machine.
What did I do wrong during this process ? Thanks for any help.
Your classes are in a wrong structure inside a jar file, because of the bin folder. My suggestion: pack it all into a jar starting in a bin folder.
Now when you extract your jar you will see 2 folders: META-INF and bin.
If you make the jar from bin folder you will see: META-INF and game, and it will work.
It doesn't work for you simply, because it can't find the main class, since it is inside the bin/game/YourClass.class, not in game/YourClass.class.
...\bin> jar cvfm t.jar manifest.mf geometry/*.class decoration/*.class levels/*.class game/*.class
And then just:
...\bin> java -jar t.jar

IntelliJ: Why can't I use a class from external jar

When I created the jar file I wrote the following java file:
package myjar;
public class MyClass {
public static void hello() {
System.out.println("hello");
}
public static void main(String[] args) {
MyClass.hello();
}
}
I named the file MyClass.java.
I created a class file by typing "javac src/main/java/myjar/MyClass.java"
I generated a jar file by the command:
"jar cvf myjar.jar src/main/java/myjar/MyClass.class"
Now, I took the jar file and added it to Project Structure. The name of the jar is 'myjar':
and in the IDE I wrote "import myjar.MyClass" and the word 'myjar' was marked in red.
When I use 'MyClass.hello()' in the project, I get an error:
no main manifest
Do you know what I can to to load it successfully ?
You can only import some classes from your libraries on classpath into a Java source file, not a contents of a whole Java archive (JAR) file. So if you have inside myjar.jar file class Foo.class inside package (folder) bar, you can do
import bar.Foo;
After adding the .jar as a library in IntelliJ, you still need to add it to the module.
Then the library will appear in the module's list of dependencies.

Trying to run jar file from command line using classpath(for other libs) raises "Could not find or load main class", on Windows

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.

Running a Java Program from jar file not working if another package is used, classpath set

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.

Categories

Resources