import java class in JRuby - java

I am trying to import a Java Class in Jruby
$ ls
bin src
$ ls bin/com/practice
Test.class
$ ls src/com/practice
Test.java
$ cat src/com/practice/Test.java
package com.practice;
public class Test {
public static String foo(){
return "Java!!";
}
public static void main(String args[]){
System.out.println(Test.foo());
}
}
$ jirb -Ibin
jruby-1.7.10 :001 > java_import 'com.pratice.Test'
NameError: cannot load Java class com.pratice.Test
from org/jruby/javasupport/JavaClass.java:1250:in `for_name'
from org/jruby/javasupport/JavaUtilities.java:34:in `get_proxy_class'
from file:/Users/gaurish/.rvm/rubies/jruby-1.7.10/lib/jruby.jar!/jruby/java/core_ext/object.rb:26:in `java_import'
from org/jruby/RubyArray.java:2409:in `map'
from file:/Users/gaurish/.rvm/rubies/jruby-1.7.10/lib/jruby.jar!/jruby/java/core_ext/object.rb:22:in `java_import'
from (irb):1:in `evaluate'
from org/jruby/RubyKernel.java:1119:in `eval'
from org/jruby/RubyKernel.java:1519:in `loop'
from org/jruby/RubyKernel.java:1282:in `catch'
from org/jruby/RubyKernel.java:1282:in `catch'
from /Users/gaurish/.rvm/rubies/jruby-1.7.10/bin/jirb:13:in `(root)'
What am I doing wrong here?

You have a number of problems:
You've misspelled "practice" in irb: java_import 'com.pratice.Test'
Your class isn't actually in the correct package. You need to add package com.practice; to the Java code.
You need to add "bin" to your classpath so that the JVM can find the classes: $CLASSPATH << 'bin'
Altogether, I was able to run this in IRB:
$CLASSPATH << 'bin'
com.practice.Test.foo
# => "Java!!"
EDIT: I failed to copy your package line, that's my fault, not yours. :-)

Related

Compile source files to a different directory and then accessing them in another directory, how? JAVA

My directory structure:
C:\jaBHa
here I want to create two packages - sources and classes.
so it looks like
C:\jaBHa\classes
C:\jaBHa\sources
the sources package must contain all the source files i.e., the .java files.
And then I want them to compile to the classes package which would contain all the .class files.
here are the codes for the three classes...
// MyDate.java
package classes;
public interface MyDate {
void showDate();
}
// DateImpl.java
package classes;
import java.util.*;
import classes.*;
public class DateImpl implements MyDate {
public void showDate() {
Date d = new Date();
System.out.println(d);
}
}
// Test.java
package classes;
import classes.*;
public class Test {
public static void main(String[] args) {
DateImpl d = new DateImpl();
d.showDate();
}
}
Now, I know that there's some issue with my class DateImpl java file, but my objective is...
that I want to compile these java files using the commands
C:\jaBHa> javac -d C:\jaBHa\sources MyDate.java
C:\jaBHa> javac -d C:\jaBHa\sources DateImpl.java
C:\jaBHa> javac -d C:\jaBHa\sources Test.java
with this the MyDate.java, DateImpl.java and Test.java files will compile to classes package...
and then i run it by typing
C:\jaBHa> java classes.Test
as the class file of Test.java is in C:\jaBHa\sources\classes package...
but here's the problem, it compiles to C:\jaBHa\sources\ here it will create a package classes but I want it to compile to C:\jaBHa\classes not C:\jaBHa\sources\classes...
so I have to keep my all java files in C:\jaBHa\ so that it can compile to classes package.
The -d option specifies the output directory for class files.
Try this.
C:\jaBHa>javac -d . sources\*.java
C:\jaBHa>java classes.Test
Mon Oct 24 19:19:05 JST 2022
C:\jaBHa>

How to access other files in any editor?

For example I have Main.java and test.java. test has public static int bro = 5; so i try to print test.bro from Main but the class test is not found. An IDE like Eclipse takes care of this for me but how do I do this with an editor? Sorry noob question. I'm in cmd in the directory of deez files and i type javac Main.java, den java Main. Thanks.
file Main.java:
public class Main {
public static void main(String[] args) {
System.out.println(test.bro);
}
}
file test.java:
public class test {
public static int bro = 5;
}
So suppose you have two source files: Main.java and test.java then you need to compile them first.
You can do it via command javac Main.java test.java. That command will produce 2 files in your current directory: Main.class and test.class. Which contain compiled java code.
Now you need to run your main class with classpath which contains both of your classes. So you need to run command java -cp . Main. Where . represents directory with your compiled classes.

javac adding classpath breaks my local class compilation

When I import a package to my MyLib class (which requires -cp to javac) I can no longer compile my MyMain class.
MyMain.java:
class MyMain
{
public static void main (String [] args)
{
MyLib.do_stuff ();
}
}
MyLib.java:
import com.google.gson.JsonElement;
class MyLib
{
public static void do_stuff ()
{
System.out.println ("Hello.");
}
}
When I javac MyLib.java I have do do it like this
javac -cp GSON_JAR_PATH MyLib.java
That works but if I
javac MyMain.java
I get
./MyLib.java:1: error: package com.google.gson does not exist
import com.google.gson.JsonElement;
but if I add -cp to the compilation command
javac -cp GSON_JAR_PATH MyMain.java
I get
MyMain.java:5: error: cannot find symbol
MyLib.do_stuff ();
^
symbol: variable MyLib
location: class MyMain
Use "-cp path1:path2" - colon separated. (semicolon works on windows) (the parameter to cp is quoted....
javac -cp path1:path2 //or ; for windows.
Note 1 - setting -cp overrides any existing CLASSPATH environment or default
path setting.
Note 2 - if no CLASSPATH setting then default is '.' - until the -cp overrides that.
So in the posted case - the "." was set for path (either CLASSPATH or default) up until the -cp was used which overrode that default - so it needs to be added back in.

java relative path vs absolute path on command line

Running into an interesting problem
When I run:
$cd /projects/MyProject/
$java -cp . S3Sample
This works fine. However if I run:
$java -cp /projects/MyProject /projects/MyProject/S3Sample
Error: Could not find or load main class .projects.MyProject.S3Sample
Why is that. Did a quick look and can't find the answer. Thanks!
I have this folder structure:
- home
- org
- test
+ Foo.java
+ Foo.class
And the code in Foo.java is a simple hello world application:
//Note the usage of the package statement here.
package org.test;
public class Foo {
public static void main(String[] args) {
System.out.println("Hello world");
}
}
Then, in command line, in order to execute Foo.class, I should provide the complete name of the class (I'm in "/home" folder in cmd):
$ java -cp "org/test;." Foo
Exception in thread "main" java.lang.NoClassDefFoundError: Foo (wrong name: org/test/Foo)
$ java -cp "org/test;." org.test.Foo
Hello world
Now, I edit the class above and remove the package sentence:
//no package specified
public class Foo {
public static void main(String[] args) {
System.out.println("Hello world");
}
}
After recompiling the class and executing the same command lines:
$ java -cp "org/test;." Foo
Hello world
$ java -cp "org/test;." org.test.Foo
Exception in thread "main" java.lang.NoClassDefFoundError: org/test/Foo (wrong name: Foo)
TL;DR
Make sure to always specify the full name of the class. Check if your class belongs to a package. Specifying the path of the class to execute is the same as writing the full name of the class, java program will replace / by ..
You should run
$ java -cp /projects/MyProject S3Sample
The path for class is already CLASSPATH-relative
With java, you specify the fully qualified name of a class containing the main method you want executed. (The launcher will replace / with .). This class needs to be in the classpath. The argument is not a path to a file.

Setup simple java package

On linux RedHat machine in /home/user/Downloads/ I do mkdir proj, cd proj, mkdir lib.
Now I want to have my java program residing in the proj package, so I have:
package proj;
import static proj.lib.Mathem.*;
public class MyJavaApp {
public static void main(String[] args) {
System.out.println("abc");
time = 2013;
}
}
and my library residing in a proj.lib subpackage, so I have:
package proj.lib;
public final class Mathem {
public static long time;
private Mathem() {}
}
if I cd lib/ and do javac *.java then Mathem.java compiles fine into Mathem.class but when I go back cd .. and do javac *.java I get an error saying that package proj.lib does not exist.
Could you please help to understand why java compiler does not see such simple package structure ?
It sounds like you are in the proj directory when it fails. You need to be in the directory above that, i.e., in /home/user/Downloads in your example. Then enter "javac proj/MyJavaApp.java"
There is something called Classpath... I suggest you read up on it
Try adding a public static long getTime() method in Mathem.

Categories

Resources