Q:
How can I run a simple Java application on my Android Emulator?
I have some issues with connecting an external database on my Android Emulator.
So, I thought I would try a simple Java program first. I compiled it using the "javac hello.java" command and then used the "javaw" command to execute the program. I got this:
"Error: Could not find or load main class hello.hello"
I believe that the error was because the program is looking for a class called hello and I gave it a class called hello.hello. Then, I tried compiling and executing with this command:
"javac -d bin Hello.java"
"java -d bin Hello"
Here's what I get:
"Error: Could not find or load main class Hello.Hello"
I used the same code in the "javaw" command as I did with "javaw hello.hello". The first program I tried compiled and executed fine, but I want to run the second program (with the Hello.Hello class), because that's what my Java code is. I can post the source code, if you want, but I don't think it will be useful.
Can you explain what I did wrong and what I can do to solve this problem?
Thanks!
PS: I am using Android Studio 2.2.1 and running in Windows 7 Home Premium.
A:
You are missing the name of your class if you give just a class name of your project and a jar file.
javaw -cp "YOUR_JAR_PATH" Hello.Hello
When you execute the program, if it is a simple java program, you should replace the parameter with
Your class name and you must add the following at the end:
java -cp "YOUR_JAR_PATH;." Hello.Hello
If you want the method Hello to be called in your program, you should add public static void main(String[] args) at the top of your Hello class
A:
Your second error:
Error: Could not find or load main class Hello.Hello
is because you are not using the Hello.Hello class as the main class.
When you use java -d bin Hello, your main class is Hello, which does not have a main method. You can change it by executing:
javac -d bin Hello.java
java -classpath bin Hello
Make sure Hello.java is located in the bin directory.
You might have other issues depending on what your code contains.
A:
If you are running from command prompt,
java -cp "
" ClassName
The java program would look for the classname.class in your Java.jar file
If you have other class dependencies, you can pass them like the following:
java -cp
java -cp ClassName
Hope this is helpful.
A:
If you are running on Windows, you have to start command prompt with jdk_home\bin path or jdk_home\lib path (that is, you have to add -Djava.library.path=jdk_home\lib\ at the first command line before java Hello)
Another solution is to add another command line : java -classpath ./lib -Djava.library.path=./lib Hello
Note that if your application contains some .jar file that are from the outside the java_home, you must add their path in the first command line : java -classpath .\path_to_file.jar;.\path_to_another_file.jar ...
If you need to add -cp ... on every program that you want to execute, you must specify the main class with -m parameter
java -m Hello -cp "..\..\.." -Djava.library.path=.\lib Hello
or
java -m Hello -cp "..\..\.." Hello
You can also use
java -m Hello -cp .\lib Hello
The program should be Hello.java
EDIT (thanks @user163059) : The first case is needed in windows (sometime) because the lib folder has a sub-folder javaws.exe which would make the -classpath useless.
EDIT2 : If you are not on Windows, you don't need the second argument in Windows ; if you are on Windows, this second argument is needed in Windows
EDIT3 : In my system, I need also this property in JDK_HOME/jre/lib/security/java.security :
permission java.lang.RuntimePermission "setIO";
and this property in \lib\jfxrt.jar :
org.fife.ui.rs.RsFXApplication=com.sun.javafx.app.LauncherImpl