Execute a Groovy class in a package from the command line - command-line

Is there a way to execute a Groovy class by specifying the package with dots, as with java?
Example: File ./my/package/MyClass.groovy:
package my.package
class MyClass {
static void main(String[] args) {
println "ok"
}
}
> cd my/package
my/package> groovy MyClass
ok
> cd ../..
> groovy my/package/MyClass.groovy
ok
> groovy my/package/MyClass
ok
> groovy my.package.MyClass
Caught: java.io.FileNotFoundException: my.package.MyClass
I was expecting the last command to work. I tried various ways of setting the classpath, to no avail.

First of all, package is a reserved keyword, so you can't use it as a a package name.
Second of all, you can't do that in Groovy, since the dot notation is used for classes, not for scripts, so you need a compiled class file to use it.
Still, you can replace the groovy command with java + classpath:
java -cp /usr/share/java/groovy/embeddable/groovy-all-1.6.3.jar:. my.some.MyClass
You can add an alias to it 'g_java' for instance to make it less verbose.

Related

Object pythagorean is not a member of package com - Scala programming error

I am working through Atomic Scala 2.0, learning Scala. I am trying to create a package myself and trying to use it in my programs. This is the code for creating my package:
package com.atomicscala.pythagorean
class RightTriangle {
def hypotenuse(a:Double, b:Double):Double={
math.sqrt(a*a + b*b)
}
def area(a:Double, b:Double):Double={
a*b/2
}
}
And this is my program:
import com.atomicscala.pythagorean._
object test{
def main(args:Array[String])={
val rt = new RightTriangle
println(rt.hypotenuse(3,4))
}
}
When I try to run my program after compiling the package, I get an error like this:
error: object pythagorean is not a member of package com.atomicscala
Instead, if I name the package as just pythogorean, the code works fine. what is causing this?
Try it with the package spelled correctly:
You have:
package com.atmoicscala.pythagorean
probably should be
package com.atomicscala.pythagorean
I was able to solve it by mentioning the Classpath in the Scala command.
scala filename -classpath . (If the package is in the current working
directory)
scala filename -classpath PackageLocation
The root cause is on the "fsc", fsc will reset when classpath change.
You may reset it explicitly by execute the following
fsc -reset
then you don't need to include -classpath in your scala command.
note: I am not sure whether this is a good practice.
Another way is to run the scala command without using the fsc offline compiler
scala -nc filename

Error when calling Native Code using JNI from a Jython module

I am calling a simple HelloWorld program written in C from a Jython module (inside a PyDev project). There is an intermediate Java Class (in a separate Java project) which calls the native code using JNI (Java Native Interface). The native call is successful if I run the Java class directly. But when I call the Java class from my Jython module, I get the following error:
java.lang.UnsatisfiedLinkError: no ctest in java.library.path
I have successfully run several C programs from my Jython module using JNA (Java Native Access). But JNA hits performance (speed) really hard and I want to revisit JNI and fix this problem (also I simply want to know what is going on). The IDE I am using is Eclipse.
Here is the code for the Java class:
package JNIPackage;
public class HelloWorld {
native void helloFromC(); /* (1) */
static {
// Added the line below but still no luck. Was sure this would fix it.
System.setProperty("java.library.path", "/Users/haiderriaz/Desktop/JNI-C");
System.loadLibrary("ctest"); /* (2) */
}
static public void main(String argv[]) {
HelloWorld helloWorld = new HelloWorld();
helloWorld.helloFromC(); /* (3) */
}
}
Running this Java class directly, there is no error and "Hello World" gets printed out. But when I import JNIPackage to my Jython module and then call JNIPackage.HelloWorld, then all of a sudden java can not find ctest. I think this is strange and the problem only exists when calling C code using JNI as opposed to JNA.
Try System.load([full path and filename of ctest]), which works independently from values of LD_LIBRARY_PATH or java.library.path.
To ease user configuration I usually implement my own library-search-mechanism, i.e. make it look for libraries in the working directory and on the classpath too. I know ideologically this is somewhat wrong, but works much smoother for your users. Use java.io.File.exists to confirm the actual location of ctest-file and then use java.io.File.getAbsolutePath() to get the appropriate input for System.load.
System.mapLibraryName(...) can also be useful for this.

Is it possible to run rascal programs from the command line?

I know how to run rascal code from within eclipse and how to use the REPL, but I don't know how I can run a rascal file (or group of rascal files) as a program from the command line.
When I try the following, I get a parse error:
$ java -Xmx1G -Xss32m -jar rascal-shell-stable.jar mymodule.rsc
Version: 0.7.2.201501130937
Parse error in cwd:///mymodule.rsc from <1,11> to <1,12>
The contents of mymodule.rsc:
module mymodule
println("hello world");
What am I doing wrong?
Well, your mymodule.rsc is actually syntactically incorrect and will also give parse errors in the Eclipse IDE. Here is an improved version:
module mymodule
import IO;
value main(list[value] args) {
println("hello world");
}
Bonus: you should also add import IO; to make the println function available.

Java - running cmd

I'm trying to run a simple java test code. I'm getting a "can not find or load main class file "(something like that)
The tutorial I'm following uses this command
-> javac name.java (javac doesn't work, using ->java ..)
-> dir (shows the classname as a file)
> java classname
> outputs "hello world"
I can't seem to get past the ->java running.java
class apples //everything begins with a class - need this to do anything
{
public static void main(String args[])//method
{
System.out.println("Hello World");
}
}
There could be a few problems here. Check the following:
Are you saving the file as the name of the class plus .java, e.g. apples.java
When you execute it, are you typing the name of the class or the name of the class file? you should be typing java apples, not java apples.class or java apples.java.
EDIT:
I Noticed you haven't compiled the program using javac, which makes the progrm unrunnable by java [program_name]. You need to run javac [java_sourcde_file_name] to generate a .class file. If javac doesn't work, maybe:
You don't have the JDK (Java Development Kit) installed and should download it from Oracle
javac is not in your PATH - unlikely but possible - see http://www.apl.jhu.edu/~hall/java/beginner/settingup.html.
javac runs properly but your program doesn't compile properly. this seems unlikely given the program you posted looks fine.
class Apples //- need this to do anything
{
public static void main(String args[])//everything begins with a method
{
System.out.println("Hello World");
}
}
Make sure you are in current directory of java file
compile as
javac Apples.java
Run as
java Apples
Before working in it , should need to know the coding convention it would be better to work java
Well. All you have to do in Jaca is navigate you where your class files are stored and then use java "class name". You DO NOT need to put .java or .class. Just the name.
change your class name from apples to Apples (naming convention for java class names):
http://en.wikipedia.org/wiki/Naming_convention_%28programming%29#Java
also recommend you to change the file name accordingly...
then recompile and run it
javac Apples.java
java Apples
cheers

Findbugs using jsr305 annotations in eclipse is not finding bugs

I've been experimenting with the jsr 305 annotations for use with Findbugs, specifically the #CheckForNull annotation which would have avoided a bug I just found making it out to customers. I've added jsr305.jar and annotations.jar to my build path but the bugs aren't found by findbugs. I'm using Eclipse with the Eclipse Findbugs plugin. Below is some sample code which shows the same bug but doesn't when I run findbugs over it find the bug. I have tried this in Eclipse Galileo and Ganymede.
public class FindBugsAnnotationsTest {
ArrayList<String> canBeNull;
#CheckForNull
public List<String> getCanBeNull() {
return canBeNull;
}
public void shouldGetFindbugsWarning() {
canBeNull.add("a string");
getCanBeNull().add("a string");
}
}
This may be obvious, but I think your issues are with Eclipse (perhaps the FindBugs plugin in particular), not FindBugs itself.
You might consider running FindBugs from the command line to eliminate any Eclipse issues and ensure that you have FindBugs running correctly in its own. Knowing how to run FindBugs in a standalone mode will give you a fallback when your IDE is not configured properly.
I saved your source code in a file named FindBugsAnnotationsTest.java, added imports for List, ArrayList, and CheckForNull, compiled, and ran FindBugs 1.3.9. FindBugs generates several warnings about null values:
M D NP: Possible null pointer dereference in FindBugsAnnotationsTest.shouldGetFindbugsWarning() due to return value of called method Dereferenced at FindBugsAnnotationsTest.java:[line 18]
M C UwF: Unwritten field: FindBugsAnnotationsTest.canBeNull At FindBugsAnnotationsTest.java:[line 12]
M C NP: Read of unwritten field canBeNull in FindBugsAnnotationsTest.shouldGetFindbugsWarning() At FindBugsAnnotationsTest.java:[line 16]
Warnings generated: 3
These are the imports I added to the top of FindBugsAnnotationsTest.java:
import java.util.ArrayList;
import java.util.List;
import edu.umd.cs.findbugs.annotations.CheckForNull;
Commands:
javac -d . -classpath ${FINDBUGS_HOME}/lib/findbugs.jar FindBugsAnnotationsTest.java
${FINDBUGS_HOME}/bin/findbugs FindBugsAnnotationsTest.class
Where ${FINDBUGS_HOME} is the directory in which Findbugs 1.3.9 is installed. javac is assumed to be on the path.
Note: I used the findbugs.jar instead of annotations.jar and jsr305.jar but I get the same results with this command:
javac -d . -classpath ${FINDBUGS_HOME}/lib/annotations.jar:${FINDBUGS_HOME}/lib/jsr305.jar FindBugsAnnotationsTest.java