I have found some code snippet in the internet. But it is missing some classes. Where can I get the missing classes?
These are the errors I get:
package com.sun.j3d.utils.universe.SimpleUniverse doesn't exist
package com.sun.j3d.utils.geometry.ColorCube doesn't exist
package javax.media.j3d.BranchGroup doesn't exist.
Here is the code :
import com.sun.j3d.utils.universe.SimpleUniverse;
import com.sun.j3d.utils.geometry.ColorCube;
import javax.media.j3d.BranchGroup;
public class Hello3d {
public Hello3d()
{
SimpleUniverse universe = new SimpleUniverse();
BranchGroup group = new BranchGroup();
group.addChild(new ColorCube(0.3));
universe.getViewingPlatform().setNominalViewingTransform();
universe.addBranchGraph(group);
}
public static void main( String[] args ) {
new Hello3d();
}
}
You'll need to include the Java3D libraries in your class path.
You can get them from java3d.java.net. There's a couple of options, but you could download the build zip for your architecture, unzip it, unzip the j3d-j3d.zip, navigate to lib/ext and copy the jars you find into your classpath (-classpath path\to\j3dutils.jar for example)
We'll need more info about your environment (Are you using an IDE? Which one? Using javac from the command line? What command are you using?) if you need more help.
Looks like maybe you don't have the java3d library installed? If you go to sun and look for java3d, they have a decent installer. If you're starting with a jdk from the sun site, the installer seems to find the right place for the java3d classes within the existing jdk/jre directory.
Related
I want to be able to use the Apache Commons Math Library in Java but I cannot get it to work correctly and the main site is frustratingly unhelpful (at least for a novice like me) and I haven't been able to find a solution on here yet.
I went to http://commons.apache.org/proper/commons-math/download_math.cgi
downloaded the first option commons-math3-3.6.1-bin.tar.gz
unzipped it and put it into the folder with the java class that I am trying to build.
I then did the command import org.apache.commons.math3;
But I get Error: package org.apache.commons does not exist
Could someone explain (preferably in detail that not even a novice would misunderstand) why this isn't working and what I should do?
Thanks!
First you need to download jar from repository
https://mvnrepository.com/artifact/org.apache.commons/commons-math3/3.6.1
put it to folder test, in same folder create file Test.java with class Test something like this
import org.apache.commons.math3.analysis.function.Abs;
class Test {
public static void main (String ... args) {
Abs abs = new Abs();
System.out.println(abs.value(-10.0d));
}
}
after that compile it with command javac -cp "commons-math3-3.6.1.jar" Test.java
and run it java -cp ".;commons-math3-3.6.1.jar" Test
output will be 10.0
I'm making a simple window that renders a moving triangle using the Java OpenGL libraries (jogamp) latest stable build 2.2.4, I've got everything set up like the tutorials I found on the JOGL website, the thing is while running this code on Netbeans it works fine, the method GetDefault() gets my systems OpenGL version, the window renders and also the animation. The problem is when I build the project, I make sure all the jars and .dll files are in the /dist folder and execute the Jar with "java -jar name.jar" I get the following error message:
Exception in thread "main" javax.media.opengl.GLException: Profile GL_DEFAULT is not
available on null, but: []
at javax.media.opengl.GLProfile.get(GLProfile.java:962)
at javax.media.opengl.GLProfile.getDefault(GLProfile.java:693)
at javax.media.opengl.GLProfile.getDefault(GLProfile.java:704)
at cl.lucas.clases.Main.main(Main.java:14)
This is my Main class code:
import com.jogamp.opengl.util.FPSAnimator;
import java.awt.Frame;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import javax.media.opengl.GLCapabilities;
import javax.media.opengl.GLProfile;
import javax.media.opengl.awt.GLCanvas;
public class Main {
public static void main(String[] args) {
GLProfile glp = GLProfile.getDefault();
GLCapabilities caps = new GLCapabilities(glp);
GLCanvas canvas = new GLCanvas(caps);
Frame frame = new Frame("AWT Window Test");
frame.setSize(300, 300);
frame.add(canvas);
frame.setVisible(true);
frame.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
canvas.addGLEventListener(new EscenaSimple());
FPSAnimator animator = new FPSAnimator(canvas, 60);
animator.start();
}
}
Any ideas why it would work only on the IDE but not outside, also I'm running Windows 7, I also tried running the program on Eclipse and no problem there, the thing is the jar executable, maybe I'm missing something.
Your fat JAR is probably incomplete, mine works flawlessly under GNU Linux, OS X and Windows. You must follow this layout, you can use this Ant script within Eclipse to make it work. My detailed instructions are here and they work even though you use only JogAmp without the engine mentioned in my tutorial.
Numerous developers already asked us lots of questions about fat JARs, rather contact us on our official forum. You don't need to put the native libraries (DLLs under Windows) into a particular directory, setting the Java library path is necessary only if you don't use the JARs containing the native libraries, just keep the automated native library loading enabled (it's on by default) and GlueGen will detect your architecture, extract the proper native libraries (for JOGL, JOAL and JOCL) and load them.
If you really want to use the "-jar" option, your JAR must contain absolutely everything needed for your application to run and in the expected location. Read carefully the paragraph of my tutorial, you have to set at least the manifest attribute "Main-Class" to make it work properly, so that Java can find the main entry point of your application.
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.
I'm a little lost here. We were given a jar file which contained 3 different .class files in it. I successfully built the path to the jar file, but I don't know how to use them. The class files obviously contain methods that can be used, but I've never worked with a jar file before so I don't know how to use them. I'm not sure if I have to import them somehow, how to instantiate them or anything. I have searched for tutorials to no avail.
All I'm looking for is some guidance so I can get moving on this project. If it is a case where I have to import them somehow how would I do it? For example, I have WikiEdits.class contained in WikiEdits.jar. The name of my main class is P2. Can someone show me a brief example of how this works?
Add the jar to your classpath, if you are using an IDE.
Then, a java class that uses it would look like something like this:
package p2;
import blah.WikiEdits; //references a class in the jar
public final class P2 { //(this is a strange name for a class, by the way)
public static void main(String... args){
//builds a new object of the given class
WikiEdits thing = new WikiEdits();
}
}
If you are using the command line, these examples may help:
http://www.javapractices.com/topic/TopicAction.do?Id=243
you need to add WikiEdits.jat to your path project, then import and instanciate the class.
import WikiEdits
P2 p = new P2();
p.somemethod();
Static class:
WikiEdit.someMethod();
In your java class add the relevant imports from the jar. And then from command line, you can compile and run your class using the classes from jar by definining the right classpath:
Compilation
on windows:
javac -cp .;pathtoyourjar YourClass.java
on linux:
javac -cp .:pathtoyourjar YourClass.java
Execution
on windows:
java -cp .;pathtoyourjar YourClass
on linux:
java -cp .:pathtoyourjar YourClass
If you are using Eclipse then follow this link to know the steps to add jar to your project:
http://www.cs.duke.edu/courses/cps004g/fall05/assign/final/addlibrary.html
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.