I first tried to run my jar file by
java -jar test2.jar
and got the following error
"Failure to load main class Manifest attribute from test2.jar"
after googling stack overflow i came up with a solution saying i needed a manefest file with the nsme of the starting class.
I tried the following
jar cfm app.jar man.txt
Now nothing happens, after I type the line the curser just goes to the next line
man file looks like
Main-Class: cStart
my cstart looks like
public class cStart {
public static void main (String[] args) {
Scanner scanner = new Scanner(System.in);
cRiunTest test;
test=new cRiunTest();
test.run();
if (true)
return;
}
}
Jar command is to manipulate jar (java archive) files. To run a jar you should use the command java -jar program.jar
Related
I'm trying to understand the inclusion of third party jar files in a java project using only the command line in Windows 10.
Specifically, I try to include the file json-20200518.jar in my "project" so that I can use the java object JSONObject in the project.
My java file:
package com.mypackage.example;
import org.json.JSONObject;
class Example {
public static void main(String[] args) {
// ... program logic
}
}
location of my java file (Examp.java):
./com/mypackage/example
location of jar file:
./jars
using cmd win10 I compile:
javac -cp "C:\Users\pfort\Desktop\java\jars\json-20200518.jar" "C:\Users\pfort\Desktop\java\com\mypackage\example\Examp.java"
compilation is successful.
Run:
java -cp "C:\Users\pfort\Desktop\java\jars\json-20200518.jar" com.mypackage.example.Examp
I get a report:
Error: Could not find or load main class com.mypackage.example.Pokus
Caused by: java.lang.ClassNotFoundException: com.mypackage.example.Pokus
Second attempt:
java -cp "C:\Users\pfort\Desktop\java\jars\json-20200518.jar" "C:\Users\pfort\Desktop\java\com\mypackage\example\Pokus"
But the same error message comes back to me.
Where am I going wrong? Is it the wrong structure? I don't get it, the compilation is successful but the run does not work.
The compiled Examp.class file isn't part of json-20200518.jar, so you'll need to add the directory containing it to the command line. Assuming it's the current directory (.):
java -cp "C:\Users\pfort\Desktop\java\jars\json-20200518.jar;." com.mypackage.example.Examp
I am trying to use the javax.jms library: https://docs.oracle.com/javaee/7/api/javax/jms/package-summary.html, for which I have downloaded the jar: http://www.java2s.com/Code/Jar/j/Downloadjavaxjms111sourcesjar.htm
Specifically, I am trying to use the MessageListener and Message classes, which I know to be in there based on the Jar decompilation. I put this jar file next to my java file, so that the file structure looks like this:
myDir
-|jms.jar
-|Main.java
Main.java:
import javax.jms.MessageListener;
import javax.jms.Message;
public class Main {
public static void main(String[] args) {
MessageListener listener = new MessageListener() {
#Override
public void onMessage(Message msg) {
}
};
System.out.println("Hello World");
}
}
I can compile this using javac -cp jms.jar Main.java from inside myDir. This creates Main.class. However, when I run java Main, I get:
Exception in thread "main" java.lang.NoClassDefFoundError: javax/jms/MessageListener
This would lead me to believe that MessageListener is not included in the jar, but it is and the file structure is javax/jms/MessageListener checks out. What dumb mistake am I making?
When I compile without -cp js, it fails, saying:
error: package javax.jms does not exist
thus, at least the compiler is looking in the jar.
I made 2 Mistakes
1: Credit to #NormR, .:jms.jar (or .; for Windows I surmise)
2: Link jar while executing. Therefore, the commands should've been:
javac -cp .:jms.jar Main.java
java -cp .:jms.jar Main
Im trying to run a jar file from java code, But unfortunately does not success.
A few details about the jar file:
The jar file located in a different folder (For example - "Folder").
The jar file using a files and folders are in the root folder (the same "Folder" i mentioned above).
What im trying to do so far:
JAR file project.
In netbeans i checked that the main class are defiend (Project properties -> Run -> Main Class).
Other JAVA program
Trying to run with the command:
Runtime.getRuntime().exec("javaw -jar "C:\\Software\\program.jar");
&&
Runtime.getRuntime().exec("javaw -jar "C:\\Software\\program.jar" "C:\\Software");
The jar file opened well, But he doesnt know and recognize his inner folders and files (the same "Folder" i mention above).
In short, it does not recognize its root folder.
Trying to run with ProcessBuilder
ProcessBuilder pb = new ProcessBuilder("cmd.exe", "/C", "start", "javaw", "-jar", "C:\\Software\\program.jar");
pb.directory(new File("C:\\Software"));
try {
pb.start();
} catch (IOException ex) {
}
In Some PC's its works fine, But in other pc's its not work and i got an error message: "Could not find the main class"
** Offcourse if i run the jar with double click its works.
So how can i run a jar file from other java program ?
Use this variant of .exec where you specify working folder as the third argument. (In your examples, you always only use one argument.)
exec("javaw -jar "C:\\Software\\program.jar", null, "C:\\Software");
You can try to call it something like below. There are 2 types of calling it.
public class JarExecutor {
public static void main(String[] args) throws IOException, InterruptedException {
//This is first way of calling.
Process proc=Runtime.getRuntime().exec(new String[]{"java","-jar" ,"C:\\Users\\Leno\\Desktop\\JarsPractise\\JarsPrac.jar"});
//This is second way of calling.
Process proc=Runtime.getRuntime().exec(new String[]{"java","-cp","C:\\Users\\Leno\\Desktop\\JarsPractise\\JarsPrac.jar","com.shiva.practise.FloydTriangle"});
proc.waitFor();
BufferedInputStream is=new BufferedInputStream(proc.getInputStream());
byte[] byt=new byte[is.available()];
is.read(byt,0,byt.length);
System.out.println(new String(byt));
}
}
I type javac helloworld.java at cmd in win 7.
C:\Users\User\Downloads\java_tut>javac HelloWorld.java
C:\Users\User\Downloads\java_tut>dir *.class
Directory of C:\Users\User\Downloads\java_tut
03/28/2014 05:42 PM 429 YourClassName.class
C:\Users\User\Downloads\java_tut>
I searched the following directories for helloworld.class:
java, jre, jdk, java_tut, jre/bin, jdk/bin, and my entire harddrive.
I did need to manually add the /jdk/bin directory to my path. I wonder if that matters?
Another possible reason is an empty .java source file.
This causes javac to silently produce nothing.
I experienced that effect with a Hello Word program and a Macintosh editor - which would save with Cmd-S, but does not save with Ctrl-S. I learned this after 20 years of Java programming.
If HelloWorld.java compiled without any errors, then the file HelloWorld.class should definitely be in the java_tut directory.
EDIT (based on your comments and edits):
Check if your Java source file HelloWorld.java looks as follows:
public class HelloWorld {
public static void main(String[] args) {
System.out.println("Hellow, World!");
}
}
The class must have the same name as the Java source file or you get following compiler error message:
[515]% javac HelloWorld.java
HelloWorld.java:1: error: class YourClassName is public, should be declared in a file named YourClassName.java
public class YourClassName {
^
1 error
Although I asked about the package declaration, I can tell you the correct approach:
Let's assume you have a Java class with that source:
package my.test;
public class HelloWorld {
public static void main(String[] args) {
System.out.println("Hello World.");
}
}
Assuming your project root directory is
C:\Projects\java_tut
you must put the source file HelloWorld.java into the directory
C:\Projects\java_tut\my\test
Afterwards you compile and start this little program while being in the java_tut directory with the following commands:
C:\Projects\java_tut> javac my/test/HelloWorld.java
C:\Projects\java_tut> dir my\test
[...]
28.03.2014 09:35 <DIR> .
28.03.2014 09:35 <DIR> ..
28.03.2014 09:35 434 HelloWorld.class
28.03.2014 09:34 134 HelloWorld.java
[...]
C:\Projects\java_tut> java my.test.HelloWorld
Hello World.
Explanation: If working with packages (and you always should use packages for your classes) you must not "sit" in that package, but always run the commands from outside the package (folder).
YourClassName.class is the correct file in this case. The class name isn't generated based on the .java file's name. It's generated based on the class name inside the .java file. In my .java file, I named the class YourClassName and not HelloWorld.
public class RunScript {
public static void main(String[] args) {
System.out.println("Hello World");
}
}
I created a Java project and a package called com.klong
Inside of the package I have one .java file with the above code in it.
I export it into a runnable jar file. Then I try to run the jar in a command line using this:
java test.jar
When I try that, I get the following error
C:\Users\IBM_ADMIN\Tracing stuff>java test.jar
Exception in thread "main" java.lang.NoClassDefFoundError: test.jar
Caused by: java.lang.ClassNotFoundException: test.jar
at java.net.URLClassLoader.findClass(URLClassLoader.java:434)
at java.lang.ClassLoader.loadClass(ClassLoader.java:653)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:358)
at java.lang.ClassLoader.loadClass(ClassLoader.java:619)
Could not find the main class: test.jar. Program will exit.
I've tinkered with exporting into a normal jar file and such. I've looked at other questions about this error. I've tried using commands such as set classpath=BLEH
This project is as simple as can be so hopefully you can help me figure out this pesky error!
You need to run java -jar test.jar, what you're currently doing is asking java to look for compiled classes with the name test.jar.