Brandon
Brandon

Reputation: 23485

Run Jar File Does not work. Can't find Main

I use netbeans to create my java projects for school. I don't like how netbeans uses an internal console instead of the usual black CMD/Windows/Terminal console so I decided to compile my project via Command-Line using a batch file. My batch file refuses to run the jar though. It says that main class is not found. I cannot figure out why :S

Can someone help me or tell me how to fix it?

All the information needed to help me I believe is below:

@echo off

set ProjectName=WildWidgetsWarehouse.jar
set ProjectPath=C:/Users/Brandon/Documents/NetBeansProjects/

set path=C:/Program Files/Java/jdk1.7.0_11/bin


cd /d %~dp0
ECHO.
dir %ProjectPath%/*.java
ECHO.
ECHO.

for %%* in (.) do set FolderName=%%~n*
for %%* in (..) do set ParentDirectory=%%~dpnx*

javac -d ../Classes *.java -cp ../Classes;std.jar

cd %ParentDirectory%\Classes
jar cvf %UserProfile%\Desktop\%ProjectName% %FolderName% .*
java -cp . %UserProfile%\Desktop\%ProjectName%

PAUSE

When ran, it prints:

Invalid switch - "Users".

Note: Some input files use unchecked or unsafe operations.
Note: Recompile with -Xlint:unchecked for details.
.* : no such file or directory

Prints all the file it added.. It adds all my classes.. See picture below

Error: Could not find or load main class C:\Users\Brandon\Desktop\WildWidgetsWar
ehouse.jar
Press any key to continue . . .

My Jar file looks like:

enter image description here enter image description here

Upvotes: 0

Views: 735

Answers (1)

Kent
Kent

Reputation: 195209

to run a jar file, you need -jar option

java -jar xxx.jar

please man java, read -jar option part.

also you may want to add main-class information into the manifest of your jar file.

man jar, and check out the e option.

the MANIFEST.MF of your jar should look like (just example)

Manifest-Version: 1.0
Created-By: 1.6.0 (or maven, ant, blahblah)
Main-Class: com.yourpackage.MainClass

Upvotes: 1

Related Questions