I want to know if there is any other way to output the same results without using Apache common Library. I did this way but i need to know how to do it without any external imports.
Thanks a lot ahead.
import org.apache.commons.lang.StringEscapeUtils;
public class Solution
{
public static void main(String[] args)
{
String path="\"C:\\Program Files\\Java\\jdk1.7.0\\bin\"";
String pathJava = StringEscapeUtils.escapeJava(path);
System.out.println("It's Window path: "+ path);
System.out.println("It's java path: "+pathJava);
}
}
Why not checkout the original source here to understand how apache's StringEscapeUtils does it?
Related
How I can generate .wasm file or .wast file for the following code:
import java.util.ArrayList;
public class NewClass2
{
public static void main(String[] args)
{
ArrayList<String> lis = new ArrayList<>();
lis.add("My");
lis.add("Name");
System.out.println(lis.get(2));
}
}
Can anyone help me in this regard? I have check TeaVM, CheerpJ, etc, but unable to find to get generate web assembly code.
While one could, in theory, write a compiler from JavaScript to WebAssembly, converting JavaScript to WebAssembly is not currently possible or something that WebAssembly was explicitly designed for. If you want to write code that looks somewhat like JavaScript, but targets WebAssembly, the closest thing today is probably AssemblyScript: https://www.assemblyscript.org/.
in my program I dynamically get the name of the .java file. In this file I need to find all methods, foreach method also all parameters (also with their annotations).
I read through the discussions here and found this https://code.google.com/p/javaparser/ javaparser, that seems pretty easy to use, but the problem is, that it is just for 1.5.
Than you mentioned, that Java 1.6 has already got built-in parser (javax.lang.model). But I can not figure out, how it works. Do you know any good tutorial/example of it?
Do you know any other way to parse java source file?
How about using Doclet API?
Normally, This API is used from bat file, but you can invoke programmatically like the following.
IMPORTANT: This API exists in not rt.jar(JRE) but tools.jar (JDK). So you need to add tool.jar into classpath.
import com.sun.javadoc.ClassDoc;
import com.sun.javadoc.Doclet;
import com.sun.javadoc.MethodDoc;
import com.sun.javadoc.RootDoc;
import com.sun.tools.javadoc.Main;
public class DocletTest {
public static void main(String[] args) {
Main.execute("", Analyzer.class.getName(), new String[] {"path/to/your/file.java"});
}
public static class Analyzer extends Doclet {
public static boolean start(RootDoc root) {
for (ClassDoc classDoc : root.classes()) {
System.out.println("Class: " + classDoc.qualifiedName());
for (MethodDoc methodDoc : classDoc.methods()) {
System.out.println(" " + methodDoc.returnType() + " " + methodDoc.name() + methodDoc.signature());
}
}
return false;
}
}
}
Take another look at the javaparser project. It has been updated to support all modern Java versions.
The Doclet API is really hard to use and is badly documented. It will be either going away or be replaced with something better, hopefully even in Java 1.9.
I when i use reader.readLine(), the string length is always 80 chars and after the main string unicode spaces are padded up.
Is there a way to remove those unwanted characters.
(java.io.RandomAccessFile reader)
String.trim is not working on this
You can use StringUtils.strip from Commons Lang. It is Unicode-aware.
You can write a custom method in Java to remove the Unicode space characters , using Character.isWhitespace(char) and Character.isSpaceChar(char) methods, for your specific purpose.
The Spring framework has a StringUtils class with a trimWhitespace(String) method which appears to be based on Character.isWhitespace(char) from the source code here.
use Google Guava
CharMatcher.WHITESPACE.trimFrom(source);
or try this https://gist.github.com/michalbcz/4861a2b8ed73bb73764e909b87664cb2
If you do not want a big libs. Just use:
str.replaceAll("^[\\s\\p{Z}]+|[\\s\\p{Z}]+$", "");
Testing
public static String trim(String str) {
return str.replaceAll("^[\\s\\p{Z}]+|[\\s\\p{Z}]+$", "");
}
public static void main(String[] args) {
System.out.println(trim("\t tes ting \u00a0").length());
System.out.println(trim("\t testing \u00a0").length());
System.out.println(trim("tes ting \u00a0").length());
System.out.println(trim("\t tes ting").length());
}
would have been faster to just search stackoverflow for this question becoz there are multiple questions on that topic there. well, try this:
st.replaceAll("\\s","")
check this one here: link
I want to find out how to open any exe in Windows using Java code. I have searched Google before and they only show me part of the code that they use, I think, because it doesn't seem to compile.
I have downloaded JDK 7 to compile. I don't use Eclipse at the moment and also explaining what I had to do to get it to work in detail would help a lot.
to what Sri Harsha Chilakapati said: would i need to create a class for the code?
Thanks to those who answered but i didn't quite get what you meant but i did however manage to find a website which had what i was after:
http://www.rgagnon.com/javadetails/java-0014.html
public class Test {
public static void main(String[] args) throws Exception {
Process p = Runtime.getRuntime().exec(
"\"c:/program files/windows/notepad.exe\"");
p.waitFor();
}
}
the above was what i was after but thanks again anyway to the people who answered.
Try this.
String myExe = "C:\\MyExe.exe";
String args = "";
Runtime.getRuntime().exec(myExe + " " + args);
Hope this helps.
I would recommend the ProcessBuilder, especially for additional arguments.
My cell phone provider offers a limited number of free text messages on their website. I frequently use the service although I hate constantly having a tab open in my browser.
Does anyone know/point me in the right direction of how I could create a jar file/command line utility so I can fill out the appropriate forms on the site. I've always wanted to code up a project like this in Java, just in case anyone asks why I'm not using something else.
Kind Regards,
Lar
Try with Webdriver from Google or Selenium.
Sounds like you need a framework designed for doing functional testing. These act as browsers and can navigate web sites for testing and automation. You don't need the testing functionality, but it would still serve your needs.
Try HtmlUnit, or LiFT, which is a higher-level abstraction built on HtmlUnit.
Use Watij with the Eclipse IDE. When your done, compile as an .exe or run with a batch file.
Here is some sample code I wrote for filling in fields for a Google search, which can be adjusted for the web form you want to control :
package goog;
import junit.framework.TestCase;
import watij.runtime.ie.IE;
import static watij.finders.SymbolFactory.*;
public class GTestCases extends TestCase {
private static watij.runtime.ie.IE activeIE_m;
public static IE attachToIE(String url) throws Exception {
if (activeIE_m==null)
{
activeIE_m = new IE();
activeIE_m.start(url);
} else {
activeIE_m.goTo(url);
}
activeIE_m.bringToFront();
return (activeIE_m);
}
public static String getActiveUrl () throws Exception {
String currUrl = activeIE_m.url().toString();
return currUrl;
}
public void testGoogleLogin() throws Exception {
IE ie = attachToIE("http://google.com");
if ( ie.containsText("/Sign in/") ) {
ie.div(id,"guser").link(0).click();
if ( ie.containsText("Sign in with your") ||
ie.containsText("Sign in to iGoogle with your")) {
ie.textField(name,"Email").set("test#gmail.com");
ie.textField(name,"Passwd").set("test");
if ( ie.checkbox(name,"PersistentCookie").checked() ){
ie.checkbox(name,"PersistentCookie").click();
}
ie.button(name,"signIn").click();
}
}
System.out.println("Login finished.");
}
public void testGoogleSearch() throws Exception {
//IE ie = attachToIE( getActiveUrl() );
IE ie = attachToIE( "http://www.google.com/advanced_search?hl=en" );
ie.div(id,"opt-handle").click();
ie.textField(name,"as_q").set("Watij");
ie.selectList(name,"lr").select("English");
ie.button(value,"Advanced Search").click();
System.out.println("Search finished.");
}
public void testGoogleResult() throws Exception {
IE ie = attachToIE( getActiveUrl() );
ie.link(href,"http://groups.google.com/group/watij").click();
System.out.println("Followed link.");
}
}
It depends on how they are sending the form information.
If they are using a simple GET request, all you need to do is fill in the appropriate url parameters.
Otherwise you will need to post the form information to the target page.
You could use Watij, which provides a Java/COM interface onto Internet Explorer. Then write a small amount of Java code to navigate the form, insert values and submit.
Alternatively, if it's simple, then check out HttpClient, which is a simple Java HTTP client API.
Whatever you do, watch out that you don't contravene your terms of service (easy during testing - perhaps you should work against a mock interface initially?)
WebTest is yet another webapp testing framework that may be easier to use than the alternatives cited by others.
Check out the Apache Commons Net Package. There you can send a POSt request to a page. This is quite low level but may do what you want (if not you might check out the functional testing suites but it is probably not as easy to dig into).
As jjnguy says, you'll need to dissect the form to find out all the parameters.
With them you can form your own request using Apache's HTTP Client and fire it off.