Getting the hash value of String - java

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

Related

Can't compile java in oracle: badly formed source

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.

Unable to import java.net.URLEncoder in groovy

I'm pretty new in groovy, trying to encode a URL in my first piece of groovy code.
Here is a section of my codeļ¼š
import java.net.URLEncoder
String url = baseUrl + URLEncoder.encode(parameter);
It looks quite the same as many examples I check online but it throws error as below.
General error during canonicalization: Importing [java.net.URLEncoder] is not allowed
java.lang.SecurityException: Importing [java.net.URLEncoder] is not allowed
I have also tried to use the class directly as illustrated here
String url = baseUrl + java.net.URLEncoder.encode(parameter);
and another version:
String url = baseUrl + URLEncoder.encode(parameter);
both throw me error:
General error during canonicalization: Indirect import checks prevents usage of expression
java.lang.SecurityException: Indirect import checks prevents usage of expression
appreciate if any guru can help clear the doubts.
According to http://groovy.codehaus.org/Differences+from+Java, java.net.* package in Groovy is imported by default, which means java.net.URLEncoder is also imported. Use it without import.
Edit: For me, using this Groovy code:
println URLEncoder.encode("URL encoding fine!")
prints URL+encoding+fine%21

JavaCV/OpenCV: cvLoadImage not working

I installed the JavaCV/OpenCV libraries, and I'm having a problem with the basic example code.
According to several examples that I have looked at, this code should load an image:
IplImage image = cvLoadImage("C:\\img.jpg");
But, when I run that I get a "cannot find symbol" error.
Since this is my first time using it, I'm not sure if I messed the install up or not.
According to the newest JavaCV readme, I do have the correct version of OpenCV. I also have all the JavaCV jar files imported. As far as I can tell, I also have all the paths set correctly too.
Anyone know what the problem is?
Edit:
Full code:
import com.googlecode.javacv.CanvasFrame;
import com.googlecode.javacv.cpp.opencv_core.IplImage;
import java.io.File;
public class demo {
public static void main(String[] args)
{
IplImage image = cvLoadImage("C:\\img.jpg");
final CanvasFrame canvas = new CanvasFrame("Demo");
canvas.showImage(image);
canvas.setDefaultCloseOperation(javax.swing.JFrame.EXIT_ON_CLOSE);
}
}
Error when I try to run it:
Exception in thread "main" java.lang.RuntimeException: Uncompilable source code - Erroneous sym type: cvLoadImage
at javacv.demo.main(demo.java:17)
Java Result: 1
Seems like it is claiming cvLoadImage doesn't take a string as an argument.
A walk around that i find for you is to load the image by ImageIO and passe it later to IplImage
e.g.:
BufferedImage img = ImageIO.read(new File("C:\\img.jpg") );
IplImage origImg = IplImage.createFrom(img);
This solved my problem: import static org.bytedeco.javacpp.opencv_imgcodecs.*;
You have to add this import statement:
import static org.bytedeco.javacpp.opencv_imgcodecs.cvLoadImage;
This is required so that the static method cvLoadImage can be used without using the class name.
You have to import com.googlecode.javacv.cpp.opencv_highgui.*;
With javacv 0,9 you have to import static org.bytedeco.javacpp.opencv_highgui.*;
I got the same error then, i imported the following package, problem solved.
import static com.googlecode.javacv.cpp.opencv_highgui.*;
This might be old but for those who stumbled upon this problem like me just now,
here is how I solved it and why:
First OP's error: Exception in thread "main" java.lang.RuntimeException: Uncompilable source code - Erroneous sym type: cvLoadImage at javacv.demo.main(demo.java:17)
This indicates that the compiler cannot find the cvLoadImage method that you are trying to call.
cvLoadImage is a static method under JavaCPP.
Specifically it is a static method under opencv_imgcodecs class.
To solve this issue one must first specify the import of the opencv_imgcodecs class.
This can be done by adding the import:
import static org.bytedeco.javacpp.opencv_imgcodecs.cvLoadImage;
This in turn would result for the opencv_imgcodecs class to be usable within your class along with its static methods and other functions.
I hope this helps.
Got the same problem recently.
if you are using javacv-0.10 (more recent at the moment), import manually this one:
import static org.bytedeco.javacpp.opencv_highgui.*;
but the source of JRE of the project should be higher than 1.5
In my case, the problem happened when the squeegee in debug mode.
Try to run in normal mode.

Exception in thread "main" java.lang.NoClassDefFoundError: wrong name

My file directory:
project/src/m2mcom/entities/AutomatedTelnetClient.java
/web/Simple.java
/org/apache/commons/net/telnet/TelnetClient.java
The source code of the Simple.java:
package m2mcom.web;
import m2mcom.entities.AutomatedTelnetClient;
import java.util.*;
import java.io.*;
public class Simple {
public static void main(String [] args) {
try {
AutomatedTelnetClient telnet = new AutomatedTelnetClient();
String answer = telnet.request();
System.out.println(answer);
} catch (Exception e) {
System.err.println("Error");
}
}
}
And when I execute Simple.class, without any errors of compilation, I get this error message:
C:\Users\Victor\Desktop\project2\src\m2mcom\web>java Simple
Exception in thread "main" java.lang.NoClassDefFoundError: Simple (wrong name: m
2mcom/web/Simple)
Does anyone know how to solve this?
You're executing the command in the wrong folder, with the wrong classname. You need to use the fully qualified name (FQN) when running a Java class. And of course, you have to be in the right directory. In your example, the FQN of your class is m2mcom.web.Simple (combination of the package m2mcom.web and the simple name Simple).
As far as deducing the right directory, your classes are stored in a hierarchical folder structure, which basically starts in C:\Users\Victor\Desktop\project2\src.
So to correctly execute your program, from C:\Users\Victor\Desktop\project2\src, do;
java m2mcom.web.Simple
package m2mcom.web;
remove above line and recompile it.
when you run your code in netbeans it including in a m2mcom.web package.that is not in your class file.
So you have to be in the directory right above the package name when you execute the java command which should be in the form packagename.classname without the .class suffix.

Weird error with Locale.getISOCountries()

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();
}
}

Categories

Resources