Win a copy of OCP Java SE 8 Programmer II Exam Study Guide this week in the OCP forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Could not find or load main class  RSS feed

 
Ranch Hand
Posts: 1537
11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,

I have a simple executable Jar file.

My manifest says this:



----

The CopyFromAToB.class file is in the root directory of the JAR file.

----

Yet, when I try this:

I get this:

Could not find or load main class CopyFromAToB

---

What could possibly be wrong with this simple setup?

The code runs fine standalone -- that is, as the existing Java code inside the IDE.

Thanks in advance,

- mike

 
Saloon Keeper
Posts: 4060
48
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is CopyFromAToB in a package?
 
Mike London
Ranch Hand
Posts: 1537
11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Carey Brown wrote:Is CopyFromAToB in a package?



No, it's just in the root directory of the JAR file which seems to match the manifest.

Is a package an implicit requirement?

Thanks,
 
Carey Brown
Saloon Keeper
Posts: 4060
48
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, it may be in the root directory, but did you use "package" when you wrote the class? Something like
 
Rancher
Posts: 2862
32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Another test to see if the class can be found in the jar file:
java -cp TheJarFileName.jar CopyFromAToB
 
Mike London
Ranch Hand
Posts: 1537
11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Carey Brown wrote:Yes, it may be in the root directory, but did you use "package" when you wrote the class? Something like



Sorry Carey that I wasn't totally complete in my posting.

Nope, there is no package statement at the top of the class.

Baffling, right?

- mike
 
Carey Brown
Saloon Keeper
Posts: 4060
48
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Could you post the source code of CopyFromAToB.java ?
 
Norm Radder
Rancher
Posts: 2862
32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Did you try this?

java -cp TheJarFileName.jar CopyFromAToB

If that worked it would say the class file was ok but maybe there was a problem with the manifest file.
 
Mike London
Ranch Hand
Posts: 1537
11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Norm Radder wrote:Did you try this?

java -cp TheJarFileName.jar CopyFromAToB

If that worked it would say the class file was ok but maybe there was a problem with the manifest file.



Yeah, sorry, meant to say that didn't work either.

In any case, I seem to have found the "problem", but I don't understand what's going on.

In the META-INF folder, Intellij is putting a bunch of files there I have no idea why.

For example there is a benign LICENSE.TXT (apache) file, but also an INDEX.LIST file. If I tried to remove the INDEX.LIST file, I got a security exception. This is probably due to other files that I have no idea why they were in the META-INF folder: an RSA file and an "SF" file (extensions). There's also a LICENSE file with no extension. If I remove all these extra files in the META-INF folder, then everything works!

But, with the default build of the JAR file from Intellij, it can't find or load the main class!

I don't seem to be able to tell Intellij how to control this when creating a JAR artifact.

Maybe I should be doing an ant build instead? I don't recall if Ant will unpack JAR files into directories. Seems like a lot of work for what should be simple.

Would appreciate any comments.

Thanks,

-- mike

P.S. I certainly don't miss problems like this when I'm doing Python.
 
Norm Radder
Rancher
Posts: 2862
32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Yeah, sorry, meant to say that didn't work either.


That means the problem is with the class file being in the jar file.  With the jar file on the classpath (-cp) the java command will look in the jar file for the class file referenced on the command line.
If the class file is not found by the java program, then there is a problem there.  That problem would not involve the contents of the manifest.
 
Saloon Keeper
Posts: 8599
161
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Apparently IntelliJ generates an index file for your application and signs the JAR.

You can Google how to disable signing in IntelliJ, but it doesn't explain why execution fails. I surmise it has to do with the generated index. Can you post the contents of the index file?
 
Mike London
Ranch Hand
Posts: 1537
11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Stephan van Hulst wrote:Apparently IntelliJ generates an index file for your application and signs the JAR.

You can Google how to disable signing in IntelliJ, but it doesn't explain why execution fails. I surmise it has to do with the generated index. Can you post the contents of the index file?



Didn't see anything online about disabling signing, only enabling (or wishing for) signing.

I could email you the file, but too proprietary to post.

Thanks Stephan.

- mike
 
Norm Radder
Rancher
Posts: 2862
32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can there be problems executing a class from a signed jar file when using the -cp option and specifying the class to execute on the commandline?
 
Stephan van Hulst
Saloon Keeper
Posts: 8599
161
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The index file is used to speed up the process of finding class definitions. When the index file isn't correct, the runtime isn't going to find the classes involved, regardless of whether they are in the correct place.

JAR signing shouldn't have any relation to this problem. It's just that when you sign the JAR, you get a security exception when you alter any part of the JAR after you've created it, including deleting the index file. That's why you need to delete both the index and the signature files.

Mike, can you recreate the problem with a new project that doesn't contain proprietary data?
 
Mike London
Ranch Hand
Posts: 1537
11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Stephan van Hulst wrote:The index file is used to speed up the process of finding class definitions. When the index file isn't correct, the runtime isn't going to find the classes involved, regardless of whether they are in the correct place.

JAR signing shouldn't have any relation to this problem. It's just that when you sign the JAR, you get a security exception when you alter any part of the JAR after you've created it, including deleting the index file. That's why you need to delete both the index and the signature files.

Mike, can you recreate the problem with a new project that doesn't contain proprietary data?



I will try to get you a sample, but the project, or one like it, uses two (commercial) third party JARs so I couldn't post them anyway.

Appreciate all your help.

- mike
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!