This week's book giveaways are in the Angular and TypeScript and Web Services forums.
We're giving away four copies each of Programming with Types and The Design of Web APIs and have the authors on-line!
See this thread and this one for details.
Win a copy of Programming with Types this week in the Angular and TypeScript forum
or The Design of Web APIs in the Web Services forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Bear Bibeault
  • Paul Clapham
  • Jeanne Boyarsky
Sheriffs:
  • Junilu Lacar
  • Knute Snortum
  • Henry Wong
Saloon Keepers:
  • Ron McLeod
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Frits Walraven
  • Joe Ess
  • salvin francis

Why is my create jar file programatically not working? Getting "Could not find or load Main Class...

 
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why is my create jar file programatically not working can anybody help? So doing this does create a jar file. When I run the jar file via command line, it gives me "Could not find or load main class ...". I'm not sure exactly why. I took some code i found here: java - How to use JarOutputStream to create a JAR file? - Stack Overflow and apparently it didn't specify the main class so I specifically added it myself. am i doing something wrong? And btw, the file (that contains all the class files) do not have any directories in them. so it's essentially just one folder with a bunch of class files in there. below is the code. if anybody can like get it to work maybe try out a hello world for testing purposes and get it to work that would be so sweet. i tried and i tried and just couldn't get it to work and confused. thanks again!

fyi I had the commons-io-2.4 jar referenced into my build path that can be downloaded here: Commons IO - Download Commons IO
reason i guess i added to that, though i'm not sure if it was necessary or not, was because it has a file filter which can tell which files are hidden or not and thing is without it it was adding like DS_STORE(which is hidden and i guess unnecessary) to the jar. not sure if this logic is right but just letting you know.

 
Sheriff
Posts: 6564
176
Eclipse IDE Postgres Database VI Editor Chrome Java Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I suspect this is the problem:



This isn't doing what you think it is. Yout want to replace all "\"s, but backslash is a metacharacter in regexes, so you write "\\". But a backslash is also a metacharacter in the java.langString, so you need to write "\\\\".

Edit: except I'm wrong. replace() take a character sequence, not a regex. So never mind.
 
Bartender
Posts: 1464
32
Netbeans IDE C++ Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Where is the HelloWorld class (referred to at Line 34) defined?
 
Sheriff
Posts: 14615
243
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

This isn't literally what you have though, right? You are providing the actual full path to the folder containing all the classes, right? Just checking.
 
Richard Mccaff
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Stevens Miller wrote:Where is the HelloWorld class (referred to at Line 34) defined?



I mean this is your classical/typical public static void main(String[] args) { System.out.println("Hello World");}

Just an example. The thing I want to make clear is that all the class files are correct and like they all work together. I've tested it out multiple times via eclipse and all that shanigans and it works perfectly. I've also created a runnable jar before VIA eclipse and it worked like a charm so i know something's wrong here.
 
Richard Mccaff
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Junilu Lacar wrote:


This isn't literally what you have though, right? You are providing the actual full path to the folder containing all the classes, right? Just checking.

Yea. you're right haha. not literally. it's in the format of: /Users/john/Desktop/filesofclasses
 
Richard Mccaff
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Richard Mccaff wrote:

Stevens Miller wrote:Where is the HelloWorld class (referred to at Line 34) defined?



I mean this is your classical/typical public static void main(String[] args) { System.out.println("Hello World");}

Just an example. The thing I want to make clear is that all the class files are correct and like they all work together. I've tested it out multiple times via eclipse and all that shanigans and it works perfectly. I've also created a runnable jar before VIA eclipse and it worked like a charm so i know something's wrong here.



Oh where is it defined. I misread your question. HelloWorld is simply in the same folder as all the classes which I'm referring to in that nonliteral line on line 36 when creating the file. I'm not sure if that's right but i figured if it isn't in some folders than it should just be in its own name right? It's also kinda weird because i've asked some friends about this and they told me to why not cd into the jar to figure out if the format is even right and i was like, wow oh why didn't i think of this but then i did that and it says that it's not a directory so i can't cd into it. And also the fact that right i create a runnable jar in eclipse, it's 1.5 mb but then i do it via the static class i wrote above it's like 200k kb. So something tells possibly there's an error when copying the files into the jar? Weird. :/

and another thing I wanted to add on is i checked whether it was actually going into the right folder so you see that sys print there i'm printing out the names of the files in that folder with all the class files and it does indeed go into the right folder and target the right files.
 
Richard Mccaff
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I fixed it. My Main-Class was pointing to the wrong place. It was apparently in another folder. I put it all in the root folder and it worked.
 
Bartender
Posts: 5167
11
Netbeans IDE Opera Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Richard, in future please BeForthrightWhenCrossPostingToOtherSites
http://www.java-forums.org/new-java/93498-why-my-create-jar-file-programatically-not-working-getting-could-not-find.html
 
Morning came much too soon and it brought along a friend named Margarita Hangover, and a tiny ad.
Java file APIs (DOC, XLS, PDF, and many more)
https://products.aspose.com/total/java
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!