Call Scala code from Java? - java

I'm using Netbeans to write Scala and Java.
Netbeans generated a .jar file for the Scala project. I tried importing that file in the Java project. But I couldn't import any class from that .jar file into my Java project.
I also tried importing scala-library.jar to the java project, and could import classes from that jar.
I want to write my library in Scala, then expose a small interface that only involves Java stuff, then write a Java wrapper so that people can use it as a Java package. Is that possible? How do I do it?
Thank you very much.

There should be no problem in doing this.
Have you verified (e.g. using WinZip or the jar utility) that your .jar file actually contains the relevant .class files? (use jar tvf mylib.jar to check)
If you have verified that the correct .class files exist in your jar file, what is the runtime error you are seeing? Is it a NoClassDefFoundError? If so, which class cannot be found? How are you referring to the (Scala) class in your (Java) code?

Related

How to change compile only one class of a java source code

I am using Apache POI library on Java. I want to add a method to XWPFSDTContent class. I have the source code but when I want to compiling after adding method I have to use "ant jar" command but it takes so much time and makes it harder to debug because it compiles the whole library. Is there a way to compile only one source file? I can't use javac command because of its dependency on the library.
Assuming you have the source code of Apache POI imported into a java project in some kind of IDE. Then you basically only need to edit your one class, afterwards the IDE should have generated the new .class file.
Take this class file (and any possibly existing anonymous inner class files) and copy it into your jar (replacing the old .class files). That should work assuming that you compiled the class file with the same java version as the rest of the classes inside the jar and your jar is not signed.

How to Import Classes from Reference Library in Eclipse?

I am new to both Java and to the Eclipse IDE, and I am having trouble with adding a Referenced Library and actually using it in my code.
I am in a software testing class, and my assignment is to use the JWebUnit library to run some basic tests on a website of my choice. I have downloaded a zip of the library and added it to the build path, so that it shows up in my Referenced Libraries list.
When I try to actually use that library, though, the import statement gives an error.
I just cannot figure out what I am doing wrong that the base class cannot see this library.
The screenshot show that you have added the folder net as class folder to the Java Build Path. Maybe you have added the source code (.java files) or Javadoc (.html files) instead of the executable bytecode (.class files). Usually .jar files are provided containing .class files which can directly be added to the Java Build Path.
Probably you want to download e.g. the file jwebunit-3.3-release.zip from here and add all JARs contained in that ZIP (in the root folder and in lib) to your Java Build Path instead.

RobotFramework cannot import Java keyword library

I did some robot framework python examples with pybot, and referenced .py files as my library files. The folder structure I used was test/lib/myLib.py and test/test/myTest.robot, where /test was at the same level as the /src folder in my project in eclipse.
I then tried to do similar things with a java project using jython. I have /test/test/myTest.robot which imports the library ../lib/myLib.java. I even tried importing this file using RIDE, but it always shows up as red text, meaning the import failed. The specific message I get from using jybot on the command line is:
"Importing test library 'C:\Users\cody\git\myProject\test\lib\myLib.java' failed: ImportError: No module named myLib"
I read that I might need to add it to classpath, and I think in order to do so, I need to make it a .jar file. I'd rather not do all that if possible to just leave it as a .java file. I attempted to add the lib folder to the build path... By that I mean I added the /test/lib folder to the "Source folders on build path". I also exported the darn thing as a jar and added that as a library. All this was done from the project properties in Eclipse.
I'm pretty stuck... any ideas how to get this working?
Java being a compiled language, you need to compile your java Class before importing it with RobotFramework.
Normally, Eclipse would do that for you, and put the resulting .class files inside a bin repository. If not, you can issue the javac path/to/file.java command, and move the resulting .class file where you want it (somewhere referenced by the classpath.
From within the .robot file, you should have the line Library test/lib/myLib, without neither .java nor .class at the end.

Extracting java class files from jar

I had running executable jar of a Java project. Unfortunately I have lost all the source code but I still got the executable jar of it. Is there any possibility to extract my classes from the jar?
I have tried extracting but class files are in damaged shape.
To get source from .class files , just download a decompiler from here and get them.
Are you talking about decompiling or just receiving the class files? The class files can be extracted using any software capable of reading ZIP. If you talk about decompiling that usually will work purly. But you could try Jad which I had the best experience with.

how to use a .class file with .java files

I'm quite green to java (I program in C/C++) and I still haven't figured out how this all works together.
I'm not trying to ask for anyone to do my homework, I'm just confused about some basic java stuff. I have an assignment which wants me to create a maze. The assignment comes with some files which have been pre-implemented and theoretically works. The folder gives me some .java files and some .class files. I opened the .java files and most of them are interfaces with no real code, so I'm assuming the meat of it are in these .class files. My question is, how do I interface with these .class files? do I need to do something like import myClass; if I have a file called myClass.class?
I've tried importing the file directory which these files are contained in (through Eclipse), but it seems the .class files don't show up. I'm guessing it's being processed in the background?
Here's a link to the assignment
http://www.ics.uci.edu/~goodrich/teach/ics23/LabManual/Dark/
Here's the zip file with all the .java and .class files
http://www.ics.uci.edu/~goodrich/teach/ics23/LabManual/Dark/Dark.zip
Thanks in advance!
To add .class files to your project in Eclipse: right-click your project, select properties --> Java Build Path --> Libraries and click on Add Class Folder button. Select the folder with the classes.
No you won't need to import. Place the class files in the same folder so their path is included and simply call on them like normal datatypes.
For example:
myClass mc = new myClass();
or
(new myClass()).myRoutine();
If some method is static
myClass.staticalicious();
importing is used generally when path to classfiles needs to be specified.
Also, .class files are simply compiled .java files.
Oh and lastly, follow Java nomenclature :)
It would be MyClass, not myClass.
.class files are compiled versions of .java files. Assuming that the .class files are named and packaged appropriately you should be able to reference them like this:
Filename: some.package.class
Code: import some.package;
Once you've imported the package you can call methods on the classes contained therein.
As others mentioned, the class files are the compiled Java files, so no there is not "more meat" in them than you can see in the source files. There are class files for interfaces and class files for implementations.
I assume you just use the default package.
So just set the classpath to the directory holding these pre-compiled classes:
e.g. to compile your classes on a Linux/ Unix system:
javac -classpath <dirWithClassFiles>:. .....
and to run the application:
java -classpath <dirWithClassFiles>:. .....
You then can directly use those pre-compiled classes in your own classes.
class files are the binary output of the java compiler. they are the java equivalent of .o files in C or C++ the "meat" in this instance is probably for you to implement.
The java compiler handles finding and including the necessary files, you may need to import some.package.name.*; to avoid having to use full class names (e.g. SomeClass instead of some.package.name.SomeClass)
The class files are compiled classes and you cannot view them in your IDE. Your IDE will allow you to import them but just drop off the extension...
import example.some.Thing;
The .java files are the source files that were most likely used to compile the .class files. I'm sure they are that just for your reference.
Eclipse IDE does not show the .class files. you can add the jars or class files to your project build path. And then you can import the perticular class to your own class to use it.

Categories

Resources