Main class not found even when its existing - java

i'm working in my tablet a project that i want to run in my raspberry pi. The IDE AIDE, that i'm using can't export my project as an executable jar file. so i decided to try that by using the command line
I used:
jar cvf ServerActivities.jar *
too create the jar file whichbworked but when i want to start it by using
Java -jar ServerActivities.jar
it says that my Main class couldnt be found or loaded, but it exists and contains an main method

You need to create a manifest and point to your Main class from there.
See: https://docs.oracle.com/javase/tutorial/deployment/jar/appman.html
or: How to setup Main class in manifest file in jar produced by NetBeans project

Related

how to extract src as a jar when classpath is in a referenced library?

I have a bunch of java files and my launch config uses the main class in an external library. I'm having no luck with exporting it as a jar though. When I try to select a path for my main class, nothing shows up. But I can launch my program fine through eclipse. Is this even possible to do?
here is my manifest file
Manifest-Version: 1.0
Rsrc-Class-Path: ./ sb-vit-api.jar sb-vital-framework.jar vital-client.jar
Class-Path: .
I want to access the classfile called Loader.class which sits inside sb-vital-framework.jar inside a package called simple.
First, make sure you have your own main class. For example, CallLoader.java. This class should have a static main method where it can instantiate and call your external Loader class. Run your CallLoader class (right click/Run as.../Java Application). This will create a Launch Configuration.
After this is working:
Right-click on the project and select Export... Then select Java/Runnable Jar. Pick your launch configuration and choose an export destination e.g. myjar.jar. On the command line, you can run your Jar as java -jar myjar.jar.
If you want to directly call your external main then you may edit the MANIFEST in the jar to make that main the main main. :)

How to specify a runnable jar to use tomcat's lib for dependencies

I have made a java project in eclipse that's a runnable jar if I export it.
My structure of my project :
When I export my project as a runnable jar it asks me how I would like my dependencies to be packaged and I choose to put it in a lib folder. So when exporting it exports the runnable jar as well as creates a sub folder with my dependent jar files which I want to be only the 5 jars located in Referenced libraries.
This is the export window :
This creates my runnable jar and then a folder _lib with my other jars.
This works fine but as you see in the first image I also included the Tomcat lib library because some of the jar files in tomcat lib are used by my project.
The problem is that when I export, all the jar files in the tomcat lib get exported to my sub lib folder which I do not want.
Basically I want to deploy my runnable to a server witch will get the dependent jar files from the sub directory lib only those 5 jar files in image (Referenced Libraries). Tomcat is installed on the server so it should point to tomcats lib to get the rest of the jars.
Want my project to use the generated lib folder and then the tomcat lib.
Hope it makes sense what I am trying to ask.
Using Eclipse and java 1.7. developing on windows, deploying to linux running the application via command line on linux box.
What I have done to get this working is on the linux side to start the java runable class another way by specifying the classpath this will give the class you are trying to run the ability to first check the folders that you specify. So in my example instead of executing my app on the linux box with the following :
nohup java -jar /usr/local/P6/StockListShunter/StockListShunter.jar &
I did the following and specified the Class path lib that java should check for .jar file dependencies :
nohup java -classpath :/usr/mylib1/*:/usr/StockListShunter_lib/*:/usr/share/lib/*: stockListShunter.Shunter &
Notice I specified 3 libraries java should check when running my runable class, mylib1,StockListShunter_lib and lib.
After you specified the classpath you will see you need to specify your java class witch will be your main method class. In my example it is stockListShunter.Shunter where stockListShunter is my package name.
Just take note that you should make sure that the class you are trying to excecute is included in one of the jar files that you specified in the classpath otherwise it wont find the class and you will get an error : Error: Could not find or load main class stockListShunter.Shunter
So when you run the command the process will be started, my program is in a loop so the process will be alive until I kill it. If your program should be alive you can confirm it is running by running the following command.
ps aux | grep stockListShunter.Shunter
Where stockListShunter.Shunter is your main class name. This should display all the processes where the command has a stockListShunter.Shunter in it.
If you want to end the process like mine that's in a loop you can run the following command.
pkill -f 'java.*stockListShunter.Shunter'
Again where stockListShunter.Shunter is your main class name. This should kill your java process.
Hope this could help some one in the future.

Getting ClassNotFound Exception when executing jar

Am using IntelliJ 12.0.4 Community Edition.
I created a Java console app called DB with main class name of DB.
I packaged it into an executable jar file called DB.jar.
In that app I connect to an Oracle DB using JDBC.
I packaged the necessary JDBC jar files into the DB.jar via the Intellij's Project Structure (Modules, Library)
When I execute the app from within IntelliJ it runs successfully
If I copy DB.jar to some directory and execute it via "java -jar DB.jar" I get an ClassNotFound exception on oracle.jdbc.driver.OracleDriver
I looked in DB.jar and the jdbc jar files (ojdbc6.jar, ojdbc14.jar) are in DB.jar
Any thoughts?
Jars in an executable jar are not on the classpath, typically.
You can put them in the same folder as DB.jar and do:
java -cp DB.jar;ojdbc6.jar;ojdbc14.jar <MainClass goes here>
and that should run it.
You could also put the Class-Path entry in the Manifest.MF file inside your jar to reference other jars relative to the location of DB.jar in the computer's file system. Reference the two other jar files and then you can do java -jar DB.jar <MainClass goes here> (assuming the two jars are in the right place.
There is some discussion here about the general frustration about not being able to do what you want to do (and what many others have wanted for years).
Classpath including JAR within a JAR
Explain about Main Class
When you run a java application from the command line, there is some class where the execution starts. It will look something like this:
package com.mycompany.app;
public class StartHere {
public static void main(String[] args) {
// Your code goes here ...
}
}
If this example was the class where your application starts you would use a command line this to start it. This assumes you have the StartHere class in the right package location in a jar on the classpath:
java -cp DB.jar;ojdbc6.jar;ojdbc14.jar com.mycompany.app.StartHere
Notice that this allows you to have multiple applications in the same jar. Just make multiple classes with a main() method and run them with different starting classes shown on the command line.

Exporting project in Java(Eclipse)

I wrote a simple game with 2D graphics in Java (Eclipse) and lib Slick2D. When I export project to Runnable Jar File in Eclipse and try to run this file just nothing happens. I can run this game in Eclipse without any problems. When I write java and put my jar in console to get path and hit enter i have sth like that:
C:\Users\Me>java C:\Users\Jolanta\Desktop\pnd.jar Error: Could not
find or load main class C:\Users\Jolanta\Desktop\pnd.jar
In launch configuration while exporting I selected right main class. Any ideas?
it's command for starting jar file with main class:
java -jar C:\Users\Jolanta\Desktop\pnd.jar
You need to provide native libraries along with the jar file. Here are some scripts that will launch your game:
windows (.bat file) :
java -Djava.library.path="native/windows/" -jar pnd.jar
linux :
java -Djava.library.path="native/linux/" -jar pnd.jar
where your directory will look like this:
your .jar and scripts are in the same directory along with 'native' folder.
not sure where I got this folder from, I can send it to you if you want

Building Jar file using Eclipse Indigo

I have this project on Eclipse 3.7 (Indigo) and planning to build the *.jar.
I am to build the project by File > Export > Java.Runnable JAR File > Named the filename of the jar file > Finish.
An error always return to me when running the jar file.
Could not find the main class: [package nmame].[Class name of Main class (PSVM) is located].
Program will exit.
In addition, MySQL Connector 5.1 was included as Referenced Library for JDBC use.
How can I resolve and be able to build a jar file that is running as runned on Eclipse as Java Application?
Thanks in advance.
When you export the project try selecting the option :
Copy required libraries into a sub-folder next to the generated jar
This will create a lib folder containing the required dependency libraries ..
You have also to select a valid launch configuration for the program, to make sure that the MANIFEST file will be created with the MAin class attribute populated correctly .
When you run the generated jar you should run it with this command line (READ the MANIFEST.MF) :
java -jar yourjar.jar
or with Main class specified in the command line :
java yourjar.jar MainClass

Categories