Cannot run a .jar file - java

I'm having issues when I try to execute a .jar file. I always get the message from the command prompt "cannot find or load main class" every time I try to run the file I have just compiled.
I have tried the following(using the command prompt and saving the java code with Notepad++ and creating the manifest.txt file in the same folder where the .java code was saved):
javac classname.java
java cfm classname.jar manifest.txt classname.class
java -jar classname
I have also tried:
javac classname.java
java classname
And:
java packagename.classname
After compiling the .java file
I know there are hundreds of questions like this in the forums, but I have looked everywhere and I keep getting the same error. I just cannot run the .jar file even after I create it.
It's probably something I have missed but I am out of ideas.
Manifest:
Main-Class: classname

Let's see.
java -jar classname.jar
would work if your MANIFEST.MF calls out the correct class.
java -cp classname.jar my.package.MyClass
would work in any case.

Related

How to include JAR file in a java program that is being executed by command line (Windows)

I know that there are similar posts to this, but I tried all suggested answers there and they didn't work out (maybe I did something wrong). I have a java project with the following folders:
bin
src
in the bin folder, I have few folders with .class files in them (compiled the project with eclipse, now just need to run it with command line). These are the folders inside bin:
client
game
protocol
threadPerClient
tokenizer
I also added the jar file (gson-2.2.1) to the bin folder. My main class is at threadPerClient folder, and this class name is MultipleClientProtocolServer. The main class get an int as a parameter. These are the commands that I tried to run via command line, from bin folder:
java -cp gson-2.2.1.jar threadPerClient.MultipleClientProtocolServer 4000.
And I got for that:
Error: Could not find or load main class threadPerClient.MultipleClientProtocolServer
java -cp threadPerClient/:./ MultipleClientProtocolServer 4000
And I got for that:
Error: Could not find or load main class MultipleClientProtocolServer
java -cp threadPerClient;. com.example.MultipleClientProtocolServer 4000
And I got for that:
Error: Could not find or load main class com.example.MultipleClientProtocolServer
What am I doing wrong?
Make sure you're running command 1. of your question from within the bin folder. Also make sure in -cp you specify the gson jar as well as the current folder:
Unix:
java -cp gson-2.2.1.jar:./ threadPerClient.MultipleClientProtocolServer 4000
Windows should be:
java -cp gson-2.2.1.jar;. threadPerClient.MultipleClientProtocolServer 4000
but I cannot test the command for windows since I don't have a windows machine.

What is the proper entry point, when compiling java to jar?

So, when I write my java file:
public class Program
{
public static void main(String[] args)
{
System.out.println("Serious business logic.");
}
}
Then in windows cmd, I compile this way:
javac Program.java
jar cfe Program.jar Program Program.class
java -jar Program.jar
It's fine, and the result is:
"Serious business logic."
When in Netbeans I create a Project, it adds this line:
package program;
And I can not compile in cmd, only inside the IDE.
I've tried manifest.txt, UTF8 encoding without BOM, plus linebreak at the and of file.
Manifest.txt:
Main-Class: program.Program
"jar cvfm Program.jar Manifest.txt Program.class"
and without manifest.txt, just in cmd program.Program
"jar cfe Program.jar program.Program Program.class"
When I tried to run:
java -jar Program.jar
it results in:
"Error: Could not find or load main class program.Program"
I've already checked the following websites:
http://www.skylit.com/javamethods/faqs/createjar.html
https://docs.oracle.com/javase/tutorial/deployment/jar/build.html
and haven't got any idea how to do. Could you please help me?
How do I compile with package keyword? What is the proper entry point?
Thanks!
(ps jre1.8.0_91 ; jdk1.8.0_66 should I use same 32 or 64 bit for both of jre and jdk?)
Make sure when you are compiling your program as a JAR, Program.class is inside of a folder called program. The package keyword that Netbeans has added at the beginning of your script is telling the executable that it is inside of a folder called program. If you are just adding the class file without making sure it is in the correct package (folder), it will not run properly because it does not know where to find it. Your command should be changed to:
jar cvfm Program.jar Manifest.txt program
where program is a folder containing Program.class. Your manifest may be left alone but also needs to be included with compilation.

Making a ImageJ .jar from command line

I have modified a ImageJ source code and i have compiled from command line, using the javac command, but Im trying to create a .jar file and I need help.
It is the first time that I'm trying to compile and create .jar from command line. When I created the .jar, I did the next command:
jar cf test.jar ImageJ/ij/*
Directory "ImageJ/ij/" contains the compiled code.
It doesn't fail but when I'm trying to execute the .jar It returns the error "Failed to load Main-Class manifest attribute from test.jar". When I execute a .jar I always do "java -jar file.jar" but, in this case, It doesn't work.
If I execute "java -cp test.jar ij.ImageJ" (ij.ImageJ is the Main Class) It works but I need to execute a macro in Batch mode and... It fails.
java -cp test.jar ij.ImageJ -batch ../MacrosIJ/helloWorld.ijm
Exception in thread "main" java.awt.HeadlessException:
No X11 DISPLAY variable was set, but this program performed an operation which requires it.
...
If I create the .jar in Eclipse It works perfectly, Can someone explain what i am doing wrong?.
If someone can help I will be grateful. Thanks!!
The recommended way to build ImageJ from source is using Maven:
git clone https://github.com/imagej/ImageJA.git
mvn
cd target
java -cp ij-1.50.jar ij.ImageJ

Run java program sing Cmd

I'm trying to run Java program using cmd.
When I compile that file using
javac helloworld.java
It works perfectly but when I try to run that file using
java helloworld
I get an error:
couldn't find or load main class.
even though my I put my javac path in class path in system variables and javac working correctly.
After searching on how I can fix it I found that I can use
java -cp . helloworld
because it let you to find .class file.
and it works but I know that I can run java program without this -cp so what this for and how I can run my program without it?
-cp specifies the Java classpath for the JRE you are attempting to start. Look for an environment variable CLASSPATH and add '.'.
-cp is used to set the classpath for the jar file, this flag is same as importing a jar file to eclipse and then use it.
If you want to run without this flag, use should set the classpath first beforing running.
export CLASSPATH=path/to/your/jarfile.jar
If you already have some classpath set
export CLASSPATH=$CLASSPATH:path/to/your/jarfile.jar
If you want to include current directory
export CLASSPATH=$CLASSPATH:path/to/your/jarfile.jar:.
As other have mentioned, you can set the CLASSPATH. However, a much better approach is to bundle the .class files in a JAR ans user java -jar nameofthejar.jar.

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

Categories

Resources