I'm completely new to Java and clojure. But with previous experience in common lisp, I thought I would give clojure a try. I'm unable to figure out few very basic things.
This is the actual Java code.
import syntaxtree.*;
import visitor.*;
public class Main {
public static void main(String [] args) {
try {
Node root = new MicroJavaParser(System.in).Goal();
System.out.println("Program parsed successfully");
}
catch (ParseException e) {
System.out.println(e.toString());
}
}
}
When I run this code, the outcome is as expected.
└──╼ java Main < ../input/Factorial.java
Program parsed successfully
In Clojure I tried this :
(ns clj-assign2.core)
(defn -main
[]
(def root
(.Goal
(MicroJavaParser. (. System in))))
(println "Successfully parsed"))
But when this code is run, the following exception is raised :
└──╼ lein run < ../assign2/input/Factorial.java
Exception in thread "main" java.lang.IllegalArgumentException: No matching field found: Goal for class MicroJavaParser
at clojure.lang.Reflector.getInstanceField(Reflector.java:271)
at clojure.lang.Reflector.invokeNoArgInstanceMember(Reflector.java:300)
at clj_assign2.core$_main.invoke(core.clj:7)
< --- snipped --- >
What am I doing wrong here?
Maybe you are missing an import statement in your clojure program?
Related
users!
I have a problem with my Microsoft VS Code.
When i run my code with method
System.in.read()
i have a problem "Evaluation failed because the thread is not suspended."
P.S. When i run file using javac and java this code is work.
i also have problem with VS Code another problem
My code
import java.io.IOException;
public class example {
public static void main(String args[])
throws IOException {
char symbol;
int count = 0, count_space = 0;
do {
symbol = (char) System.in.read();
if (symbol == ' ') {
count_space += 1;
}
count += 1;
} while(symbol != '.');
System.out.println("Всего символов: " + count);
System.out.println("Из них пробелов: " + count_space);
}
}
Sorry for bad grammar.
Can you help me?
I'm a chinese student and i had the same problem. I found a solution in baidu.
The built-in debugging console of vscode does not support java input. So you need to modify the debug console in the debug configuration file (launch.json), just modify the console property from "internalConsole" to "integratedTerminal" or "externalTerminal".
I have a java program which throws some exception,I tried executing it from shell script and printing 0 on failure and 1 on successful execution of java program.But It also printing the Exception onto console I just want to print exit code only.How to do this ?.Any suggestion are appreciated .
following are my Java program and script files
Test.Java
public class EchoTest {
public static void main (String args[]) {
System.out.println ("scuccess Prasad Bezavada "+(2/0));
}
}
Test.sh(script file)
java Test
if [ $? -eq 0 ]
then echo "1"
else echo "0"
fi
getting the following out put
$sh Test.sh
Exception in thread "main" java.lang.ArithmeticException: / by zero
at EchoTest.main(EchoTest.java:3)
0
$
Expecting output is like below(i.e just want to skip the exception message)
$sh Test.sh
0
$
Try this.
java Test 2> /dev/null
if [ $? -eq 0 ]
then echo "1"
else echo "0"
fi
you have to catch the exceptions. After that, you would be able to output exactly what you want. on your example:
public class EchoTest {
public static void main (String args[]) {
try{
System.out.println ("scuccess Prasad Bezavada "+(2/0));
} catch (Exception e){
// doing nothing is ok for your intended behaviour
}
}
}
First of all, you would like your Java program to return a value (either 1 or 0).
In our case we will consider that if an exception is thrown, 1 will be returned and 0 otherwise. Also, exception will be hid (which is a bad practice. You should always log exceptions at least if you are not willing to show it on screen)
public class EchoTest {
public static void main (String args[]) {
try {
System.out.println ("scuccess Prasad Bezavada "+(2/0));
System.exit(0);
}
catch (Exception e) {
// log your exception here
System.exit(1);
}
}
}
Once this is done then what you will need to work on is on getting java's output code.
java Test
output = $?
# do some logic here..
if [[ $output -eq 0 ]]; then
echo "executed"
else
echo "exception thrown"
fi
Finally, this will indeed return either 1 or 0 depending on execution ignoring exception case, which is what you actually requested.
I've prepared a class with a static method in Java 6, which I've exported to a JAR file:
package pl.poznan.put.stemutil;
public class Stemmer {
public static String stemText(String text) {
Set<String> c = new HashSet<String>();
...
return StringUtils.join(c, " ");
}
}
I import it to R with following code:
require(rJava)
.jinit("java/stem-util.jar")
stem = J("pl.poznan.put.stemutil.Stemmer")$stemText
Then, when I call it directly it works, e.g:
> stem("płotkami")
[1] "płotek płotka"
But when I'll try to use it with tm_map() function, something goes wrong:
> vc = VCorpus(vs, readerControl = list(language = "pl"))
> vc[[1]]
<<PlainTextDocument (metadata: 7)>>
mirki mirkówny zaczynam wolne jutra ( ͡° ͜ʖ ͡°) #pijzwykopem #piwozlidla
> vc = tm_map(vc, stem)
Komunikat ostrzegawczy:
In mclapply(content(x), FUN, ...) :
all scheduled cores encountered errors in user code
> vc[[1]]
[1] "Error in FUN(X[[1L]], ...) : \n Sorry, parameter type `NA' is ambiguous or not supported.\n"
attr(,"class")
[1] "try-error"
attr(,"condition")
<simpleError in FUN(X[[1L]], ...): Sorry, parameter type `NA' is ambiguous or not supported.>
What am I doing incorrectly?
Finally adding mc.cores parameter has worked for me. However, It's more a workaround, than a proper solution.
vc = tm_map(vc, content_transformer(stem), mc.cores=1)
i em trying to fetch some query from an url and then pass them to a java program for further execution. The problem i am facing is that my php code is calling my java program but is not passing the values.
till now i have worked on these codes,
PHP PROGRAM:
<?php
$phonecode= $_GET['phonecode'];
$keyword= $_GET['keyword'];
$location= $_GET['location'];
exec("C:\Users\Abc\Documents\NetBeansProjects\JavaApplication11\src\javaapplication11\main.java -jar jar/name.jar hello" . $phonecode . ' ' . $keyword . ' ' . $location, $output);
print_r($output);
?>
JAVA PROGRAM:
public class Main
{
public static void main(String args[])
{
try
{
String phonecode = args[];
System.out.println(args[]);
System.out.println(phonecode);// i have only tried to print phonecode for now
}
catch(Exception e)
{
System.out.println(e);
}
}
}
Ok, a couple of issues with the Java code you've posted, here's a working version of what you posted:
class Main
{
public static void main(String[] args)//String[] args, not String args[]
{
if (args.length == 0)
{//check to see if we received arguments
System.out.println("No arguments");
return;
}
if ( args.length < 3)
{//and make sure that there are enough args to continue
System.out.println("To few arguments");
return;
}
try
{//this try-catch block can be left out
String phonecode = args[0];//first arg
String keyword = args[1];//second
String location = args[2];//third
//print out the values
System.out.print("Phonecode: ");
System.out.println(phonecode);
System.out.print("keyword: ");
System.out.println(keyword);
System.out.print("location: ");
System.out.println(location);
}
catch(Exception e)
{
System.out.println(e.getMessage());//get the exception MESSAGE
}
}
}
Now, save that as a .java file, and compile it, it should churn out a Main.class file. I compiled it from the command-line:
javac main.java
I don't have netbeans installed, but I suspect the .class file will be written to a different directory, something like:
C:\Users\Abc\Documents\NetBeansProjects\JavaApplication11\bin\javaapplication11\Main.class
// note the BIN
Then, to execute, you need to run the java command, and pass it the path to this Main.class file, leaving out the .class extension. Thus, we end up with:
java /path/to/Main 123 keywrd loc
Should result in the output:
Phonecode: 123
keyword: keywrd
location: loc
In your PHP code:
exec('java /path/to/Main '.escapeshellarg($phonecode). ' '.escapeshellarg($keyword).' '.escapeshellarg($location), $output, $status);
if ($status === 0)
{//always check exit code, 0 indicates success
var_dump($output);
}
else
exit('Error: java exec failed: '.$status);
There are a couple of other issues, too: like $phonecode = $_GET['phonecode']; doesn't check if that $_GET param exists. If it doesn't your code will emit notices. To fix:
$phonecode = isset($_GET['phonecode']) ? $_GET['phonecode'] : '';
Other niggles include: the backslash is a special char in strings, it is used in escape sequences: \n is a newline char. PHP can deal with the *NIX directory separator /, even on windows. Use that, or escape the backslashes (C:\\Users\\Abc\\ and so on).
A file that only contains PHP code doesn't require the closing ?> tag. In fact: it is recommended you leave it out.
your java code should look like
public static void main (String[] args) {
for (String s: args) {
System.out.println(s);
}
}
Note String[] args, not String args[]
Also on PHP side in exec you need space between string hello, and variable $phonecode if you want those to be looked as a 2 separate arguments.
I'm working on getting (JavaScript) scripting to work in Java.
I have a program in JavaScript, defined in my Java program (along with instances of all the necessary script engine related things) like so:
static ScriptEngineManager engineManager = new ScriptEngineManager();
static ScriptEngine jsengine = engineManager.getEngineByName("js");
static Invocable jsinvoke = (Invocable) jsengine;
static String program =
"//importPackage(javax.swing);" +
"function myMethod(x, y) {" +
"return x+y;" +
"}";
At the start of the program I do call this, which works without complaint:
try {
jsengine.eval(program);
} catch(ScriptException e) {e.printStackTrace();}
Then, I call myMethod with this:
try {
jsinvoke.invokeFunction("myMethod", x, y);
} catch(ScriptException se) {
se.printStackTrace();
}
catch(NoSuchMethodException nsme) {
nsme.printStackTrace();
}
It gives the error java.lang.NoSuchMethodException: no such method: myMethod. It clearly exists in the JavaScript, so what did I do wrong?
The commented code seems to be the source of problem, since it comments out even the method name myMethod
//importPackage(javax.swing);
remove this line and rerun your code
If you want to preserve your comment then instead of single line comment (//) use multi line comment (/**/)