B4J Question Error: Could not find or load main class ...

canalrun

Well-Known Member
Licensed User
Longtime User
I upgraded to B4J 4.0

The IDE executes properly and I can develop and execute code from within the IDE, but if I try to double-click on the Objects Jar file or if I open a command prompt and try to execute the Objects Jar file directly, I get the error "Error: Could not find or load main class"

For example I execute the following command from a command prompt within the Objects directory:
>C:\Java\jdk1.8.0_66\bin\java.exe extract.jar

I get:
Error: Could not find or load main class extract.jar

I can open and run the .b4j file from the IDE, however.

Using previous versions of B4J I had no trouble executing the Jar files that were produced when I double clicked on the Jar file.


The following comes from my investigations – it may be totally unrelated.

Some web searches have suggested that my Java_Path variable is pointing to invalid symlinks

They suggests that I do some "mklink" commands from an administrator command prompt.

I wasn't sure how to easily get an administrator command prompt – so I punted.



Is my error due to a configuration problem? Because of other application problems I had to relocate my JDK installation from C:\Program files\Java to C:\Java so I wouldn't be surprised if something is pointing to the wrong place.

If the solution does require updating the Symbolic links, could you give a very brief description of how to open an administrator command prompt and issue the proper commands?

Is the solution something else?

Thanks,
Barry.
 

Roycefer

Well-Known Member
Licensed User
Longtime User
You're missing the "-jar" parameter in your command line call. The way you're doing it, java.exe is trying to load a class called "extract.jar".
 
Upvote 0

canalrun

Well-Known Member
Licensed User
Longtime User
Ooh! Thank you.

Now if I issue the command:
C:\Java\jdk1.8.0_66\bin\java.exe -jar extract.jar

It does open the program correctly.

But, I still have the problem that if I Double-click on the Jar file it does not open. I am pretty sure this is a misconfiguration on my my system. I'm not familiar enough with Java to know what it is.

When I double-click on the jar, a command window flashes open and closed. I assume it's closing because of some error, likely related to the no "-jar" parameter not being present.

Any ideas?

Thanks,
Barry.
 
Upvote 0

Roycefer

Well-Known Member
Licensed User
Longtime User
I would check your system default applications to make sure java.exe or javaw.exe are the default programs for opening .jar files. Also make sure the link in the defaults is pointing to the correct installation of Java. It wouldn't hurt to check your PATH variable, either, to make sure it's updated. If it is, you should be able to type
B4X:
java -jar extract.jar
while in extract.jar's directory and not have to specify C:\Java....
You should also be able to type
B4X:
java -version
from any directory and get the correct output for your latest installed Java version.
 
Upvote 0

canalrun

Well-Known Member
Licensed User
Longtime User
Thanks.
I checked by Default Programs by right clicking on the Extract.jar file, open with, browse for program, check always open with, explicitly setting: C:\Java\jdk1.8.0_66\bin\java.exe

My environment values are set:
JAVA_HOME C:\Java\jdk1.8.0_66
Path C:\ProgramData\Oracle\Java\javapath; ...

According to what I've found on the net the Path is the problem. Someone said that with the latest Java they now use symbolic links in the ProgramData folder. I would almost bet my symbolic links got messed up when I moved the JDK from C:\Program Files\Java to C:\Java

When I execute the program as follows (this corresponds to double-clicking the jar file):
C:\Users\Barry\Extractor\Objects>extract.jar
I get:
Error: Could not find or load main class C:\Users\Barry\Extractor\Objects\Extract.jar

If I execute the jar explicitly referencing the Java.exe and including the -jar option:
C:\Users\Barry\Extractor\Objects>C:\Java\jdk1.8.0_66\bin\java.exe -jar extract.jar
it opens as expected and I get:
Program started.

When I explicitly reference Java.exe, -version executes as expected:
C:\Users\Barry\Objects>C:\Java\jdk1.8.0_66\bin\java.exe -version
java version "1.8.0_66"
Java(TM) SE Runtime Environment (build 1.8.0_66-b18)
Java HotSpot(TM) 64-Bit Server VM (build 25.66-b18, mixed mode)


Does all this suggests anything?

Barry.
 
Upvote 0

canalrun

Well-Known Member
Licensed User
Longtime User
Ahh, the plot thickens...

In Windows Explorer I was able to navigate to:
C:\ProgramData\Oracle\Java\javapath

This folder contains three symbolic links. If I show properties of any of the links I see that, yes in fact, it is pointing to Program Files\Java rather than \Java.

Moving the Java installation seems to have caused problems.

Okay, simple, so I will just edit the symbolic links... Not so fast – it gives me a privilege violation.

I think I'm stuck at either running Explorer as administrator or opening a Command window as administrator.

I've done this other times. Right clicking gave me a "Run as administrator" option. But I don't see this now.

Barry.
 
Upvote 0

Roycefer

Well-Known Member
Licensed User
Longtime User
Press the Windows key. Type "explorer". Windows Explorer should be one of the search results. Right click on that result and "Run as Administrator" should be one of the options, provided you are signed into Windows with an account that has Administrator access.
 
Upvote 0

canalrun

Well-Known Member
Licensed User
Longtime User
Thanks for your info and the description of how to "Run as administrator".

I was able to execute the mklink commands. The links now point to the right place, but that still did not fix the problem.

Hello Rube Goldberg!

I created a batch file, JavaJar.bat, and placed it in the C:\Java\jdk1.8.0_66 folder.

I modified the default program to run for .jar files to point to this batch file. The batch file contains:
B4X:
C:\Java\jdk1.8.0_66\bin\Java.exe -jar %1

So the default program for.jar files is set to be this batch file. This batch file runs Java.exe explicitly stating the directory.

Now all I have to do is recall the batch file command so that it does not leave the Command window open.

I double-click on my Export.jar file and it runs.

I'm not proud of this solution, but it works. I would appreciate learning the correct way to solve the problem which was probably caused by my moving the Java stuff from C:\Program Files\Java to C:\Java.

Why did I move Java? I have a real old image-editing program that uses Java but does not like spaces in filenames. It has one function that I can't find anywhere else. I don't remember any details, but when you need it, it's really handy.

Thanks,
Barry.
 
Last edited:
Upvote 0

Roycefer

Well-Known Member
Licensed User
Longtime User
Why not completely uninstall all jdks and delete all the java directories and then install java from scratch in the desired directory? I think the installation process on Windows automatically sets the PATH variable
 
Upvote 0

canalrun

Well-Known Member
Licensed User
Longtime User
That's an option.

The first time I installed the latest JDK, I changed the install directory to C:\Java

When I went to set my B4J tools configure paths I did not find a Jdk1.8.0_66 directory only the Jre1.8.0_66. Of course, it may have been hiding in plain sight.

I reinstalled Java to the default directory and the Jdk folder was now present, but in C:\Program Files\Java

I just moved it, although I knew it would probably be a problem down the road.

B4J 4.0 worked fine until I double clicked on the output jar file – more of an annoyance than a problem.

Barry.
 
Upvote 0
Top