Where to copy a file so I can open it from GWT Eclipse Google Plugin in dev mode? - gwt

I'm learning GWT with Google Eclipse Plugin, and I want to use some configuration file (generatorConfig.xml) from my server code, how do I upload it to the default devmode server? how do I open it from my Java code?
I've put the generatorConfig.xml file in the war/WEB-INF/deploy/[my app]/ but I can't open it...
String line;
BufferedReader in;
in = new BufferedReader(new FileReader("generatorConfig.xml"));
line = in.readLine();
I get this stack trace:
java.io.FileNotFoundException: generatorConfig.xml (No such file or directory)
at java.io.FileInputStream.open(Native Method)
at java.io.FileInputStream.<init>(FileInputStream.java:146)
at java.io.FileInputStream.<init>(FileInputStream.java:101)
at java.io.FileReader.<init>(FileReader.java:58)
at bo.rowen.server.GreetingServiceImpl.greetServer(GreetingServiceImpl.java:50)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)

You need to put it in /war/WEB-INF/ folder. Then you can:
ServletContext context = getServletContext();
InputStream is = context.getResourceAsStream("/WEB-INF/generatorConfig.xml");

Finally, I solved my problem including the file in the src folder and loading just with the file name, (my file now is named configuration.xml):
String resource = "configuration.xml";
try {
reader = Resources.getResourceAsReader(resource);
sqlSessionFactory = new SqlSessionFactoryBuilder().build(reader);
} catch (IOException e) {
e.printStackTrace();
}

Related

Springboot use FileInputStream to read file(chinese name) in linux Failed

//filePath:/root/pcl/代码安全审计报告.xlsx
File file = new File(filePath);
InputStream is = new FileInputStream(file);
is.close();
Charset.defaultCharset().name() is "UTF-8"
System.getProperty("file.encoding") is "UTF-8" too.
exception:
java.io.FileNotFoundException: /root/pcl/���밲ȫ��Ʊ���.xlsx (No such file or directory)
at java.io.FileInputStream.open0(Native Method)
at java.io.FileInputStream.open(FileInputStream.java:195)
at java.io.FileInputStream.(FileInputStream.java:138)

integrate an ant project

I want to run a following project:
https://gforge.inria.fr/plugins/mediawiki/wiki/melinda/index.php/RDF_keys
In the project page, they resume the steps as the following:
1-git clone https://gforge.inria.fr/git/melinda/melinda.git
2-cd melinda/trunk/rdfifd/
3-ant jar
4- java -jar pseudo-keys.jar -t -i /path/to/rdf_file -o /path/to/output/file
So how can i integrate and run this project in an eclipse project?
I already following these setp on a linux command line and it is work well and pseudo-keys.jar is generated. I take this .jar and In my laptop i write the following code:
public class TestDriver {
/** Executes a command */
public static void execute(String cmd, File folder) throws Exception {
Process p = Runtime.getRuntime().exec(cmd, null, folder);
BufferedReader bri = new BufferedReader(new InputStreamReader(p.getInputStream()));
BufferedReader bre = new BufferedReader(new InputStreamReader(p.getErrorStream()));
String s1, s2 = null;
while (null != (s1 = bri.readLine()) || null != (s2 = bre.readLine())) {
if (s1 != null) System.out.println(s1);
if (s2 != null) System.err.println(s2);
}
p.waitFor();
}
public static void main(String[]args) throws Exception{
String cmd = "java -jar C:/Users/user/Desktop/pseudo-keys.jar -t -i C:/Users/user/Desktop/randomLocation/datasetLocation/restaurant1.rdf -o C:/Users/user/Desktop/randomLocation/key.xml";
execute(cmd, null);
}
The execution give the following error:
java.lang.NoClassDefFoundError: org/apache/commons/cli/ParseException
at java.lang.Class.getDeclaredMethods0(Native Method)
at java.lang.Class.privateGetDeclaredMethods(Class.java:2701)
at java.lang.Class.privateGetMethodRecursive(Class.java:3048)
at java.lang.Class.getMethod0(Class.java:3018)
at java.lang.Class.getMethod(Class.java:1784)
at sun.launcher.LauncherHelper.validateMainClass(LauncherHelper.java:544)
at sun.launcher.LauncherHelper.checkAndLoadMain(LauncherHelper.java:526)
Caused by: java.lang.ClassNotFoundException: org.apache.commons.cli.ParseException
at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:331)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
... 7 more
Error: A JNI error has occurred, please check your installation and try again
Exception in thread "main"
Any help please to run the code? Where is my mistake?
Thank you
Copy the URI https://gforge.inria.fr/git/melinda/melinda.git to the clipboard
In the Git Repositories click the Clone a Git Repository button and follow the wizard (if the dialog is not prefilled, enter the URI manually)
Right-click the node melinda [master] > Working Tree > trunk > rdfifd and choose Import Projects... and follow the import wizard
In the Package Explorer or Project Explorer open the file rdfifd/build.xml via double-click
In the Outline view right-click the target jar and choose Run As > Ant Build
On the command line run java -jar pseudo-keys.jar -t -i /path/to/rdf_file -o /path/to/output/file. Alternatively, you can skip step 4 and 5 and run fr.inrialpes.exmo.rdfkeys.KeyExtraction as Java application with the necessary arguments (Run > Run Configurations..., in tab Arguments field Program arguments).

Talend - The import org.apache cannot be resolved

I've created a custom Talend component, which at certain step connects to an external Http service. For that, I'm using org.apache.commons.httpclient through javajet imports. I've seen the modules already exist in the Modules view. Nevertheless, when running a job the console outputs:
Execution failed : Failed to generate code.
[----------
1. ERROR in /Users/frb/Downloads/TOS_DI-20160510_1709-V6.2.0/workspace/.JETEmitters/src/org/talend/designer/codegen/translators/ngsi/orion/TOrionAppendBeginJava.java (at line 14)
import org.apache.commons.httpclient.*;
^^^^^^^^^^
The import org.apache cannot be resolved
----------
2. ERROR in /Users/frb/Downloads/TOS_DI-20160510_1709-V6.2.0/workspace/.JETEmitters/src/org/talend/designer/codegen/translators/ngsi/orion/TOrionAppendBeginJava.java (at line 15)
import org.apache.commons.httpclient.methods.*;
^^^^^^^^^^
The import org.apache cannot be resolved
----------
3. ERROR in /Users/frb/Downloads/TOS_DI-20160510_1709-V6.2.0/workspace/.JETEmitters/src/org/talend/designer/codegen/translators/ngsi/orion/TOrionAppendBeginJava.java (at line 16)
import org.apache.commons.httpclient.params.HttpMethodParams;;
^^^^^^^^^^
The import org.apache cannot be resolved
----------
3 problems (3 errors)
]
Any hints about how to fix this issue? My Talend version is 6.2.0.
EDIT 1
This is my begin code:
<%# jet
imports="
org.talend.core.model.process.INode
org.talend.core.model.process.ElementParameterParser
org.talend.core.model.metadata.IMetadataTable
org.talend.core.model.metadata.IMetadataColumn
org.talend.core.model.process.IConnection
org.talend.core.model.process.IConnectionCategory
org.talend.designer.codegen.config.CodeGeneratorArgument
org.talend.core.model.metadata.types.JavaTypesManager
org.talend.core.model.metadata.types.JavaType
java.util.List
java.util.Map
org.apache.commons.httpclient.*
org.apache.commons.httpclient.methods.*
org.apache.commons.httpclient.params.HttpMethodParams
"
%>
<%
// Get the CID
CodeGeneratorArgument codeGenArgument = (CodeGeneratorArgument) argument;
INode node = (INode)codeGenArgument.getArgument();
String cid = node.getUniqueName();
// Get the component parameters
String orionEndpoint = ElementParameterParser.getValue(node, "__ORION_ENDPOINT__");
String authEndpoint = ElementParameterParser.getValue(node, "__AUTH_ENDPOINT__");
String authUsername = ElementParameterParser.getValue(node, "__AUTH_USERNAME__");
String authPassword = ElementParameterParser.getValue(node, "__AUTH_PASSWORD__");
String entityIdField = ElementParameterParser.getValue(node, "__ENTITY_ID_FIELD__");
String entityTypeField = ElementParameterParser.getValue(node, "__ENTITY_TYPE_FIELD__");
String defaultEntityType = ElementParameterParser.getValue(node, "__DEFAULT_ENTITY_TYPE__");
String ignoredFilds = ElementParameterParser.getValue(node, "__IGNORED_FIELDS__");
%>
System.out.println("I am the begin section");
HttpClient client = new HttpClient();
PostMethod method = new PostMethod(<%=authEndpoint%>);
method.setRequestHeader(new Header("Content-Type", "application/json"));
method.setRequestBody("{\"username\":\"" + <%=authUsername%> + "\",\"password\":\"" + <%=authPassword%> + "\"}");
try {
int statusCode = client.executeMethod(method);
if (statusCode != HttpStatus.SC_OK) {
System.err.println("Method failed: " + method.getStatusLine());
} // if
byte[] responseBody = method.getResponseBody();
System.out.println(new String(responseBody));
} catch (HttpException e) {
System.err.println("Fatal protocol violation: " + e.getMessage());
e.printStackTrace();
} catch (IOException e) {
System.err.println("Fatal transport error: " + e.getMessage());
e.printStackTrace();
} finally {
method.releaseConnection();
} // try
EDIT 2
I've added the following to my Component Descriptor file:
<IMPORTS>
<IMPORT
NAME="commons-httpclient"
MODULE="commons-httpclient-3.1.jar"
REQUIRED="true"
/>
</IMPORTS>
Now, in the modules view I'm able to see the following:
Sadly, the component outputs the same errors.
EDIT 3
After removing the imports and using fully qualified names, as suggested by #Balazs Gunics, the code seems to be generated. Nevertheless, some other errors related to commons-httpclient arise at running time:
Starting job job_tOrionAppend at 08:20 21/06/2016.
[statistics] connecting to socket on port 3916
[statistics] connected
I am the begin section
Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/commons/logging/LogFactory
at org.apache.commons.httpclient.HttpClient.<clinit>(HttpClient.java:66)
at iotp_talend_connectors.job_torionappend_0_1.job_tOrionAppend.tMysqlInput_1Process(job_tOrionAppend.java:854)
at iotp_talend_connectors.job_torionappend_0_1.job_tOrionAppend.tMysqlConnection_1Process(job_tOrionAppend.java:422)
at iotp_talend_connectors.job_torionappend_0_1.job_tOrionAppend.runJobInTOS(job_tOrionAppend.java:1355)
at iotp_talend_connectors.job_torionappend_0_1.job_tOrionAppend.main(job_tOrionAppend.java:1212)
Caused by: java.lang.ClassNotFoundException: org.apache.commons.logging.LogFactory
at java.net.URLClassLoader$1.run(URLClassLoader.java:372)
at java.net.URLClassLoader$1.run(URLClassLoader.java:361)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:360)
at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
[statistics] disconnected
[statistics] disconnected
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
... 5 more
Job job_tOrionAppend ended at 08:20 21/06/2016. [exit code=1]
So in the begin.javajet code of yours the following code will only import these libraries for the code generation itself. But you need them to the generated code.
Generating java using java makes it hard to oversee this.
<%# jet
imports="
org.apache.commons.httpclient.*
org.apache.commons.httpclient.methods.*
org.apache.commons.httpclient.params.HttpMethodParams
So what you need is to have these imports added to the generated code. Well that is not really possible :( https://www.talendforge.org/forum/viewtopic.php?id=3670 To do that you need to modify the xml descriptor for your component.
So your imports are right. All you have to do is make sure you use the fully qualified names. I.e.: This piece of code:
System.out.println("I am the begin section");
HttpClient client = new HttpClient();
PostMethod method = new PostMethod(<%=authEndpoint%>);
Have to be rewritten to look like this:
System.out.println("I am the begin section");
org.apache.commons.httpclient.HttpClient client =
new org.apache.commons.httpclient.HttpClient();
org.apache.commons.httpclient.methods.PostMethod method =
new org.apache.commons.httpclient.methods.PostMethod(<%=authEndpoint%>);
Yes, it would be way more elegant if we could import the classes and use them.

StringTemplate : how to import from a jar?

I have a case where I am loading a string template group from a file contained in a jar.
This works fine using the following mechanism:
final String urlName = new StringBuilder()
.append("jar:file:").append(templateJar.getAbsolutePath()).append("!")
.append(templateFileName).toString();
final URL url;
try {
url = new URL(urlName);
} catch (MalformedURLException ex) {
throw new GeneratorException("bad manifest url", ex);
}
final STGroup stg = new STGroupFile(url, "US-ASCII", '<', '>');
The difficulty comes in when the template file contains an
...
import "../../dataTypeMaps.stg"
...
String template fails with the following:
can't load group file jar:file:/home/phreed/.m2/repository/edu/vanderbilt/isis/druid/druid-template/2.0.0/druid-template-2.0.0.jar!/template/src/main/java/sponsor/orm/ContractCreator.stg
Caused by: java.lang.IllegalArgumentException: No such group file: ../../dataTypeMaps.stg
at org.stringtemplate.v4.STGroupFile.<init>(STGroupFile.java:69)
at org.stringtemplate.v4.STGroup.importTemplates(STGroup.java:570)
at org.stringtemplate.v4.compiler.GroupParser.group(GroupParser.java:199)
at org.stringtemplate.v4.STGroup.loadGroupFile(STGroup.java:619)
at org.stringtemplate.v4.STGroupFile.load(STGroupFile.java:139)
at org.stringtemplate.v4.STGroupFile.load(STGroupFile.java:128)
at org.stringtemplate.v4.STGroup.lookupTemplate(STGroup.java:237)
at org.stringtemplate.v4.STGroup.getInstanceOf(STGroup.java:172)
at edu.vanderbilt.isis.druid.generator.Generator.build(Generator.java:215)
at edu.vanderbilt.isis.druid.generator.DruidMojo.execute(DruidMojo.java:193)
at org.apache.maven.plugin.DefaultBuildPluginManager.executeMojo(DefaultBuildPluginManager.java:101)
at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:209)
at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:153)
at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:145)
at org.apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProject(LifecycleModuleBuilder.java:84)
at org.apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProject(LifecycleModuleBuilder.java:59)
at org.apache.maven.lifecycle.internal.LifecycleStarter.singleThreadedBuild(LifecycleStarter.java:183)
at org.apache.maven.lifecycle.internal.LifecycleStarter.execute(LifecycleStarter.java:161)
at org.apache.maven.DefaultMaven.doExecute(DefaultMaven.java:320)
at org.apache.maven.DefaultMaven.execute(DefaultMaven.java:156)
at org.apache.maven.cli.MavenCli.execute(MavenCli.java:537)
at org.apache.maven.cli.MavenCli.doMain(MavenCli.java:196)
at org.apache.maven.cli.MavenCli.main(MavenCli.java:141)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at org.codehaus.plexus.classworlds.launcher.Launcher.launchEnhanced(Launcher.java:290)
at org.codehaus.plexus.classworlds.launcher.Launcher.launch(Launcher.java:230)
at org.codehaus.plexus.classworlds.launcher.Launcher.mainWithExitCode(Launcher.java:409)
at org.codehaus.plexus.classworlds.launcher.Launcher.main(Launcher.java:352)
Is it possible to set things up with the jar so that the import will work?
The above approach works fine when there is no jar involved.
The simple answer is that the path to the imported file is wrong
...
import "dataTypeMaps.stg"
...
The import will cause the file to be looked for starting at the root of the jar.
The above import would amount to the file being placed at...
final String urlName = new StringBuilder()
.append("jar:file:").append(templateJar.getAbsolutePath()).append("!")
.append("dataTypeMaps.stg").toString();
Why the behavior is different than when the template group file is on the native file system I do not know.
In order to get this to work I changed the classpath to include the jar file.
As this was done in the context of a Maven plugin, the plugin needs to change the classpath dynamically. This was done with the following code...
public void setTemplateJarName(String templateJarName) throws GeneratorException {
this.templateJarName = templateJarName;
final Thread ct = Thread.currentThread();
final ClassLoader pcl = ct.getContextClassLoader();
URL[] nurl;
try {
nurl = new URL[]{ new URL("file://"+templateJarName) };
} catch (MalformedURLException ex) {
throw new GeneratorException("could not load template jar", ex);
}
final URLClassLoader ucl = new URLClassLoader(nurl, pcl);
ct.setContextClassLoader(ucl);
}
Double check your templates are really in your jar.
Use the following code :
If the templates are dispatched in a tree like this:
/-->resources
+--> c/ (many .st and .stg files)
+--> cpp/ (many .st and .stg files)
+--> java/ (many .st and .stg files)
+--> c.stg
+--> cpp.stg
+--> java.stg
The content of java.stg is:
group Java;
import "java"
doNothing() ::= <<>>
To load all the files in one call :
URL url = getClass().getResource( "/resources/" + templateName );
STGroup group = new STGroupFile( url, "utf-8", '<', '>' );
In my case templateName equals c.stg, cpp.stg or java.stg.
The relative path only works on the file system. If you want to import a template from the classpath, use the fully qualified name. This is the same, as when you would load the file from the classpath yourself, using Class::getResource(). Using the fully qualified name, also works for the filesystem.
So, assuming there are two template files:
src/main/resources/util/date.stg
src/main/resources/generator/class.stg
Then in class.stg you use the fully qualified name:
import "util/date.stg"

Java dynamic class loading fails on windows, but working fine on linux

I am trying to load a class dynamically from a jar file. It worked fine on a Ubuntu linux box ( Sun Java Version 1.6.0_24 (b07).
When I tried to run the same thing on Windows (Windows 7, Java version "1.6.0_14") it fails with Class Not Found exception.
Following is code :
try {
String jarFile = "/sqljdbc4.jar";
File newf = new File(jarFile);
System.out.println(newf.getAbsolutePath());
System.out.println("File exists ? :" + newf.exists());
String urlPath = "jar:file://" + newf.getAbsolutePath() + "!/";
System.out.println(urlPath);
ClassLoader cur = Thread.currentThread().getContextClassLoader();
URL[] jarUrlArray = { new URL(urlPath) };
URLClassLoader cl = URLClassLoader.newInstance(jarUrlArray, cur);
Class c = Class.forName(
"com.microsoft.sqlserver.jdbc.SQLServerDriver", true, cl);
Method m[] = c.getMethods();
for (Method mm : m) {
System.out.println(mm.getName());
}
} catch (Exception e) {
e.printStackTrace();
}
While running on Linux, jar is placed at root and for Windows its at c:\ (source and binaries are in some folder on C:\ so "/sqljdbc4.jar" resolves to c:\sqljdbc4.jar on windows, I have made sure that correct jar location in passed to classloader for both the platforms.
Following is the stack trace i get on windows
java.lang.ClassNotFoundException: com.microsoft.sqlserver.jdbc.SQLServerDriver
at java.net.URLClassLoader$1.run(URLClassLoader.java:200)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:188)
at java.lang.ClassLoader.loadClass(ClassLoader.java:306)
at java.net.FactoryURLClassLoader.loadClass(URLClassLoader.java:594)
at java.lang.ClassLoader.loadClass(ClassLoader.java:251)
at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:319)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Class.java:247)
at DemoClass.loadAClass(DemoClass.java:31)
at DemoClass.main(DemoClass.java:14)
NOTE : You can use any jar that u have to try this out. I was playing with MS SQL Server JDBC Driver jar.
Thanks !
-Abhijeet.
Try using this to create the URL rather than manually building the string:
URL[] jarUrlArray = { newf.toURI().toURL() };