I'm executing the following code in Navicat:
CREATE OR REPLACE AND COMPILE JAVA SOURCE NAMED "util"
AS
import java.io.*;
import java.lang.*;
public class util extends Object
{
public static String exec(String cmd)
{
Runtime.getRuntime().exec(cmd);
return "";
}
}
And it fails with:
ORA-29536: badly formed source: Encountered "<EOF>" at line 1, column 16.
Was expecting:
";" ...
, Time: 0.059000s
The source code compiles correctly with javac, why won't it work in Oracle19?
If you compile it you get the exception:
ORA-29535: source requires recompilation
util:7: error: unreported exception IOException; must be caught or declared to be thrown
Runtime.getRuntime().exec(cmd);
^
1 error
You can fix that (and a few other minor things that don't affect compilation but aren't necessary to include, and you probably don't want to use a quoted identifier):
CREATE OR REPLACE AND COMPILE JAVA SOURCE NAMED util
AS
import java.io.IOException;
public class Util
{
public static String exec(String cmd) throws IOException
{
Runtime.getRuntime().exec(cmd);
return "";
}
}
Then it compiles Oracle 18 db<>fiddle.
Note: this function is a huge security issue for your database and you probably should find an alternative solution rather than allowing arbitrary code execution.
Related
in a file named filename.java
class filename{
public static void main(String[] a){
System.out.println("From filename main method");
}
}
public class ClassName{
public static void main(String[] a){
System.out.println("From First main method");
}
}
Observe below commands:
Command 1:
C:\javaDJ>java filename.java
From filename main method
Command 2:
C:\javaDJ>javac filename.java
filename.java:7: error: class ClassName is public, should be declared in a file named ClassName.java
public class ClassName{
^
1 error
Observation:
command 1 compiles (i assume internally ) and executes successfully.
command 2 throws compilation error.
Problem Statement :
How is java cmd able to compile the file called filename.java, when the file(filename.java) contains a public class (ClassName)which is not named 'filename.java' (the name of the file-name.) ?
To highlight a specific section from the JEP#Launch Single-File Source-Code Programs with regards to the behavior
In source-file mode, execution proceeds as follows:
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.
The feature which enabled you to execute Command 1 successfully was introduced in Java 11. The feature allows you to execute a Java source code file directly using the java interpreter. The source code is compiled in memory and then executed by the interpreter, without producing a .class file on disk. Check this for more information.
The error you got in Command 2 has been there since the beginning of Java.
Many Websites allow a user to type in Java code and run it. How does a program accept Java written externally/at run time and run it?
The only/closest answer i see on StackOverflow is from 5 years ago about Android development that recommended using Janino (Compile and execute arbitrary Java string in Android). Is this still the way to go? Has a better approach (like something built into Java) appeared in the last half decade?
If it helps, I'm building a training app for my students. The code is short (a few methods, maybe 20 lines max) and they must use standard libraries (no need to worry about importing things from maven, etc.).
Like similar online coding sites, I'd like to return the output of the run (or compilation failure).
An example use case:
Webpage says "The code below has an error, try to fix it." A text box contains code. A semicolon is missing.
User modifies the code and presses submit.
The Webpage either returns a compile error message or success.
Another use case would be for me to execute unit tests on the code they submitted and return the result. The point being, I give the user feedback on the code compilation/run.
Here is a small example to use the Java compiler interface
import java.io.File;
import java.lang.reflect.Method;
import java.net.URL;
import java.net.URLClassLoader;
import javax.tools.JavaCompiler;
import javax.tools.ToolProvider;
public class Compiler {
public static void main(String[] args) throws Exception {
// compile the java file
JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
int result = compiler.run(null, null, null, "D:\\development\\snippets\\Test.java");
System.out.println("compiler result " + result);
// load the new class
File classesDir = new File("D:\\development\\snippets\\");
URLClassLoader classLoader = URLClassLoader.newInstance(new URL[] { classesDir.toURI().toURL() });
Class<?> cls = Class.forName("Test", true, classLoader);
// invoke a method of the class via reflection
Object instance = cls.getDeclaredConstructors()[0].newInstance();
Method testMethod = cls.getMethod("test");
String testMethodResult = (String) testMethod.invoke(instance);
System.out.println(testMethodResult);
}
}
And here the test class
public class Test {
public String test() {
return "String from test.";
}
}
Running the Compiler class returns
compiler result 0
String from test.
I am having the following code to get the hash value of String:
package encryption;
import java.security.MessageDigest;
public class MessageDigestExample {
public static void main(String[] args)throws Exception{
String input = "This is a message";
MessageDigest hash = MessageDigest.getInstance("SHA1");
System.out.println("input : " + input);
hash.update(Utils.toByteArray(input));
System.out.println("digest : " + Utils.toHex(hash.digest()));
} }
I am getting this exception at the moment:
Exception in thread "main" java.lang.Error: Unresolved compilation problems:
Utils cannot be resolved
Utils cannot be resolved
I added apache-storm-1.2.2 lib, but does not work,
Any help, please!
You are using the (class?) name Utils, without ever importing it.
You probably lack some import what.ever.Utils statement here.
Beyond that, you get a runtime exception because you try to run code that did not compile. Although some IDEs, like eclipse, allow for that, it is in generally a bad idea, especially when you are a newbie to Java. You should always fix all compiler errors before you try to run a class.
You should import the Utils library you are using. If I am reading it correctly, add the following line underneath the package section:
import org.apache.storm.utils.Utils;
This should resolve the library Utils for you
I want to create c library and use it in my java code on an Linux OS. I'm trying to understand and implement natural library concept.
I'm following this tutorial
http://diglib.stanford.edu:8091/~testbed/doc/JavaUsage/JNI/tutorial.txt
Which is helpful me to understand concept a little. However, I get errors when I try to do it myself. I searced for errors I am getting but none of solutions helped.
Main class code and class for natural library I wrote is as follows:
package natLib;
import natLib.getKeyPressed;
public class main {
public static void main(String[] args) {
getKeyPressed natlab=new getKeyPressed();
char c=natlab.keyboardPressedKey();
}
}
package natLib;
public class getKeyPressed {
static {
System.loadLibrary("natlab");
}
public native char keyboardPressedKey();
}
when I write "javac main.java"
I get errors like
"main.java:6: error: cannot find symbol
getKeyPressed natlab=new getKeyPressed();"
And when I skip for main and just do javac prcess for class with native method, try to obtain a header file
javah -jni getKeyPressed.class
Although there is a file as getKeyPressed.class, I get errors like:
"Exception in thread "main" java.lang.IllegalArgumentException: Not a valid class name: getKeyPressed.class"
I try it without .class extention it says
"Error: Could not find class file for 'getKeyPressed'."
It says that even when I make getKeyPressed class file by copying getKeyPressed.class.
It seems I am making a major mistake, any suggestions to solve this?
javah expects a fully qualified classname. (e.g. natLib.getKeyPressed, not just getKeyPressed)
I'm using this code:
for (final String code : Locale.getISOCountries())
{
//stuff here
}
But on compile I get this error:
[ERROR] Line 21: No source code is available for type java.util.Locale; did you forget to inherit a required module?
And then a stack trace of compiler errors.
I'm doing both of these imports at the beginning of the class:
package com.me.example;
import java.util.Locale;
import java.util.*;
What can be wrong?
In Netbeans i see the autocomplete options and no syntax error for the Locale object...
Something screwy with your setup, the folllowing program works fine for me.
import java.util.*;
import java.util.Locale;
public class Donors {
public static void main (String [] args) {
for (final String code : Locale.getISOCountries()) {
System.out.println (code);
}
}
}
The fact that it's asking for source code leads me to believe that it's trying to compile or run it in some sort of debugging mode. You shouldn't need the source code for java.util.* to compile, that's just bizarre.
See if my simple test program works in your environment, then try looking for something along those lines (debugging options). Final step: compile your code with the baseline javac (not NetBeans).
UPDATE:
Actually, I have found something. If you are creating GWT applications, I don't think java.util.Locale is available on the client side (only the server side). All of the references on the web to this error message point to GWT and its limitations on the client side which are, after all, converted to Javascript goodies, so cannot be expected to support the entire set of Java libraries.
This page here shows how to do i18n on GWT apps and there's no mention of java.util.Locale except on the server side.
Looks like there might be something fishy in your build environment, as Locale.getISOCountries() should work just fine. Try compiling a small test program manually and see if you get the same error.
Definitely try to boil this down to a minimum, three-line program (or so), compile from the command-line, then put that class into your IDE and see if you still get the error, and if not, then change/add one line at a time until you have the original failing program, looking for what causes the problem. I'm thinking maybe some other import in your code is importing a Locale class? Why in the world would it be looking for source code?
See what happens when you compile this from the command-line:
import java.util.*;
public class LocaleTest {
public static void main(String[] args) {
Locale.getISOCountries();
}
}