Newbie questions concerning CS106A, Eclipse, & ACM Libraries - java

After a while (5ish years) away from programming, I'm trying to follow along with Stanford's CS106A course but my inability to understand how Eclipse is halting my progress. I have looked on this site and elsewhere, but have only been partially successful. Basically, I have embarrassingly basic tech support questions.
First question: the book encourages you to play around with Java in Eclipse, but Stanford pre-loads all the projects. After the Karel portion of the class, I have no idea how create new projects/files/? to do this. How does one create a new project from which one can import the various portions of acm.jar? Whenever I try, either adding acm.jar through the New Java Project wizard or through tweaking the properties on an existing project, the acm.jar icon never appears in the project folder.
Second question: After Karel, I am also having trouble with Stanford's pre-loaded programs. To try and follow along without being able to create new projects (see above), I tried instead to modify MyProgram.java in the ACMStarterProject project. The code is here:
import acm.program.*;
public class MyProgram extends ConsoleProgram {
public void run() {
println("hello");
}
}
Here is the relevant portion of the Package Explorer.
Here is what I think is the relevant portion of the Console tab.
Many thanks!

Related

Integrating a Java Class into a PyDev Project (Jython) in Eclipse

I have a Java class that i would like to import into my Jython script. Unfortunately, eclipse won't let me create a Java class inside my Jython project.In the window, where you create and name your Java class, I get a message at the top (alongside a red cross) saying, "Source folder is not a Java project" when I type the name of the would be class. How do I rectify this? I need the Java Class to call C code using the JNI (declaring the native method,loading and then calling it). Thank You !!!!!!!
What you can do is to create second module which would be java project. Anyhow, logically it should be that way. Please check out other similar question - PyDev: Jython modules & Java classes in the same project.
Other links that might help - http://pydev.org/manual_101_project_conf2.html
So what nefo_x suggested is correct. You need to create a new Java project that will contain your Java class. Then import the Java package as you would a python module. But there are a few things to watch out for in eclipse to make it work. I list the whole process below:
Your Java class (or classes) should not be in the default package. You need to create a new package and make/put your java class files there.
Export the package as a jar file to some place on your computer.
Add the jar file (located at some place on your computer) to your python path.
Import the package by writing "import PackageName".
The problem for me was that I had my java class in the default package. That does not work due to some naming issues. Anyhow, hope that this helps.

NanoHTTPD: How do I add it to a current Java eclipse project and use it?

This is my first time with Java and Eclipse. I started a brand new Java project and I want to import/add NanoHTTPD into it. How do you this?
This is NanoHTTPD's site: http://nanohttpd.com
Thanks!
Edit
Lesson learned, be specific or you get backslashed for asking. I edited the question and here's some background and the problem I'm running into.
I'm developing a Nodejs backend that needs to query a JAVA project I was given. Pipes are a no go because the services will run on different machines. Tomcat seems like an overkill so I decided to use NanoHTTPD to develop the web service. I come from Ruby & Nodejs so compilation and Eclipse are very new to me. First off, I have no JAR file just TAR and ZIP and from what I read they are fundamentally different. However, I tried importing the TAR and ZIP files as recommended but the structure I get in Eclipse does not seem right compared to the JRE System Library or others I've seen. Notwithstanding, I went ahead and tried to import the package from my Main.java file
package fi.iki.elonen;
public class Main {
public static void main (String[] args)
{
System.out.println("Main");
}
}
When I try to run it I get the following error:
Exception in thread "main" java.lang.Error: Unresolved compilation problem:
at Main.main(Main.java:4)
I found a great article from IBM "Managing the Java classpath (UNIX and Mac OS X)" where it mentions that an IDE such as Eclipse can alleviate the pain of dealing with the source path, classpath and compilation. Unfortunately, I'm afraid this is where I might be getting stuck.
I tried uploading images of what I have but apparently I'm not popular enough yet to do it.
Could someone help me figuring out how to not only import libraries but using them on projects? Even just a URL to a clear Linux/Mac OS X post that explains import with multiple packages would be great.
NanoHTTPD is designed to be very lightweight.
I just cut and pasted the 'NanoHTTPD' class from the source on github, its all in there - and pasted it as a class into my own project.
Then I created a subclass of nanoHTTPD, overrode the 'serve' method to send my own stuff and it was job done.
Download the jar, drag it into the project, and right-click it to add it to the build path.

package javafx.media does not exist

I'm new to programming and don't know what to do... jGRASP gives this error (the title) when i try to run an mp3 file using java via this code :
import javafx.scene.media.MediaPlayer;
import javafx.scene.media.Media;
public class hehe{
public static void main(String[]args){
String krow="hoho.mp3";
Media trial = new Media(krow);
MediaPlayer Ply = new MediaPlayer(trial);
Ply.play();
}
}
I searched for a solution but couldn't find one.
You need to download e(fx)clipse plugin or use NetBeans or IntelliJ. If you are using 'regular' Eclipse, you need to add jfxrt.jar to your classpath.
What you are using is JavaFX. It comes along with your JDK. However, jfxrt.jar is not on standard classpath.
In spite of resolving that, your program won't run because running JavaFX program is different from running 'usual' Java programs. You need to extend the Application class and create a scene graph.
Have a look here on how to get started.

How to import a class from a package in a different folder?

I am new to java. This is a basic question about packages. I have a small java project named "stacklist.java" in Netbeans IDE. It's package name is stacklist. And it has 4 different classes. One of them is ListNode.
Now i need ListNode object in other project "queuelist.java".
directory structure is StackList->src->stacklist and QueueList->src->queuelist. Both StackList and QueueList are at the same level.
And added the folder(StackList\src) in Libraries of queuelist.java project. I did "import stacklist.*;"
When i run "clean and build project", i am getting this: "error: package stacklist does not exist
import stacklist.*;"
Please suggest me.
For
package a.b.c;
public class D;
package e;
import a.b.c.D;
public class E;
you need
src\a\b\c\D.java
src\e\E.java
You might go for Maven, a popular professional build infrastructure which helps with libraries from the internet and library versioning. And programming conventions.
For maven:
package a.b.c;
public class D;
package e;
import a.b.c.D;
public class E;
you need
src\main\java\a\b\c\D.java
src\main\java\e\E.java
Developing two projects needs care. If one project gives a library StackList.jar then you need to keep this library builded up to date. Often an IDE takes a shortcut, but the explicit use of a library may yield version errors.
Adding StackList.jar file and removing the folder(StackList\src) from the Libraries of current project made this to run without errors.

Using the Stanford NLP libraries from within R, using the rJava package

Does anybody have any experience with using StanfordCoreNLP ( http://nlp.stanford.edu/software/corenlp.shtml through rJava in R? I’ve been struggling to get it to work for two days now, and think I’ve exhausted Google and previous questions on StackOverflow.
Essentially I’m trying to use the StanfordNLP libraries from within R. I have zero Java experience, but experience with other languages, so understand the basics about classes and objects etc.
From what I can see, the demo .java file that comes with the libraries seems to show that to use the classes from within Java, you’d import the libraries and then create a new object, along the lines of:
import java.io.*;
import java.util.*;
import edu.stanford.nlp.io.*;
import edu.stanford.nlp.ling.*;
import edu.stanford.nlp.pipeline.*;
import edu.stanford.nlp.trees.*;
import edu.stanford.nlp.util.*;
public class demo {
etc.
etc.
StanfordCoreNLP pipeline = new StanfordCoreNLP();
etc.
From within R, I’ve tried calling some standard java functions; this works fine, which makes me think it’s the way I’m trying to access the Stanford libraries that’s causing the issue.
I extracted the Stanford ZIP to h:\stanfordcore, so the .jar files are all in the root of this directory. As well as the various other files contained in the zip, it contains the main .jar files:
joda-time.jar
stanford-corenlp-1.3.4.jar
stanford-corenlp-1.3.4-javadoc.jar
stanford-corenlp-1.3.4-models.jar
joda-time-2.1-sources.jar
jollyday-0.4.7-sources.jar
stanford-corenlp-1.3.4-sources.jar
xom.jar
jollyday.jar
If I try to access the NLP tools from the command line, it works fine.
From within R, I initalized the JVM and set the classpath variable:
.jinit(classpath = " h:/stanfordcore", parameters = getOption("java.parameters"),silent = FALSE, force.init = TRUE)
After this, if I use the command
.jclassPath()
This shows that the directory containing the required .jar files has been added and gives this output in R:
[1] "H:\RProject-2.15.1\library\rJava\java" "h:\ stanfordcore"
However, when I try create a new object (not sure if this is the right Java terminology) I get an error.
I’ve tried creating the object in dozens of different ways (basically shooting in the dark though), but the most promising (simply because it seems to actually find the class is):
pipeline <- .jnew(class="edu/stanford/nlp/pipeline/StanfordCoreNLP",check=TRUE,silent=FALSE)
I know this finds the class, because if I change the class parameter to something not listed in the API, I get a cannot find class error.
As it stands, however, I get the error:
Error in .jnew(class = "edu/stanford/nlp/pipeline/StanfordCoreNLP", check = TRUE, :
java.lang.NoClassDefFoundError: Could not initialize class edu.stanford.nlp.pipeline.StanfordCoreNLP
My Googling indicates that this might be something to do with not finding a required .jar file, but I’m completely stuck. Am I missing something obvious?
If anyone can point me even a little in the right direction, I’d be incredibly grateful.
Thanks in advance!
Peter
Your classpath is wrong - you are using a directory but you have JAR files. You have to either unpack all JAR files in the directory you specify (unusual) or you have to add all the JAR files to the class path (more common). [And you'll have to fix your typos, obviously, but I assume those come form the fact that you were not using copy/paste]
PS: please use stats-rosuda-devel mailing list if you want more timely answers.
Success!
After hours of tinkering, I managed to find a work-around. If anyone is interested, this is what I did:
Using Eclipse, I started a new project.
I then created a directory called ‘lib’ under the root of the project and copied all the Stanford .jar files into this directory.
After this, I edited the properties of the project in Eclipse, went to ‘Java Build Path’, clicked the libraries tab.
I then choose to import the Java system libraries.
I also clicked ‘Add External Jars’ and selected all the Stanford jars from the lib directory.
I then created intetermediary Java classes to call the Stanford classes (rather than trying to call them directly from R).
Example:
import java.lang.Object;
import java.util.Properties;
import java.io.*;
import java.util.*;
import edu.stanford.nlp.io.*;
import edu.stanford.nlp.ling.*;
import edu.stanford.nlp.pipeline.*;
import edu.stanford.nlp.trees.*;
import edu.stanford.nlp.util.*;
public class NLP {
public static void main(String[] args) {
Properties props = new Properties();
props.put("annotators", "tokenize");
StanfordCoreNLP coreNLP = new StanfordCoreNLP(props);
}
}
This doesn’t return anything, but shows how a Stanford object can be created.
Build the project using Eclipse.
From within R, then set the working directory to the Java project's /bin directory (this isn’t strictly necessary, as you can add the classpath directory instead, but it simplifies things).
Then the object can be created in R with:
.jinit(classpath = ".") // This initilizes the JVM
obj = .jnew("NLP")
After this any methods you’ve created within the intermediary java classes can be called with:
Name_of_var_to_store_return_value = . jcall(class name, signature type, method, paramters)
I still didn’t figure out why I can’t call the Stanford classes directly from R, but this method works. I suspect that #ChristopherManning is right and my problem is down to calling the external jar from R. By building it from scratch, the Stanford jars are linked during the build, so I guess that’s what fixed it.

Categories