Could not find Main Class

Résolu
nadong94 Messages postés 11 Date d'inscription   Statut Membre Dernière intervention   -  
nadong94 Messages postés 11 Date d'inscription   Statut Membre Dernière intervention   -
Bonjour à tous,

J'utilise java 6 et j'ai créer un .jar avec eclipse, mais j'ai modifier le fichier MANIFEST.MF afin d'y ajouter les Class-Path nécessaire au fonctionnement du programme.
Et quand je lance le fichier.jar via la console j'ai un message d'erreur, comme quoi qu'il ne trouve pas la classe principale.
Contenu du fichier MANIFEST.MF
Manifest-Version: 1.0
Main-Class: IHM.Main
Class-Path:
Y:\NEMO - Espace travail\Nathan\API\poi-bin-3.14\poi-3.14\poi-3.14-20160307.jar
Y:\NEMO - Espace travail\Nathan\API\poi-bin3.14\poi-3.14\poi-ooxml-3.14-20160307.jar
Y:\NEMO - Espace travail\Nathan\API\poi-bin-3.14\poi-3.14\poi-ooxml-schemas-3.14-20160307.jar
Y:\NEMO - Espace travail\Nathan\API\poi-bin-3.14\poi-3.14\ooxml-lib\xmlbeans-2.6.0.jar
//ligne vide

La console :
m:\Jar>java -jar H2O.jar
Exception in thread "main" java.lang.NoClassDefFoundError: IHM/Main
Caused by: java.lang.ClassNotFoundException: IHM.Main
at java.net.URLClassLoader$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
Could not find the main class: IHM.Main. Program will exit.

m:\Jar>


Merci d'avance pour vos aides :) :)
A voir également:

4 réponses

nadong94 Messages postés 11 Date d'inscription   Statut Membre Dernière intervention   1
 
3 choses :
1 ► Erreur de chemin d'accès car je suppose les espaces le dérange même avec des "" ou des ''
2 ► il y avait des espaces à la fin de certaine lignes, mais maintenant j'ai tout mis sur une ligne sans espace à la fin puis une seule ligne vide
3 ► J'ai un fichier xls qui doit être utiliser au lancement de l'application.

2 questions :
1 ► Comment prendre en compte les chemins d'accès avec des espaces ?
2► Malgré que le fichier xls se trouve dans le JAR (car je veux qu'il soit au même niveau que l'exécutable) il ne le trouve pas, faut-il le préciser dans le Class-Path ?

Merci encore :)
0
KX Messages postés 16761 Date d'inscription   Statut Modérateur Dernière intervention   3 020
 
Bonjour,

Ne mets pas tes dépendances en chemin absolu Y:\ parce que si tu donnes ton jar à quelqu'un d'autre il ne pourra pas l'utiliser à moins d'avoir aussi un répertoire Y:\ avec les même fichiers au même endroit.

Utilises plutôt des chemins relatifs, par exemple un répertoire lib à côté de ton jar.

De plus, ne sautes pas de lignes, ce sont les espaces qui séparent deux librairies dans le class-path.

Class-Path: lib\poi-3.14-20160307.jar lib\poi-ooxml-3.14-20160307.jar lib\poi-ooxml-schemas-3.14-20160307.jar lib\xmlbeans-2.6.0.jar

Voir: https://docs.oracle.com/javase/tutorial/deployment/jar/downman.html

Quant à la déclaration de Main-Class ça semble bon, sous réserve bien sûr que dans ton jar tu ais un dossier IHM avec un fichier Main.class dont le code Java déclare un package IHM, une classe Main, et une méthode
public static void main(String[] args)


Voir: https://docs.oracle.com/javase/tutorial/deployment/jar/appman.html

Pour le fichier xls tu peux le récupérer avec
getClass().getClassLoader().getResource("file.xls")
en supposant que file.xls est à la racine du Jar.

Voir: https://docs.oracle.com/javase/tutorial/deployment/webstart/retrievingResources.html

Remarque : tu pourrais aussi te simplifier la vie avec Maven, qui peux te créer automatiquement ton jar avec la bonne configuration dans le manifest (et bien plus encore).
https://forums.commentcamarche.net/forum/affich-37593827-debuter-avec-maven
0
nadong94
 
Merci pour la réponse,
Pour le fichier xls toujours le même message d'erreur.
Voici ma méthode :
public void loadCableFile()
{
final String CABLETYPE = "CableType.xls";
this.getClass().getClassLoader().getResource(CABLETYPE);
final double RATIO = 0.009900990099009901;
try
{
ZipSecureFile.setMinInflateRatio(RATIO);
File file = new File(CABLETYPE);
Workbook workbook = WorkbookFactory.create(file);

Sheet sheet = workbook.getSheetAt(0);
Iterator<Row> rows = sheet.rowIterator();
Row row;
int nbColumns;

while (rows.hasNext())
{
row = (Row) rows.next();
nbColumns = row.getLastCellNum();
for (int columnContent = 0; columnContent < nbColumns; columnContent++)
{
this.cell = row.getCell((short) columnContent);
if (this.cell != null)
{
this.valuesOfCableType.add(cell.getStringCellValue());
}
}
}
workbook.close();
} catch (InvalidFormatException e) {
UtilityMethods.getStackTrace(e);
e.printStackTrace();
} catch (IOException e) {
UtilityMethods.getStackTrace(e);
e.printStackTrace();
} catch (EncryptedDocumentException e) {
UtilityMethods.getStackTrace(e);
e.printStackTrace();
}
}


Et le message d'erreur :
M:\Jar>java -jar H2O.jar
Min : 175
java.io.FileNotFoundException: CableType.xls
at org.apache.poi.ss.usermodel.WorkbookFactory.create(WorkbookFactory.ja
va:276)
at org.apache.poi.ss.usermodel.WorkbookFactory.create(WorkbookFactory.ja
va:252)
at org.apache.poi.ss.usermodel.WorkbookFactory.create(WorkbookFactory.ja
va:231)
at EVENT.UtilityMethods.loadCableFile(UtilityMethods.java:1052)
at EVENT.Windows.windowOpened(Windows.java:70)
at java.awt.Window.processWindowEvent(Unknown Source)
at javax.swing.JFrame.processWindowEvent(Unknown Source)
at java.awt.Window.processEvent(Unknown Source)
at java.awt.Component.dispatchEventImpl(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Window.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
at java.awt.EventQueue.access$000(Unknown Source)
at java.awt.EventQueue$1.run(Unknown Source)
at java.awt.EventQueue$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.AccessControlContext$1.doIntersectionPrivilege(Unknown
Source)
at java.security.AccessControlContext$1.doIntersectionPrivilege(Unknown
Source)
at java.awt.EventQueue$2.run(Unknown Source)
at java.awt.EventQueue$2.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.AccessControlContext$1.doIntersectionPrivilege(Unknown
Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)
Max : 800

M:\Jar>

Merci encore :)
0
nadong94 Messages postés 11 Date d'inscription   Statut Membre Dernière intervention   1
 
Et quand je fais un :
URL url = this.getClass().getClassLoader().getResource(CABLETYPE);
URI uri = url.toURI();
File file = new File(uri);
Workbook workbook = WorkbookFactory.create(file);


il me dit :
Exception in thread "AWT-EventQueue-0" java.lang.IllegalArgumentException: URI i
s not hierarchical
at java.io.File.<init>(Unknown Source)
at EVENT.UtilityMethods.loadCableFile(UtilityMethods.java:1055)
at EVENT.Windows.windowOpened(Windows.java:70)
at java.awt.Window.processWindowEvent(Unknown Source)
at javax.swing.JFrame.processWindowEvent(Unknown Source)
at java.awt.Window.processEvent(Unknown Source)
at java.awt.Component.dispatchEventImpl(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Window.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
at java.awt.EventQueue.access$000(Unknown Source)
at java.awt.EventQueue$1.run(Unknown Source)
at java.awt.EventQueue$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.AccessControlContext$1.doIntersectionPrivilege(Unknown
Source)
at java.security.AccessControlContext$1.doIntersectionPrivilege(Unknown
Source)
at java.awt.EventQueue$2.run(Unknown Source)
at java.awt.EventQueue$2.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.AccessControlContext$1.doIntersectionPrivilege(Unknown
Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)
Max : 800

0
KX Messages postés 16761 Date d'inscription   Statut Modérateur Dernière intervention   3 020
 
À partir du moment où ta ressource est dans un JAR, elle n'est plus considérée comme un fichier, tu ne peux donc pas y accéder avec la classe File.

C'est pour cela que j'indiquais l'utilisation du code d'accès aux ressources :
getClass().getClassLoader().getResource("file.xls");

Dans ton cas, tu peux même directement récupérer le flux :
InputStream stream = getClass().getClassLoader().getResourceAsStream(CABLETYPE);
Workbook workbook = WorkbookFactory.create(stream);
0
nadong94 Messages postés 11 Date d'inscription   Statut Membre Dernière intervention   1 > KX Messages postés 16761 Date d'inscription   Statut Modérateur Dernière intervention  
 
Voici la console d'eclipse
ressource cabletype: CableType.xls
Protection domain: file:/Y:/NEMO%20-%20Espace%20travail/Nathan/workspace/H2O/bin/
URL: null
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at org.apache.poi.ss.usermodel.WorkbookFactory.create(WorkbookFactory.java:198)
at org.apache.poi.ss.usermodel.WorkbookFactory.create(WorkbookFactory.java:170)
at EVENT.UtilityMethods.loadCableFile(UtilityMethods.java:1056)
at EVENT.Windows.windowOpened(Windows.java:70)
at java.awt.Window.processWindowEvent(Unknown Source)
at javax.swing.JFrame.processWindowEvent(Unknown Source)
at java.awt.Window.processEvent(Unknown Source)
at java.awt.Component.dispatchEventImpl(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Window.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
at java.awt.EventQueue.access$000(Unknown Source)
at java.awt.EventQueue$1.run(Unknown Source)
at java.awt.EventQueue$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.AccessControlContext$1.doIntersectionPrivilege(Unknown Source)
at java.security.AccessControlContext$1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue$2.run(Unknown Source)
at java.awt.EventQueue$2.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.AccessControlContext$1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)
Via classe: null
0
nadong94 Messages postés 11 Date d'inscription   Statut Membre Dernière intervention   1
 
Finalement j'ai eu la confirmation de mon tuteur de stage, et le fichier doit être à l'extérieur du jar donc ce code est bon.

La méthodes :
public void loadCableFile()
{
final String CABLETYPE = "..\\CableType.xls";
final double RATIO = 0.009900990099009901;
try
{
ZipSecureFile.setMinInflateRatio(RATIO);
File file = new File(CABLETYPE);
Workbook workbook = WorkbookFactory.create(file);
Sheet sheet = workbook.getSheetAt(0);
Iterator<Row> rows = sheet.rowIterator();
Row row;
int nbColumns;
while (rows.hasNext())
{
row = (Row) rows.next();
nbColumns = row.getLastCellNum();
for (int columnContent = 0; columnContent < nbColumns; columnContent++)
{
this.cell = row.getCell((short) columnContent);
if (this.cell != null)
{
this.valuesOfCableType.add(cell.getStringCellValue());
}
}
}
workbook.close();
} catch (InvalidFormatException e) {
UtilityMethods.getStackTrace(e);
e.printStackTrace();
} catch (IOException e) {
UtilityMethods.getStackTrace(e);
e.printStackTrace();
} catch (EncryptedDocumentException e) {
UtilityMethods.getStackTrace(e);
e.printStackTrace();
}
}


Le fichier MANIFEST.MF :
Manifest-Version: 1.0
Class-Path: lib\poi-3.14-20160307.jar lib\poi-ooxml-3.14-20160307.jar
lib\poi-ooxml-schemas-3.14-20160307.jar lib\xmlbeans-2.6.0.jar
Main-Class: IHM.Main


Puis le fichier bat :
cd .\Jar\
start javaw -Xms1G -Xmx1G -jar H2O.jar
exit


Merci :) :) :)
0