How to run package com.zetcode using DrJava - java

Im trying to run this code at http://zetcode.com/tutorials/javagamestutorial/puzzle/
1 error found:
File: /Users/java/PuzzleEx.java [line: (no source location)]
Error: File is in the wrong directory or is declared part of the wrong package. Directory name 'java' does not match package name 'zetcode'.
Please help, how do I run it?

Well, first you need to compile it using javac, then you could run it. However, I'd suggest doing nothing of the sort. Just browsing through the link you provided, I'm seeing some bad practices, and outdated code (not declaring variables as interfaces, stuffing all the classes in one file, etc). Instead of following some third party, potentially outdated, tutorial, take a look at the official tutorials: https://docs.oracle.com/javase/tutorial/
Not only are the official tutorials always up to date, but they're also laid out in easy-to-follow trails and paths.
Edit:
Also, the reason it's not working is exactly as the error says: it's in a different folder than its package is declared to be. If you don't know what a package is, please take a step back and learn the basics first.

Related

How to import packages from the external Library in Java?

I'm a n00b coder. I found an interesting library and trying to start toying with it. Which is not going great. This library is from 99' and uses JUnit (which I'm unfamiliar with) so there is a lot of confusing stuff. But it seems like the source of my failing even more elementary. Namely I have troubles importing packages.
This library has a test called StandardEvalTest.java. I moved to it to main Java directory and now I'm trying and failing to launch it using JUnit.
This package path org.pokersource.game.Deck goes directly from the directory where the test StandardEvalTest.java sits.
I also added the main java directory to the PATH environmental variable. Which as I assumed will allow import to locate the package.
None of those two things help. Also I was suspecting that maybe Deck.java and Deck.class are not enough and I have to do some work to create a package from it. But as far as I can say from Oracle doc the only thing needed is a package name in the header. Which seems to be present.
So I'm out of moves. Please help!
PS: Some additional info inspired by #Dhrubo 's answer:
The test I'm trying to run indeed sits in the main java folder of the library. (I moved it here hoping that when running from here it would be easier to find the package)
If I'm trying to compile the test instead of running it with JUnit he seem to fail to find JUnit classes and other JUnit related stuff.
[Oh OK I'm an idiot! Dont't mind me]
You should include the package while running StandardEvalTest.java as below
javac -cp [classpath] org.pokersource.game.StandardEvalTest.java
and run it from package root directory, I am assuming it is custom java file that you want to compile. You run directory should be parent of your package directory.
** I also see, you are trying to compile StandardEvalTest.java instead of Deck.java ... then check your StandardEvalTest.java file whether it exists in desired location.

Creating Java Packages in IntelliJ

I've been using Eclipse for a while and I'm having trouble understanding what's going on with my first project in IntelliJ. I've read the documentation, and searched other questions, but I still can't seem to grasp it. I think there is something wrong with my project structure. This is what my structure currently looks like;
I'm trying to run the JavaForLoop class, but whenever I do, compilation fails because I have errors in the StringMethods class of the strings package. My question is why would that prevent compilation if the two classes are in separate packages? Neither class uses the other, and they both have the appropriate package declaration statements. With a similar structure in Eclipse, this would work. Should I be using a different project structure?
By default IDEA adds Build Configuration which is executed before launch and includes following steps (taken from here):
Compiling source code in the source path of a module and placing results to the output path.
Compiling source code in the test path of a module and placing results to the test output path.
Creating copies of the resource files in the output path.
Reporting problems in the Messages tool window.
check if it's your case in Edit Configuration screen and if so, remove it.
To use a class from a different package you must declare a import statement to the class.
In your JavaForLoop.java add the import before the class statement (and after package declaration where its the case)
//package ...
import strings.StringMethods;
//public class JavaForLoop { and the rest of the code
Intellij uses regular javac, which will fail to compile if you have errors anywhere in the code.
Eclipse has it's own compiler, that allows to compile and even run code that has compilation errors, causing a runtime exception if any part of the code that has errors is run. This allows you to run parts of the code that work even if other pieces of code are failing.
The simple solution is to resolve your compilation errors. You can also use the eclipse compiler with Intellij, but I've never done this so I can't comment on how well it works.

javax.mail.MimeMessage not available even though package is in CLASSPATH

I'm trying to develop an email parser - to take email that's in a file and be able to programmatically handle the various components - to know who the sender and recipient were, the subject line, main body, and any attachments. I intend to extract attachments as individual files, but I'm stumped right at the beginning.
I started with an already working java program in a fully-functional development environment and have begun adding to it. One of the first additions was this line (then later, set of lines):
import javax.mail.*;
import javax.mail.internet;
import javax.mail.internet.MimeUtility;
Later, in an appropriate place, we have this humble beginning:
MimeMessage m = null;
I was shocked to find that the compile failed with only these two changes, and I learned here (on another StackOverflow page) that the package javax.mail package isn't included in the standard JDK. Puzzled, I looked and found this:
# rpm -qa | grep -i java
android-json-org-java-4.3-0.2.r3.1.fc21.noarch
snappy-java-1.0.5-2.fc21.noarch
tzdata-java-2015b-1.fc21.noarch
python-javapackages-4.1.0-7.fc21.noarch
postgresql94-jdbc-javadoc-9.3.1101-1.f21.noarch
protobuf-java-2.5.0-11.fc21.x86_64
java-1.8.0-openjdk-1.8.0.40-25.b25.fc21.x86_64
java-1.8.0-openjdk-headless-1.8.0.40-25.b25.fc21.x86_64
javassist-3.18.1-2.fc21.noarch
apache-commons-javaflow-1.0-0.8.20120509SNAPSHOT.fc21.noarch
javapackages-tools-4.1.0-7.fc21.noarch
java-1.8.0-openjdk-devel-1.8.0.40-25.b25.fc21.x86_64
antlr3-java-3.5.2-2.fc21.noarch
javamail-1.5.1-3.fc21.noarch
xz-java-1.5-3.fc21.noarch
abrt-java-connector-1.1.0-2.fc21.x86_64
Please note that yes, actually, JavaMail is installed - version 1.5.1-3. However, just to be a belts and suspenders kinda person, I found the JavaMail project and downloaded the latest production version, and put it in the CLASSPATH.
To my great surprise, this did not cure the problem! Being the careful type, and keeping things simple to prove the way as I go, I simply removed the javax.mail.jar file from the library directory, then removed the two lines from my program, recompiled and it worked. Then, I added back in the import line, and it failed. Then I moved the jar file back into the library directory and the compile succeeded, confirming that the javax.mail package was being loaded.
However, when I added in the first reference to the library, MimeMessage (see the line above), the compile failed.
So, of course, I went to check the documentation! Indeed, MimeMessage is an available class.
What am I doing wrong?! I mean, geez, I've been using Java literally since version 1.0 - I'm not known for making too many dumb mistakes, but I figure I must be!
Two open questions come to my mind:
1) Why isn't the installed version of the JavaMail package being used? CLEARLY I had to add it. And do note that I don't have to do ANYTHING special to CLASSPATH (or anywhere else) to get all the rest of Java! And;
2) Now that I've got a JavaMail package in there, why is it not discovering the MimeMessage class?
Any / all help appreciated - and if you spot me being abjectly stupid, please point out my error gently!
Additional Information:
I added two more import lines, so there are now the three listed above. Curiously, when I have the .jar file in the path specified by CLASSPATH, but not explicitly cited, I get one error per import, but when I explicitly cite the .jar file as an explicit item, I only get ONE error! That one error is on:
import javax.mail.internet;
Naturally, knowing that the jar is actually just a zip, I unzipped and looked. Sure enough, a directory named "internet" is there, populated with 38 class files.
Somewhere along the way, I had a wild idea that somehow there was ANOTHER javax.mail entry on the system or in the CLASSPATH, so I looked. This is Fedora Core, so it's under /usr/lib, and I did NOT find another file including the string "mail" in its name, so I presume that's not it, either.
I noticed in the NOTES.txt file that there were some notes about not unpacking the jar for some applications because that was seen as a security risk, but I figured that for what I want to do there is no such risk and maybe it would work, but I couldn't get THAT to work either...
I've also been trying using -cp versus actually altering the CLASSPATH variable, but it doesn't seem to matter. I apparently get identical results either way, so I've just been using -cp for testing because it's faster to try various alternatives.
MimeMessage is in the javax.mail.internet package, which you haven't imported.
EDITED BY QUESTION ASKER:
Actually, I was trying to import, among other things:
import javax.mail.internet;
And that didn't work. What I was missing was that there's nothing to import on just javax.mail.internet. Changing it to:
import javax.mail.internet.*;
worked just fine!
So, while Bill wasn't quite correct, this answer plus his comment got me to see what I was blind to... and I feel silly!

NoClassDefFoundError when code moved from R2007a to R2013a

I am working on moving code from R2007a to R2013a. I am getting a java.lang.NoClassDefFoundError during my run in R2013a which does not appear in R2007a. It occurs when I call.
feval('get',fname,jevent);
Where fname is a product.ProxyField object for an Object Filter and jevent is a product.format.java.internal.JavaEvent.
The class is in a jar file on the path and is being accessed by another class in the same jar file. The stack trace does not leave the realm of the product if that helps.
I do not have access to the original code for the jar file. I do have access to code derived from that original code and both classes are in the same package. I'm guessing this has something to do with differences in the java version but I'm not sure what to do since I don't have the original code to recompile.
Unfortunately I can't provide actual source or full detail but a google search only yielded results for MATLAB startup issues. Any thoughts?
Seems like the difference between R2007a and R2013a is that the first uses 1.5 jre and second uses 1.6 jre. It would be easier to help you if you provided the stack trace showing the exception. Sometimes classes get moved around in between jvm versions, so having the actual missing classes would help in determining if the missing class is a class that was just moved around to a different package. You could take the missing class, google it adding the same exception message as you put above and seeing who else ran into similar issues.

Getting Guava to work on GWT

I can't get Guava to work with GWT. I'm using Eclipse, I've added both guava-11.0.2.jar and guava-gwt-11.0.2.jar to my class path, inherited the module com.google.common.collect.Collect in my gwt.xml file but still can't get it working.
It compiles but when I run it a lot of error appear. Many of them refering to unresolvable annotation javax.annotation.Nullable
I tried adding jsr305.jar to my class path but nothing chanded.
Also some errors appear depending on which version of Guava I include (tried with 11.0.2, 11.0.1, 10.0.1) and if I include or not the jsr305.jar
Some examples of these error are:
The type PersonActivity.PersonFilterPredicate must implement the inherited abstract method Predicate<PersonRoleProxy>.apply(Object)
The method compare(T, T) in the type Ordering<T> is not applicable for the arguments (Object, Object)
Which is quite strange because the code compiles and these kind of error should appear at compile time.
We have been embarrassingly unable to figure out the solution to the #Nullable problem, which we believe to be a GWT problem. You can follow along at http://code.google.com/p/guava-libraries/issues/detail?id=776 We're trying to get help from the GWT team, but things have been kind of crazy lately for the people who work on the part of the compiler we're interested in. It does look like you can work around the problem by removing -strict from your GWT compilation arguments, but of course that's a terrible workaround.
Other users' experiences suggest that the weird other errors you're seeing are related to the #Nullable problem, though I haven't seen definitive confirmation. You might also confirm that all your imports are com.google.common.* and not com.google.gwt.thirdparty.guava.common.*
Sorry for all the trouble. This is one of a few major thorns in our side at the moment.
The following workaround works for GWT 2.4.0 with
Guava 14.0 RC1 and
jsr305-1.3.9.jar
Create a file named Annotation.gwt.xml with the following content:
<module>
<source path=""/>
</module>
Within the archive jsr305-1.3.9.jar copy this file into sub-directory javax/annotation.
Add the following line to your applications .gwt.xml file:
<inherits name='javax.annotation.Annotation'/>

Categories

Resources