GWT Compile Issues in Java (Google App Engine) - java

Im having a weird compile issue using the Google App engine in java using eclipse. When I try to GWT Compile my code I get an error that is as follows:
Compiling module beer.SQLBeer
Validating newly compiled units
Ignored 1 unit with compilation errors in first pass.
Compile with -strict or with -logLevel set to TRACE or DEBUG to see all errors.
Finding entry point classes
[ERROR] Errors in 'file:/C:/Users/Mark/workspace/SQLBeer/src/beer/client/SQLBeer.java'
[ERROR] Line 12: The import com.google.appengine.api.rdbms cannot be resolved
[ERROR] Line 13: The import com.google.apphosting cannot be resolved
[ERROR] Line 14: The import com.google.cloud cannot be resolved
[ERROR] Line 18: ServersServlet cannot be resolved to a type
[ERROR] Line 22: The method doPost(HttpServletRequest, HttpServletResponse) of type SQLBeer must override or implement a supertype method
[ERROR] Line 26: Connection cannot be resolved to a type
[ERROR] Line 28: AppEngineDriver cannot be resolved to a type
[ERROR] Line 29: Connection cannot be resolved to a type
[ERROR] Unable to find type 'beer.client.SQLBeer'
[ERROR] Hint: Previous compiler errors may have made this type unavailable
[ERROR] Hint: Check the inheritance chain from your module; it may not be inheriting a required module or a module may not be adding its source path entries properly
Exception in thread "UnitWriteThread"
I am not sure why it cannot resolve the imports, and this is preventing me from deploying my code onto the Google app engine. I feel because it is not playing nicely with my imports its the same reason im getting the error
[ERROR] Line 22: The method doPost(HttpServletRequest, HttpServletResponse) of type SQLBeer must override or implement a supertype method
I am really new to using GWT and the Google App Engine for Eclipse, but im trying to access a database my team created using Google Cloud SQL. And I feel Like im getting close, if i can over come these errors.
The Project Code
package beer.client;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.DriverManager;
import java.sql.SQLException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.google.appengine.api.rdbms.AppEngineDriver;
import com.google.apphosting.utils.servlet.ServersServlet;
import com.google.cloud.sql.jdbc.Connection;
#SuppressWarnings("serial")
public class SQLBeer extends ServersServlet {
#Override
public void doPost(HttpServletRequest req, HttpServletResponse resp)
throws IOException {
PrintWriter out = resp.getWriter();
Connection c = null;
try {
DriverManager.registerDriver(new AppEngineDriver());
c = (Connection) DriverManager
.getConnection("jdbc:google:rdbms://asu.edu:cst433team1:team1db/mysql");
String fname = req.getParameter("fname");
String content = req.getParameter("content");
/**
* This code appears to do the web form fun
*/
// if (fname == "" || content == "") {
// out.println("<html><head></head><body>You are missing either a message or a name! Try again! Redirecting in 3 seconds...</body></html>");
// } else {
// String statement = "INSERT INTO entries (guestName, content) VALUES( ? , ? )";
// PreparedStatement stmt = c.prepareStatement(statement);
// stmt.setString(1, fname);
// stmt.setString(2, content);
// int success = 2;
// success = stmt.executeUpdate();
// if (success == 1) {
// out.println("<html><head></head><body>Success! Redirecting in 3 seconds...</body></html>");
// } else if (success == 0) {
// out.println("<html><head></head><body>Failure! Please try again! Redirecting in 3 seconds...</body></html>");
// }
// }
} catch (SQLException e) {
e.printStackTrace();
} finally {
if (c != null)
try {
c.close();
} catch (SQLException ignore) {
}
}
//resp.setHeader("Refresh", "3; url=/beer.jsp");
}
/**
* #param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
}
}
Any Suggestion on what I could do to fix these errors? Ive tried different Imports but they all seem to lead to the same issues in the GWT Compiler.
EDIT: I changed the extend to HttpServlet and now the error is a little different
Compiling module beer.SQLBeer
Validating newly compiled units
Ignored 1 unit with compilation errors in first pass.
Compile with -strict or with -logLevel set to TRACE or DEBUG to see all errors.
Finding entry point classes
[ERROR] Errors in 'file:/C:/Users/Mark/workspace/SQLBeer/src/beer/client/SQLBeer.java'
[ERROR] Line 13: The import com.google.appengine.api.rdbms cannot be resolved
[ERROR] Line 14: The import com.google.cloud cannot be resolved
[ERROR] Line 26: Connection cannot be resolved to a type
[ERROR] Line 28: AppEngineDriver cannot be resolved to a type
[ERROR] Line 29: Connection cannot be resolved to a type
[ERROR] Unable to find type 'beer.client.SQLBeer'
[ERROR] Hint: Previous compiler errors may have made this type unavailable
[ERROR] Hint: Check the inheritance chain from your module; it may not be inheriting a required module or a module may not be adding its source path entries properly
Exception in thread "UnitWriteThread" java.lang.RuntimeException: Unable to read from byte cache

First, check that this isn't a classpath issue - meaning you don't have all the required jars in the lib directory and on the classpath.
If that fails, make sure that this code isn't Client side(guessing from your package name), which will gets compiled to javascript. You dont want this to happen for database connection code so you should use this code on the server side.
See the Documentation on Client side & Server side code.

Related

Java compiler ignores source files generated by annotations processor

I'm messing up with an annotations processor. It should be able to generate source files with Java classes, but for some reason when I start a compilation, compiler ends with an error that it can not find my generated class.
Even weirder is that after this error message I check my filesystem, the file is there and also has correct content. When I start the compilation second time, compiler is happy and ends without any errors.
To generate a file I'm using this piece of code:
private void writeBarFile() {
Filer filer = processingEnv.getFiler();
Messager messager = processingEnv.getMessager();
try {
// this should prepare a new Java source file
final JavaFileObject fooFileObject = filer.createSourceFile("sample.module.GeneratedClass");
// Here we open a writer
try (Writer writer = fooFileObject.openWriter()) {
// And write some content
writer.append("package sample.module;\n").append("public class GeneratedClass {}");
}
} catch (IOException ex) {
messager.printMessage(Diagnostic.Kind.ERROR, ex.getMessage());
}
}
By calling filer.createSourceFile I should get an instance of new Java file which will be generated.
Than I have another module, where I have my annotation used and where is also used a generated class.
package sample.module;
import sample.annotation.Foo;
#Foo(bar = "test1")
public class Sample1 {
// This class is generated by annotations processor
GeneratedClass t;
}
The output from the compiler after first compilation:
[INFO] -------------------------------------------------------------
[ERROR] COMPILATION ERROR :
[INFO] -------------------------------------------------------------
[ERROR] /F:/annotations_processor_example/sample-module/src/main/java/sample/module/Sample1.java:[10,3] cannot find symbol
symbol: class GeneratedClass
location: class sample.module.Sample1
[INFO] 1 error
[INFO] -------------------------------------------------------------
But it does not make sense because the file is already generated and I can see i on the file system.
Is there anything that must be somehow enabled or am I just missing something?
I have tried multiple Java versions (11, 17 and 19), but it's same error everywhere.
There is a sample project on GitHub.
I will appreciate any help or just an explanation what am I doing wrong.

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.

How to fix "java.lang.NoSuchMethodError" for geoip2 java running under Hive

I am having trouble when executing a UDF with geoip2 (maxmind) as dependency under Hive 2.3.4 (java 8) the same code works fine under older versions of hive that use java 7 and also under Presto that use java 8.
I have tried using exact dependencies (maven) and manual compiling, multiple versions of hive, reducing the code and dependencies to bare minimum
import org.apache.hadoop.hive.ql.exec.UDF;
import org.apache.hadoop.io.Text;
import java.io.File;
import java.io.IOException;
import java.net.InetAddress;
import com.maxmind.geoip2.record.City;
import com.maxmind.geoip2.DatabaseReader;
import com.maxmind.geoip2.model.CityResponse;
import com.maxmind.geoip2.exception.GeoIp2Exception;
public final class CityName extends UDF {
public String evaluate(String dbFile) throws IOException,GeoIp2Exception {
File database = new File(dbFile);
InetAddress ipAddress = InetAddress.getByName("127.0.0.1");
DatabaseReader reader = new DatabaseReader.Builder(database).build();
return "Database: "+dbFile.toString();
}
}
Error message
2019-05-21T16:31:56,631 ERROR [ce3bca33-87aa-4468-b1ed-7080e95efb2d main([])]: ql.Driver (SessionState.java:printError(1126)) - FAILED: SemanticException [Error 10014]: Line 1:7 Wrong arguments ''/root/GeoIP2-City.mmdb'': org.apache.hadoop.hive.ql.metadata.HiveException: Unable to execute method public java.lang.String com.example.hive.udf.CityName.evaluate(java.lang.String) throws java.io.IOException,com.maxmind.geoip2.exception.GeoIp2Exception,java.net.UnknownHostException with arguments {/root/GeoIP2-City.mmdb}:com.maxmind.geoip2.DatabaseReader.(Lcom/maxmind/geoip2/DatabaseReader$Builder;Lcom/maxmind/geoip2/DatabaseReader$1;)V
org.apache.hadoop.hive.ql.parse.SemanticException: Line 1:7 Wrong arguments ''/root/GeoIP2-City.mmdb'': org.apache.hadoop.hive.ql.metadata.HiveException: Unable to execute method public java.lang.String com.example.hive.udf.CityName.evaluate(java.lang.String) throws java.io.IOException,com.maxmind.geoip2.exception.GeoIp2Exception,java.net.UnknownHostException with arguments {/root/GeoIP2-City.mmdb}:com.maxmind.geoip2.DatabaseReader.(Lcom/maxmind/geoip2/DatabaseReader$Builder;Lcom/maxmind/geoip2/DatabaseReader$1;)V
at org.apache.hadoop.hive.ql.parse.TypeCheckProcFactory$DefaultExprProcessor.process(TypeCheckProcFactory.java:1367)
at org.apache.hadoop.hive.ql.lib.DefaultRuleDispatcher.dispatch(DefaultRuleDispatcher.java:90)
at org.apache.hadoop.hive.ql.lib.DefaultGraphWalker.dispatchAndReturn(DefaultGraphWalker.java:105)
.......
Caused by: java.lang.NoSuchMethodError: com.maxmind.geoip2.DatabaseReader.(Lcom/maxmind/geoip2/DatabaseReader$Builder;Lcom/maxmind/geoip2/DatabaseReader$1;)V
at com.maxmind.geoip2.DatabaseReader$Builder.build(DatabaseReader.java:160)
at com.example.hive.udf.CityName.evaluate(CityName.java:20)
... 47 more
We hit a very similar issue to that. In our case it turned out in the classpath of the Hive deployment there is an obsolete version of maxmind-db. This might even be by default on newer releases of Hive, but I haven't confirmed yet.
To fix, just shade any references to com.maxmind. Also, just to be safe, shade any any references to fasterxml.jackson as well - that's one of the core dependencies of GeoIP2 and conflicts of that in the classpath tend to cause weird runtime issues like this one. You can see an example of that in a PR addressing such issue in one of the Hive UDF GeoIP libs.

Assert in selenium in not working for me

'Assert cannot be resolved' is displayed when i tries to insert an Assert command
String menu1 = driver.findElement(By.xpath("value")).getText();
System.out.println(menu1);
if(Assert.assertEquals(menu1,"About Us"))
{
}
The error says it all :
'Assert cannot be resolved'
The reason for this error and the solution can be either of the following:
import org.testng.Assert; is missing.
TestNG jars were not downloaded properly. You may need to download the TestNG jars and reconfigure.
It is worth mentioning that Assert.assertEquals() method returns void. So the compiler should be complaining about the following line of code:
if(Assert.assertEquals(menu1, "About Us"))
If you have a requirement to catch the Pass or Fail status, wrap up the assertion in a try-catch block with Throwable t as follows:
String menu1 = driver.findElement(By.xpath("value")).getText();
System.out.println(menu1);
try {
Assert.assertEquals(menu1, "About Us")
System.out.println("Assertion Successful");
} catch (Throwable t) {
System.out.println("Exception raised in Assertion");
}
Output on success will be:
Assertion Successful
Output on failure will be:
Exception raised in Assertion
please add the below import statement to your code. it may resolve your issue.
import org.testng.Assert;
assertEquals doesn't return any value.
You can't put inside a if clause. please remove if statement.
just add only the assert statement.
Assert.assertEquals(menu1,"About Us");
No need to use if with Assert, try it seperately. Also check whether testng is there are not,
use
import org.testng.*;
or
import org.testng.Assert;
Like this code:
String menu1 = driver.findElement(By.xpath("value")).getText();
System.out.println(menu1);
Assert.assertEquals(menu1,"About Us");
Just Right Click on the folder which that we need to implement assertion just right click >>> Build Path >>> Add Libraries >>> TestNG

GWTQuery UI Error with .dialog()

I am trying to use dialogs with GWTQuery-UI in Kepler and can't figure out what is wrong. I followed as best I could the instructions found at https://code.google.com/p/gwtquery-ui/wiki/GettingStarted. I have the following code setup
gwt.xml
<inherits name='gwtquery.plugins.UiGoogleCdn' />
<inherits name='gwtquery.plugins.UiEmbedded' />
I have also tried the gwtquery.plugins.Ui but that hasn't solved things either.
In my java file I have the following
import static com.google.gwt.query.client.GQuery.*;
import static gwtquery.plugins.ui.Ui.Ui;
import com.google.gwt.query.client.Function;
I have the following in the html for the project.
<div style="display: none;" class="gwt-DlgBox">This is a test to see if this shows up</div>
In the main logic I have the following click handler
#UiHandler("btnShow")
public void btnShow(ClickEvent event)
{
int x = 0;
try
{
x = $(".gwt-DlgBox").length();
$(".gwt-DlgBox").as(Ui).dialog();
}
catch(Throwable e)
{
e=e; // just so I can debug break here.
}
}
When I debug I get the error: Line 80: Referencing method 'com.google.gwt.query.client.Function.f(Lcom/google/gwt/user/client/Event;Ljava/lang/Object;)': unable to resolve method
When I step through the code x = 1; which is correct, but the .dialog() line fails with the error mentioned above.
When I try and compile it I get the following error
[ERROR] Errors in 'jar:file:/C:/src/gwtquery-ui-r146.jar!/gwtquery/plugins/ui/UiWidget.java'
[ERROR] Line 80: Referencing method 'com.google.gwt.query.client.Function.f(Lcom/google/gwt/user/client/Event;Ljava/lang/Object;)': unable to resolve method
Any help would be appreciated. Yes I am new to GWT and just don't understand what I am missing.
Thanks.
If you are using gwtquery 1.4.2, then it could be the issue. I resolved it by downgrading to gwtquery 1.3.3.

Categories

Resources