How to run Java .jar without MANIFEST.MF? - java

Is it possible to run a Java app which doesn't contain MANIFEST.MF file? Of course, there's static main method,just lacks manifest file. And the app is depending on several external .jar files.
If is this possible, how to do that?

It is possible, you can specify the class to run from the command line:
java -cp yourJar.jar your.main.Class
Same question here:
How to run a class from Jar which is not the Main-Class in its Manifest file

You can also add the manifest, with the following command:
jar -uvfe your.jar foo.bar.Baz
java -jar your.jar # tries to run main in foo.bar.Baz

Of course! Just use this:
java -cp MyJar.jar com.example.Main

Yes , use this
java -cp myJar.jar package.className

Related

Can't specify class path in Java

I have a Test.jar file with ru.package.Tester class in it. When I run the following command in the directory with my jar:
java -classpath /path/to/current/directory ru.package.Tester
I get the following error message:
Could not find or load main class
I'm running on OS X
upd: When I put my jar file into /Library/Java/Extensions, it all works without specifying -classpath.
You need to give path to jar
java -classpath /path/to/current/directory/your.jar ru.package.Tester
could you please check your ~/.bash_profile - what does it contain . I too faced the similar issue on my mac yesterday and got it resolved.
Forget about the classpath. By default, Java looks for the classes directly in the directory. If you want to run a jar, you must tell Java to do so:
java -jar your.jar
Please check manifest file. it must contain main class name in manifest.

Ini4j classpath no class

i am want create jar of my project using cmd line.
I am using ini4j libs.
Everything compiles fine, but I do not know how to set the -cp to the library.
Compile:
javac -cp ".;lib/ini4j-0.5.2.jar;ini4j-0.5.2-jdk14.jar;lib/ini4j-0.5.2-jdk14.jar" gui_Frame/*.java
Create jar:
echo Main-class: gui_Frame/MainApp > manifest.txt
jar cvfm GVE.jar manifest.txt gui_Frame/*
But, if i want start java -jar GVE.jar i get following error:
Java.lang.NoClassDefFoundError: org/ini4j/wini
What am I doing wrong ?
You must specify the same classpath when running java as you did when compiling.
Or bundle all the necessary class files in one JAR.
==EDIT==
Try this:
java -cp "GVE.jar;.;lib/ini4j-0.5.2.jar" gui_Frame.MainApp

How to run jar file from SVN pre-commit hook with dependencies and then pass parameter?

I am working with pre-commit hook developed in java. Now i need to run it inside pre-commit.
List item
How can i run jar file with dependency?
Do I have to make executable jar file to run from pre-commit?
How to give classpath and parameter to jar file?
Currntly i am ruuning in this way inside pre-commit which is working fine. Now i need to move it to production
/System/Library/Frameworks/JavaVM.framework/Versions/1.6/Home/bin/java -Xdebug -Xrunjdwp:transport=dt_socket,server=y,suspend=n,address=11111
$JAVA_OPTS -Dfile.encoding=MacRoman
-classpath
/System/Library/Frameworks/JavaVM.framework/Versions/1.6/Home/lib/deploy.jar:
/System/Library/Frameworks/JavaVM.framework/Versions/1.6/Home/lib/dt.jar:
/System/Library/Frameworks/JavaVM.framework/Versions/1.6/Home/lib/javaws.jar:
/System/Library/Frameworks/JavaVM.framework/Versions/1.6/Home/lib/jce.jar:
/System/Library/Frameworks/JavaVM.framework/Versions/1.6/Home/lib/jconsole.jar:
/System/Library/Frameworks/JavaVM.framework/Versions/1.6/Home/lib/management-agent.jar:
/System/Library/Frameworks/JavaVM.framework/Versions/1.6/Home/lib/plugin.jar:
/System/Library/Frameworks/JavaVM.framework/Versions/1.6/Home/lib/ext/sunjce_provider.jar:
/System/Library/Frameworks/JavaVM.framework/Versions/1.6/Home/lib/ext/sunpkcs11.jar:
/Users/testuser/Documents/Dev/projects/SCS/sc/SCS/SCS/SCSAPI/classes:
/Users/testuser/Documents/Dev/projects/SCS/sc/g3/_lib/hibernate-tools.jar:
/Users/testuser/Documents/Dev/projects/SCS/sc/g3/_lib/dom4j-1.4.jar:
/Users/testuser/Documents/Dev/projects/SCS/sc/g3/_lib/commons-logging.jar:
/Users/testuser/Documents/Dev/projects/SCS/sc/g3/_lib/commons-collections-2.1.1.jar:
/Users/testuser/Documents/Dev/projects/SCS/sc/g3/_lib/mysql-connector-java-5.1.22-bin.jar:
/Users/testuser/Documents/Dev/projects/SCS/sc/g3/_lib/hibernate2.jar:
/Users/testuser/Documents/Dev/projects/SCS/sc/g3/_lib/ehcache-1.1.jar
com.ticoon.scs.SCSMain
pre-commit file -c /Users/testuser/Documents/Dev/projects/SCS/sc/SCS/SCS/config.xml -v -repos $REPOS -trxn $TXN
Do I have to make an executable Jar file?
You don't need to create a runnable Jar, but that is advised because it make things easier.
Basically if you run a Jar file, you have to specify the class containing the main() method:
java -jar myjar.jar x.y.z.Main
If you have a Main-Class: x.y.z.Main attribute defined in your MANIFEST.MF file, you can run the Jar file by simply:
java -jar myjar.jar
How to give classpath to a Jar file?
When you run something with the java -jar ... command, the -classpath ... property is dropped. The reason is security (Jar files can be digitally signed to ensure they weren't modified and it would be so easy to make java load a different dependency Jar file with the same content but with hijacked functionality).
The solution is simple: include the Class-Path: ... attribute too in your MANIFEST.MF file as you do with Main-Class: ....
How to give parameters to a Jar file?
As you would give parameters to a simple Java program (and the manual specifies it):
java -jar myjar.jar arg1 arg2 ... argN
Yehh its working now with single file. thanks #rlegendi
Working copy
REPOS="$1"
TXN="$2"
java -jar /Users/testuser/Documents/SCS/SCS.jar pre-commit file -c /Users/testuser/Documents/SCSAPI/config.xml -repos $REPOS -trxn $TXN

How to run Java program in command prompt

I created a Java project to call a Web service.
It has one Main java file and another class file.
I have used some jar files for HTTP client.
In Eclipse it runs fine.
I need to run the Java program in command prompt by passing some arguments.
In command prompt I went to src folder containing main java and sub class java file and gave the following command
javac mainjava.java
I'm getting following error
mainjava.java:14: cannot find symbol
symbol : class SubClass
here SubClass is my another java class file used to call the web service.
How to run the program by passing arguments?
javac is the Java compiler. java is the JVM and what you use to execute a Java program. You do not execute .java files, they are just source files.
Presumably there is .jar somewhere (or a directory containing .class files) that is the product of building it in Eclipse:
java/src/com/mypackage/Main.java
java/classes/com/mypackage/Main.class
java/lib/mypackage.jar
From directory java execute:
java -cp lib/mypackage.jar Main arg1 arg2
A very general command prompt how to for java is
javac mainjava.java
java mainjava
You'll very often see people doing
javac *.java
java mainjava
As for the subclass problem that's probably occurring because a path is missing from your class path, the -c flag I believe is used to set that.
You can use javac *.java command to compile all you java sources. Also you should learn a little about classpath because it seems that you should set appropriate classpath for succesful compilation (because your IDE use some libraries for building WebService clients). Also I can recommend you to check wich command your IDE use to build your project.
All you need to do is:
Build the mainjava class using the class path if any (optional)
javac *.java [ -cp "wb.jar;"]
Create Manifest.txt file with content is:
Main-Class: mainjava
Package the jar file for mainjava class
jar cfm mainjava.jar Manifest.txt *.class
Then you can run this .jar file from cmd with class path (optional) and put arguments for it.
java [-cp "wb.jar;"] mainjava arg0 arg1
HTH.
javac only compiles the code. You need to use java command to run the code. The error is because your classpath doesn't contain the class Subclass iwhen you tried to compile it. you need to add them with the -cp variable in javac command
java -cp classpath-entries mainjava arg1 arg2 should run your code with 2 arguments

Running jar from different directory cannot find required dependency

I'm trying to run a jar ec/mobat/MOBAT.jar which depends on some jars located in ec/mobat/lib/. It works if I do:
ec/mobat/$ java -jar MOBAT.jar
However I want to be able to run the jar from another directory
ec/$ java -jar mobat/MOBAT.jar
But I get an exception
java.lang.NoClassDefFoundError: ibis/io/Serializable
...
I tried to pass the required jars in the classpath
ec/$ CLASSPATH=... java -jar mobat/MOBAT.jar
ec/$ java -jar -cp ... mobat/MOBAT.jar
but I get exactly the same exception. Any fix?
Update: MANIFEST.INF contains the following:
Manifest-Version: 1.0
Ant-Version: Apache Ant 1.7.0
Created-By: Selmar Kagiso Smit
Main-Class: mobat.Launcher
Implementation-Version: 1.3.4
The classpath has to contain every jar you're depending on.
java -classpath b.jar;c.jar -jar a.jar //does not work see below
The ";" is system dependent for windows ":" for unix.
The jar switch is used to select the jar file whose main class is executed (Main-Class: mobat.Launcher in the manifest file). The command line:
java -classpath b.jar;c.jar;a.jar mobat.Launcher
Would produce the same result.
Alternatively classpath definitions can be added to the Manifest file. Your manifest file could contain the attribute.
Class-Path: lib/b.jar lib/c.jar
Then
java -jar a.jar
would work.
Edit:
I thought that -jar and -cp could be used together. But the java tools documentation is clear:
-jar
When you use this option, the JAR file is the source of all user
classes, and other user class path
settings are ignored.
Only the manifest and everything explict (classpath and main class) versions work.
You can not use -cp and -jar together
java -cp myjar.jar;lib/*;. mypackage.MyClass
shall work on windows and
java -cp myjar.jar:lib/*:. mypackage.MyClass
shall work on Unix

Categories

Resources