Assertion failure Aurora (ORA-29516) in Oracle 10g on loadjava - java

public class HelloWorld{
public static void add(int a, int b){
System.out.println(a+b);
}
}
and I load it into oracle via
loadjava -user system/admin Helloworld.class
This words fine.
After that I write this procedure:
create or replace
PROCEDURE start_helloworld(a in number, b in number)
AS language java
name 'HelloWorld.add(int,int)';
I want to be able to call the procedure in PL/SQL:
exec start_helloworld(1,1);
but it gives the error I mentioned.

You can't do console output from Oracle java code, since it's running within the database. Perhaps if you passed in and in/out variable, assigned the output of your arithmetic assignment to the variable and output that in the calling PL/SQL block:
var mynum NUMBER
exec start_helloworld(1,1,:mynum);
print mynum;
You would of course need to modify your java and PL/SQL wrapper to add the new parameter:
public static void add(int a, int b, int c){
c = a+b;
}
and
create or replace
PROCEDURE start_helloworld(a in number, b in number, c in out number)
AS language java
name 'HelloWorld.add(int,int,int)';

I am not an Oracle expert by any means but I have hit this issue recently so I just wanted to comment. For some reason I can't just leave a comment, So here's my answer.
Comment:
When I get the Ora-29516 error, it comes with a reason description. Is there more to the error when you get it?
Answer:
If your Aurora Assertion error comes with the reason "Uncaught exception System error: java/lang/UnsupportedClassVersionError" =>
I get this error when the version of Java I used to compile the class file isn't the same as the version of Java in Oracle (1.5.0 in 11g). To be sure you match perfectly, let Oracle compile the class for you. You'll get two benefits: 1) You will be sure the Java version matches exactly. 2) You'll have the source code loaded as a database "JAVA SOURCE" object for future reference. For security purposes, you may want to lock it down.
loadjava -user scott/tiger -resolve HelloWorld.java
By using the source file with the resolve option, Oracle will create the source object and compile the code for the class object. If you leave out the -resolve option, Oracle will create the source object and only compile it when it is called. I presume this may have good flexibility options but performance drawbacks.

This error occurs because incompatibility of different java versions in oracle and java compiler.
-> oracle version:
--TO CHECK JAVA VERSION IN ORACLE
SELECT dbms_java.get_ojvm_property(PROPSTRING=>'java.version') FROM dual;
FOLLOW THIS STEPS :
//java
class simple{
public static String world(){
return("Hello Java");
}
}
Insted of loading class Simple load your java directly,
STEP1 :
loadjava -user system/admin simple.java
STEP2:
then > create one procedure
CREATE OR REPLACE PROCEDURE PROC_DEMO as
language java
name 'simple.world()';
/
STEP3:
declare -- Parameter declaration
RESULT VARCHAR2(2000);
begin
-- Please customize initialization
-- Call the procedure/function
RESULT := FUNC_DEMO;
-- Print out the results
dbms_output.put_line( 'RESULT = ' || SUBSTR( RESULT, 1, 255));
end;

Related

Java program runs yet compilation fails

I wrote a Java program whose filename was (intentionally) different from the class I wrote inside the file. The javac command failed as expected on both CMD and WSL. The java command however worked and ran my print statement. I wrote the code intentionally this way so there is no way it was a previously compiled version of the code. The following code was written in a file called "explainJava.java" (notice the filename is different from the class name).
public class explain{
public static void main(String[] args) {
System.out.println("Java is weird");
}
}
I've had to google this myself, but I think I've found an explanation in this article.
According to that source as of Java 11 java is capable of compiling a single source file into memory.
What I conclude from that: When the file is compiled into memory and not written to disk it obviously cannot have a file name. If there is no filename there is no such thing as a wrong filename, therefore the code executes.
Please also note that the restriction of having to name a file like the public class within that file is more of a design decision to make work for the compiler easier/ faster. It is not a physical restriction so to speak. Have a look at the following thread for more details.
If you put this code:
public class explain {
public static void main(String[] args) {
System.out.println("Java is weird");
}
}
into a file named explainJava.java, and then compile it with this:
javac explainJava.java
you will get an error that correctly informs you that your filename ("explainJava") and the class defined inside that file ("explain") do not match:
explainJava.java:1: error: class explain is public, should be declared in a file named explain.java
public class explain{
^
1 error
If you run this command:
$ java explainJava.java
Java is weird
you see expected output, because you're skipping the explicit compilation step (that is, you aren't running javac first) and instead relying on behavior introduced in Java 11 that allows you to compile+run in a single step. Here's an explanation: Does the 'java' command compile Java programs?
So the answer is to either:
rename your file to match the class, so change the filename to "explain.java", or
rename the class to match the file, change public class explain to be public class explainJava

Executing the single code java file using java Vs javac using jdk11

I am using JDK11. Below is my sample class -
public class SayHi {
public static void main(String[] args) {
System.out.println("Hi There");
}
}
I executed the above class with command "java filename.java" for below scenarios
ColumnA -> Class declared as public?
ColumnB -> File name same as class name?
ColumnA ColumnB Result
Yes Yes Yes
No Yes Yes
*Yes No Yes
No No Yes
For all the scenarios, the command executed successfully and I got the result. I get compile-time error for the "Yes-No" case, if I run the "javac" command on the file name.
Why I am not getting the compile-time error when I am executing "java" command on the file name?
I have multiple public classes in a single code file. I am able to execute the file using "java filename.java" command. What I am missing with the compile-time issues when running the file with "java" command. Please help me on this.
The answers to all your questions can be found in JEP 330. I believe the following excerpts provide answers to your questions.
the first class found in the source file is executed
The source file should contain one or more top-level classes, the first of which is taken as the class to be executed
The compiler does not enforce the optional restriction defined at the end of JLS ยง7.6, that a type in a named package should exist in a file whose name is composed from the type name followed by the .java extension
In other words, when you compile a java source code file with javac, the source code file must contain a single, "public" class whose name matches the name of the file. But when you run a java source code file using the java command, the above restriction does not apply.
The class to be executed is the first top-level class found in the source file. It must contain a declaration of the standard public static void main(String[]) method.

Java system class in Oracle DB

I am using java methods in PL/SQL and recently I have stumbled upon this error :
An error was encounterd performing the request operation:
ORA-29548: Release of Java system classes in database (12.1.0.2.171017
1.6) does not match that of the oracle executable (12.1.0.2.180116 1.6)
29548. 00000 = "Java system class reported" %s"
What should I do to make it work?
Make sure the java version you use to compile and the version installed in ORACLE both are same.
select dbms_java.get_ojvm_property ('java.version')
from dual
Although, you can let Oracle do compile the source code directly.
CREATE OR REPLACE AND RESOLVE JAVA SOURCE NAMED "Azam/Azam_Test" as package Azam;
import java.lang.*;
public class Azam_Test {
public static String Azam_Test_Print(String testString) {
System.out.println(testString);
return testString;
}
}

Create Function Command for Derby Database

I am having trouble using Create Function Command for Derby Database.
To start with I tried
CREATE FUNCTION TO_DEGREES(RADIANS DOUBLE) RETURNS DOUBLE
PARAMETER STYLE JAVA NO SQL LANGUAGE JAVA
EXTERNAL NAME 'java.lang.Math.toDegrees'
and then
SELECT TO_DEGREES(3.142), BILLNO FROM SALEBILL
This works absolutely fine.
Now I tried making my own function like this :
package SQLUtils;
public final class TestClass
{
public TestClass()
{
}
public static int addNos(int val1, int val2)
{
return(val1+val2);
}
}
followed by
CREATE FUNCTION addno(no1 int, no2 int) RETURNS int
PARAMETER STYLE JAVA NO SQL LANGUAGE JAVA
EXTERNAL NAME 'SQLUtils.TestClass.addNos'
and then
SELECT addno(3,4), BILLNO FROM SALEBILL
This gives an Exception
Error code -1, SQL state 42X51: The class 'SQLUtils.TestClass' does not exist or is inaccessible. This can happen if the class is not public.
Error code 99999, SQL state XJ001: Java exception: 'SQLUtils.TestClass: java.lang.ClassNotFoundException'.
Line 6, column 1
I have made a jar file of the project containing the above Class. I may be wrong but the conclusion that I can draw from this is that this jar file needs to be in some classpath. But in which classpath and how to add it to a classpath, I am not able to understand.
I tried copying the jar file to jdk\lib folder, jre\lib folder, jdk\jre\lib folder but to no avail.
Can someone please point me in the right direction ?
I am using NetBeans IDE 7.1.2, jdk 1.7.0_09, Derby version 10.8.1.2 in Network mode. The applications and data are on a Server. I access them from Netbeans installed on client computer.

How do a call a Java jar file from Oracle PL/SQL?

My customer wants to be able to call a jar file from Oracle PL/SQL.
Java 1.6, Oracle 11g R2
How do I do this?
I did a bit of research into loadjava (the program that loads classes into Oracle).
Sounded like a pain in the butt.
So I opted to run my jar outside Oracle but called from Oracle DBMS_SCHEDULER by PL/SQL.
Here's how:
BEGIN
DBMS_SCHEDULER.CREATE_PROGRAM (
program_name => 'testjar',
program_type => 'EXECUTABLE',
program_action => 'C:\MYSTUFF\testjar.bat',
enabled => TRUE,
comments => 'test testjar'
);
END;
/
begin
dbms_scheduler.create_job
(job_name => 'testjar_job',
program_name=> 'testjar',
enabled=>true,
auto_drop=>false,
comments=>'Only run immediately by dbms_scheduler.run_job');
end;
/
begin
dbms_scheduler.run_job('testjar_job',TRUE);
end;
/
After playing around I found out that the schema JAR_NAME.jar///ClassName.methodName(.... works.
So for example:
Function do()
return String
AS
LANGUAGE java
NAME 'my_jar.jar///MyClass.myMethod() return oracle.sql.String';
Note that it seems like some jar name files does not work. For example I had problems with a "-" in jar.

Categories