• Post Reply Bookmark Topic Watch Topic
  • New Topic

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

 
Richard Mccaff
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.

 
Knute Snortum
Sheriff
Posts: 4276
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • 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.
 
Stevens Miller
Bartender
Posts: 1445
30
C++ Java Netbeans IDE Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Where is the HelloWorld class (referred to at Line 34) defined?
 
Junilu Lacar
Sheriff
Posts: 11481
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • 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.
 
Darryl Burke
Bartender
Posts: 5167
11
Java Netbeans IDE Opera
  • 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
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!