I am having trouble with running a jar file - java

I try to run a java program with the following script:
#!/usr/bin/env bash
java -cp dl.jar Elevator
Elevator is the main class. But I get an error every time.
Error: Could not find or load main class Elevator
Caused by: java.lang.ClassNotFoundException: Elevator
My classes are all in the .jar file under the directory src/hw4. I have tried adding src/hw4 to the classpath, but to no avail.

You should not use direct class name. You have to use the full package.
If in your IDE you wrote for exammple: package hw4; and you want to run the class Elevator, use this :
java -cp dl.jar hw4.Elevator
It seems to correspond to your jar file.
The src folder is not on the class path. It's just a directory in your IDE to order all files as project files and project content.

Related

Cannot use class from another directory into main java program

I am trying to use a java class hello in my main program test10. test10 is inside the folder "java" while class hello is in the folder "java1". Both these folders are at same level inside Documents folder. When I try to run the test10 file using the command below (inside java folder):
java -cp ../java1/ test10
It gives NoClassDefFoundError of class hello (I've used ".." so that it goes one level up to find java1). I can't find the way to set the classpath so that I can run the program. Please help!

How to execute a large compiled Java project via command line, without using any software (Windows)?

I have a Java project with 5 packages and 30 classes. I want to test this project on a different computer, but I can't install any sotware on that computer so I can't use things like Maven, Eclipse etc. Is there a way I can execute the program on that computer?
What I tried to do, is to compile the project using Eclipse on my computer, then went to the other computer and tried to execute the project main class via the folder that the main class .class file is at.
I.E., say that the main class name is Hello in package Greetings and Hello.class is at folder named folder. So I opened the command line window at folder and typed the command:
java Greetings.Hello
That didn't work....
Edit: After doing this I got the message: Error: Could not find or load main class Greetings.Hello
If the package name is Greetings and you want to run Hello.class
Hello class must have main method.
Hello.class must in folder name Greetings (package name).
Execute java Greetings.Hello from the one level above of Greetings folder
It seems to me Hello.class is not inside of Greetings folder
If javac is installed on the system you can directly compile on the system. you can compile even large projects including many packages with choosing different options provided by javac.
The javac tool reads class and interface definitions, written in the Java programming language, and compiles them into bytecode class files. It can also process annotations in Java source files and classes.
There are two ways to pass source code file names to javac:
For a small number of source files, simply list the file names on the command line.
For a large number of source files, list the file names in a file, separated by blanks or line breaks. Then use the list file name on the javac command line, preceded by an # character.
Source code file names must have .java suffixes, class file names must have .class suffixes, and both source and class files must have root names that identify the class. For example, a class called MyClass would be written in a source file called MyClass.java and compiled into a bytecode class file called MyClass.class.
Inner class definitions produce additional class files. These class files have names combining the inner and outer class names, such as MyClass$MyInnerClass.class.
You should arrange source files in a directory tree that reflects their package tree. For example, if you keep all your source files in C:\workspace, the source code for com.mysoft.mypack.MyClass should be in C:\workspace\com\mysoft\mypack\MyClass.java.
By default, the compiler puts each class file in the same directory as its source file. You can specify a separate destination directory with -d source
Given that you have Eclipse and you ran the code in Eclipse: the quickest way is to use Eclipse and export it to executable JAR.
If you have Run Configuration (e.g. named Hello) that you use for running the code:
Menu -> Export -> Runnable JAR file
Launch Configuration: Hello
Select export destination: (e.g. C:\tmp\Hello.jar)
Set Extract required libraries into generated JAR
Click finish.
This will create Hello.jar file you can execute typing in:
java -jar Hello.jar

Why I obtain this "Unable to access jarfile" error when I try to execute my jar?

I have the following problem trying to execute a jar file (created from my project) into the Windows shell (the DOS prompt).
I have a file named Main.jar. If I unzip this file it contains a mainPkg folder (that is the name of the package containing the Main class that itself contains the main() method).
So into the mainPkg folder there is the Main.class file that contains the main() method.
Ok so, from the shell, I go into the directory that contains the Main.jar and I perform:
C:\Projects\edi-sta\build\jar>java -jar mainPkg.Main.jar
Error: Unable to access jarfile mainPkg.Main.jar
But as you can see I obtain the Unable to access jarfile mainPkg.Main.jar. Why? What am I missing? How can I solve this issue and execute my Main.jar file?
Basically you've two types of JARs
Normal JAR - to package your classes into a single archive
Runnable JAR - This is similar to Normal jar except that you can run this with java -jar command like this java -jar RunnableMain.jar.
In this one we already configure the class having main(),so no need to pass the class name in jar command
Assuming that yours is a normal JAR, you can execute your class of interest like this
C:\Users\arkantos\work>java -classpath C:\Project\Main.jar mainPkg.Main
Notice that i've mentioned the absolute path of the JAR to add it to classpath, because I'm in a different directory, if not you can cd to that dir containing your Main.jar and then invoke your Main class like this
C:\Project>java -classpath Main.jar mainPkg.Main
Here Main.jar is inside Project directory so no need to give absolute path
The syntax for executing a class containing a main method in a jar is:
java -classpath <jarFile> <class>
In your case:
java -classpath Main.jar mainPkg.Main
If you want to execute the jar using java -jar you must create an executable jar file. That can be done in different ways depending on which build tools you use.

Error when executing a JAR file

I've been learning about JAR files and wanted to try and create and run one myself. I carried out the following steps:
Created a project folder with a 'source' subfolder and a 'classes' subfolder
I wrote 2 source files, one with a main method which creates an instance of the other class and runs a simple method in it.
Compiled these to the 'classes' subfolder. I checked to see if they would run. They did
I created a manifest.txt file and filled in the Main-Class: xxxx and hit the return key. I saved this in the sources subfolder
Created a jar file in the classes subfolder by writing
jar -cvmf manifest.txt zzz.jar *.class
Tried to execute the jar file by typing
java -jar zzz.jar
This gives a ClassNotFound exception. If I try to execute the jar by double clicking on it in windows I get an errorbox saying "Could not find the main class xxxx"
I've double checked the spelling of the class inside the manifest file and it's correct.
Possibly important: I have to compile my programs using java -cp . xyz as there is an issue with my classpath. Does this mean that I need to execute jars in a different way as well? I tried
java -cp . -jar zzz.jar
but ended up with the same exception.
Edit: I ended up starting from scratch and now it runs (with the basic -jar zzz.jar command). Frustrating that I don't know what I was doing wrong but glad that it is working!
Shouldn't number 5. be run in the classes subfolder, where all your class files are? And if your classes are in packages, which they should be, you'll likely want to use * instead of *.class..?
To check what your jar file contains you can run:
jar tf zzz.jar
You will probably have to supply the entire path of the .class file you wish to execute after the classpath. ie java -cp xxx.jar classes.mainProgram.class. Where classes is the name of the folder which contains your class files.

NoClassDefFoundError when running java tool under Xcode

I'm trying to create a Java Tool by using Xcode. I've already changed my build.xml to have Xcode target java 1.6 and not 1.3 so I can use generics. I'm getting no build errors and using 'javac' and 'java' in the terminal works. Now I want it to work in Xcode as well.
I keep getting the following error:
Exception in thread "main" java.lang.NoClassDefFoundError: MyClass
where 'MyClass' is the class containing the main method. It probably has something to do with the classpath, which as the build.xml prescribes is "${bin}". There is a bin folder in my project folder, and it contains all the .class files needed to run the program.
If anybody could help me, it'd be great!
If your class MyClass has no package statement at the top to specify a package path, then MyClass.class must be directly in that "bin" directory.
If your class does have a package statement at the top:
package my.package;
then the JVM will look under the "bin" directory for my/package/MyClass.class. In other words, the .class file will need to be in a directory called "package" that is in a directory called "my", and that "my" directory is what should be in the "bin" directory.

Categories