Running Java from an OSX ".app" bundle causes "Error -10810" - java

This was tested on OSX Mavericks through virtual box, and on Yosemite on a macbook.
I have a simple executable jar named "HelloWorld.jar".
I am trying to create a .app bundle for this java application. (Obviously, my actual program is more complex, but I have whittled it down to the barest problems).
CASE 1 - SIMPLE BUNDLE WITHOUT JAVA - WORKS COMPLETELY
Step 1 - Test at Console - Works
At the console I type
echo "Hello World (no java)" > /Users/josh/Desktop/test-output.txt
I view test-output.txt and see the expected output.
Step 2 - Test with Script - Works
I make a simple bash script named test:
#!/bin/bash
echo "Hello World (no java)" > /Users/josh/Desktop/test-output.txt
I chmod +x test and then type ./test, I view test-output.txt and see the expected output.
Step 3 - Create Rudimentary App Bundle - Works
mkdir -p test.app/Contents/MacOS
cp test test.app/Contents/MacOS
open test.app
I view test-output.txt and see the expected output.
CASE 2 - SIMPLE BUNDLE WITH JAVA - DOES NOT WORK
Setup
File HelloWorld.java:
public class HelloWorld {
public static void main ( String[] args ) {
System.out.println ( "Hello World" );
}
}
File myManifest
Main-Class: HelloWorld
Did the following at console:
javac HelloWorld.java
jar -cfm HelloWorld.jar myManifest HelloWorld.class
Step 1 - Test at Console - Works
At the console I type:
java -jar HelloWorld.jar > /Users/josh/Desktop/java-output.txt
I get the expected output: Hello World
Step 2 - Test with Script - Works
I make a simple bash script named "helloworld"
#!/bin/bash
java -jar HelloWorld.jar > /Users/josh/Desktop/java-output.txt
I chmod +x helloworld and then type ./helloworld, I get the expected output: Hello World
Step 3 (With Java) - Does not Work
mkdir -p helloworld.app/Contents/MacOS
cp helloworld helloworld.app/Contents/MacOS
cp HelloWorld.jar helloworld.app/Contents/MacOS
open helloworld.app
I get the following error:
LSOpenURLsWithRole() failed with error -10810 for the file /Users/josh/Desktop/helloworld/helloworld.app
/user/Josh/desktop/java-output.txt appears, but has no text inside.
As you can see, it appears that something is happening where running java inside an .app bundle is giving me that -10810 error.
Note: I also tried a variation of the first example, where I had the bash script launch /Applications/TextEdit.app, and that worked successfully. It makes me suspect the problem is with java specifically.
Does anyone have any idea what's causing this problem and how I can fix it?

I do not currently have an OS X machine handy to test this, but hints on the web from another question seem to imply that you need to set JAVA_HOME and possibly PATH in order to make java work inside the app bundle.
Specifically, at the top of your shell script, before attempting to run your program, put the following lines, with appropriate changes for your system.
export JAVA_HOME=/path/to/my/java/install
export PATH=$PATH:/path/to/directory/containing/java
More generally, to diagnose the root cause of the problem, change the existing line in your script to capture stderr and see if that gives you any useful output that might have otherwise been swallowed by the app's environment.
java -jar HelloWorld.jar > /Users/josh/Desktop/java-output.txt 2> /Users/josh/Desktop/java-error.txt
If you are able to capture the printed error, it may suffice to show you root cause.

Related

Show "java.lang.NoClassDefFoundError" (with Maven) when using "Code Runner"

I am using VSCode with Maven to learn Java(my javac -version is 11.0.10) under macOS Big Sur.
It is ok when I run the code in the terminal by using "RUN," which show on the list of VSCode. However, it always shows the error "Could Not Find Or Load Main Class"(actually in Chinese "找不到或無法載入主要類別 hello") and the reason "java.lang.NoClassDefFoundError: org/seifert/learnjava8/hello (wrong name: hello)".
As you can see, the .java file is just a straightforward one to show"hello world."
the whole screen image here
Had anyone who uses Code Runner met this problem before? How can I resolve this?
The error is because of it isn't called by its "fully qualified name". The project has to be called as hello.java rather than simply hello. The execution command for your file should be:
java <directory in which your file lies> hello.java
So your error should have looked like this:
The error is shown in this picture.
And if you use .java then, the problem can be solved as:
The solved program is in this picture
As you mentioned you use code runner. Follow the steps:
Head over to the setting of your vscode and search for code runner and find
Coderunner: Executor Map
Click on "Edit in settings.json"
Settings page.
Look for java as: java in settings.json page
"code-runner.executorMap": {
"java": "cd $dir && javac $fileName && java $fileNameWithoutExt",
}
Just add .java at the end of the line inside the Double quotes WITHOUT ANY SPACE. Then, save it.
"code-runner.executorMap": {
"java": "cd $dir && javac $fileName && java $fileNameWithoutExt.java",
}
Now run your program. You are good to go!
The error occurs because it must be called with its fully qualified name. To be clear, the name of this class is not hello, It's org.seifert.learnjava8.hello. I create a simple maven project, so the right execution command is:
Turn to the folder java: cd src\main\java
Compile the .java file: javac org\seifert\learnjava8\hello.java
Run the java file: java org.seifert.learnjava8.hello
When it comes to run the java file by Code Runner, the execution commands is
"code-runner.executorMap":{
"java": "cd $dir && javac $fileName && java $fileNameWithoutExt",
}
The $dir represents the direct path to current opened file, and the javac and java command doesn't include the package to be compiled together, then caused the error:
So the solution is turning to the settings and edit the "code-runner.executorMap":
"code-runner.executorMap":{
"java": "cd /users/seiferthan/.../src/main/java && javac org/seifert/learnjava8/$fileName && java org.seifert.learnjava8.$fileNameWithoutExt",
}

How to hide unwanted log messages on Visual Studio Code, running Java code

I am using Visual Studio Code to write Java programs.
Everything works fine except that I always get unwanted messages when I run the code.
For example, I created a very simple and basic Java project that contains a App.java file (in the src folder, default package):
public class App {
public static void main(String[] args) throws Exception {
System.out.println("Hello, World!");
}
}
I have installed the extensions Java Test Runner and Debugger for Java. If I press "Run", I get:
PS C:\Users\Light\Documents\J\JVsc> cd 'c:\Users\Light\Documents\J\JVsc'; & 'c:\Users\Light\.vscode\extensions\vscjava.vsco\extensions\vscjava.vscode-java-debug-0.28.0\scripts\launcher.bat' 'C:\Program Files\Java\jdk-11.0.1\bin\javaTF-8' '-cp' 'C:\Users\Ri.exe' '-Dfile.encoding=UTF-8' '-cp' 'C:\Users\Light\AppData\Roaming\Code\User\workspaceStorage\67e3f4593863e5815\bin' 'App' 7c280b6c994f0132b01b2\redhat.java\jdt_ws\JVsc_623e5815\bin' 'App'
Hello, World!
This is very bad to see, and I have to record the video for school purposes, and I don't want these messages to appear. I dont' even know if this is a debugger issue or not.
It seems that VSC calls "CD command" before running the program.
I have tried to change the launch.json setting "console": "internalConsole" but it does not solve the problem, what it happens with this setting set up this way is that NO output at all is shown.
If I digit "cd src" then MANUALLY call "java App.java" it prints Hello World without that message, but obviously I don't want to manually type the compile and run commands.
There are some workarounds:
1.Add this in settings.json:
"terminal.integrated.enableFileLinks": false,
which will show less scripts before output in integrated terminal.
2.Set "console": "externalTerminal" in launch.json, you'll get a external console and no scripts displayed befor the real output:
3.Install the extension Code Runner and add the following code in settings.json:
"code-runner.clearPreviousOutput": true,
"code-runner.showExecutionMessage": false,
Install code runner extension
go to File> preferences > settings
search for "Code-runner: Run In Terminal" and enable it to accept input from scanner.
go to File> preferences > settings, then search for "Executor Map" > click edit in settings.json
and add the following modification corresponding to the programming language, java for example:
Java : "function prompt{ \">>\"} && clear && tput setaf 6 && cd $dir && javac $fileName && java $fileNameWithoutExt && echo `n && tput setaf 5",
you can check my videos for detailed explanation
https://youtu.be/WM5iW8SyGpk
https://youtu.be/yq8j3AsEOL0
The closest we can get is by clearing the terminal before code runner runs the code. To do that:
Use the Code Runner extension
In your settings.json file, change the java value in code-runner.executor map so that it'll look like this:
"code-runner.executorMap": {
"java": "cd $dir && javac $fileName && clear && java $fileNameWithoutExt"
}
some screen shots:
code runner not running
just hit the run code button
cleared console
answered questions
final output after everything

Java -cp on linux

I made a program which runs fine on windows. When I moved it over to CentOS, I got this error:
Error: Could not find or load main class org.wbc.WBCController
This is the file setup and .sh on linux:
And this is the file setup and .bat on windows:
Does anybody know what the problem is, and how I can fix it?
Java will respond with this error even if it cannot find the file wbc.jar. I am guessing that that is your problem. You might want to see that your are executing the shell script from within the right working directory.
Check to see if you can run wbc.sh from the console or put this in wbc.sh to make sure it searches for the jar in the same directory as the shell script:
#!/bin/sh
java -cp `dirname $0`/wbc.jar org.wbc.WBCController

Execute Java on Ubuntu: PHP exec() is not working while console works just fine

My website needs PHP to run a Java program in the background. So, PHP issues exec() method to let Java do all the work. The implementation works fine on Windows, but completely failed on Ubuntu. While exec() doesn't work, a stand-alone test with console works just fine.
I've setup a test.php to narrow down the problem:
<?php
$output = exec("java -cp ~/path/to/java/class/file/folder Hello 2>&1");
//$output = exec("whoami");
echo $output;
?>
The Hello.java is simply:
public class Hello {
public static void main(String[] args) {
System.out.println("Hello, world!");
}
}
By running test.php on localhost, it shows:
Error: Could not find or load main class Hello
I tried to narrow down the cause of the error, and my thought went like this:
exec() itself is problematic:
unlikely, since whoami prints out apache-user as expected.
what the error message means:
I searched about this error. Post like this one talks about it is caused by the absence of classpath. It's not the case for me either, because in console it works. So the error message means nothing (does it?)
user/group permission:
Is it possible that apache-user is not permitted to run the class file?
I checked, and found the permission code of Hello.class to be rw-r--r--, owned by apache-user:webmasters.
But, even if no one has x permission of the file, in console I can still run it (using my own user).
I'm not sure about the situation here. But my understanding is that by running java program, it is really JVM executing it (or something else); so the permission of Hello.class doesn't matter.
I found another post has a similar situation. But its solution - specifying full path to Java bin /usr/bin/java - doesn't work for me...
What is causing the error?
Can anyone help? Detailed solution is appreciated! I'm a newbie #_#
Many thanks!!!
Have you tried java -cp /path/to/folder/containing/class/file Hello 2>&1? It appears that the class file itself should not be the classpath. It should be in the classpath. If this were a .jar file, on the other hand, then you would provide the filename in the classpath.
I've solved the problem... It's quite unexpected.
I changed the classpath.
Previously it's something like ~/myproject/to/java/class/file/folder.
And I changed it to /home/myuser/myproject/to/java/class/file/folder.
But I completely don't understand why ~ notation doesn't work with exec().
Give the path and Hello.java file free.
Test the rights for the apache user with:
sudo -u webmasters java -cp /path/to/java/class/file/folder Hello
chmod a+r Hello.class

Use Java Program As Start UP Script Linux (RHEL5) Giving Error?

hi i have created my jar file PingConsolApps.jar and i have run this jar file at terminal running fine now o want to add it as a service that would start up on boot so i have put my jar file in "/home" place and i have created file PingConsolApps in etc/init.d as following this site,
http://www.shayanderson.com/linux/ad...-on-bootup.htm
but i have edited it here,
Code:
#! /bin/sh
case "$1" in
start)
cd /home/
/usr/bin/java -jar PingConsolApps.jar &
;;
stop)
killall -v java
;;
esac
exit 0
i gave permission to it ,
Code:
chmod +x /etc/init.d/PingConsolApps
and after it i execute,
Code:
chmod 777 /etc/init.d/PingConsolApps
i try to start its service
Code:
/etc/init.d/PingConsolApps start
it does not show any error but after it i check it using
Code:
service PingConsolApps status
it does not show any thing and do not give ant error i check it in running service,
Code:
service --status -all
i do not get it there also
when i try to stop it
Code:
/etc/init.d/PingConsolApps stop
it give error "java: no process killed"
if it run this service then my sql data base would be updates while it do not up date ???
what are the mistakes in this script help me out to get my goals i am new to linux and development to specially like creating this services
Thanks in Advance
See: http://fedoraproject.org/wiki/Packaging:SysVInitScript#Initscript_template
It is mandatory to have a line like:
#chkconfig 234 90 10

Categories