I tried to use the UNIXProcess.class, but the eclipse keeps warning me that UNIXProcess is not visible.
The code is simple:
import java.lang.Process;
public class Simple {
UNIXProcess process;
}
Can anyone know to use the UNIXProcess?
Thanks
java.lang.UNIXProcess is declared as default visibility which means that it cannot be seen by classes outside of the package java.lang.*. Use ProcessBuilder or Runtime.exec(), instead, to create a valid Process object.
Related
So I have two Jars I want to use in my project from here http://www.jhlabs.com/ip/filters/index.html
I added both CheckFilter and MarbleFilter to my class path. But when I do
CheckFilter();
It says I have to create a method CheckFilter()
I'm pretty sure that's the method I need to call to use that effect. But when I try any of the other methods in the library Jar it still gives me the same thing.
I have no experience with importing/using external libraries. Any help would be great.
checkFilter = new CheckFilter();
CheckFilter();
I tried above and it says I need to create a local variable checkFilter
How are you writing up the code. I will suggest to use eclipse IDE, it will make your tasks simple
If you are using eclipse. You need to do import the jar Filters.jar to your build path
which as you mentioned you downloaded from JHLabs Download page
I found Filters.jar inside dist directory.
Then you will be able to import the class or package
import com.jhlabs.image.*;
OR
import com.jhlabs.image.CheckFilter;
After importing the class or package you will be able to create object to it by
CheckFilter checkFilter = new CheckFilter();
In case you are totally new you can take help from people over IRC or chat and get going.
Someone would be able to quickly help you out
----==----==----==----==----==----==----==----==----
Read your comments and Question again.
You are totally missing the point. If you call to CheckFilter() directly without invoking new keyword, compiler will consider you are trying to access a method which is inside the class you are writing up. and give you error.
As I mentioned above. Your are trying to accessing Instance variable for the class without declaring it. Either do
CheckFilter checkFilter;
before you access checkFilter variable or directly instantiate the class the way I mentioned.
Seems to me you are missing a log of points :D
Methods don't exist without a class. That is probably the constructor to a class. Use
CheckFilter checkFilter = new CheckFilter();
instead. Then call methods on checkFilter.
From #andy-thomas in this similar question
The import statement imports classes from the jar file, not the jar file itself.
An import statement of the form:
import stdlib.*;
will import all the classes in the package stdlib.
Oracle provides this tutorial on importing.
This statement
CheckFilter();
Tries to call a method CheckFilter defined in your class, which is not the case. If this is a utility function, this may be a static method, in which case you can call it like this
ClassName.CheckFilter(); // replace ClassName with the class containing this function
If not, then you may have to instantiate an object
ClassName obj = new ClassName();
obj.CheckFilter();
or skip the object variable
new ClassName().CheckFilter(); // Not prefered
To add jar files, right click on your project in "Package Explorer", go to "Configure Build Path" and then to "Add External Jars".
Set the jar (libraries) into your classpath and use import statements in your java code to include the required Classes.
Recently I have encountered some problem which seems a little strange to me.
In order to use some predefined class, I imported two .jar files say foo.jar and bar.jar(Both were written by others)
And my source code is like the following:
package jerry.deque
public class Deque {
.....
.....
Foo item = new Foo(); //Already defined in the foo.jar
.....
}
I added the external library exactly as what How to Import a Jar in Eclipse
did. But when I tried to use the class defined in foo.jar Eclipse shows me that "Foo can't be resolved to a type".
I spent a lot of time to fix this problem and finally succeeded after I removed
the clause: "package jerry.deque" at the beginning of my class file.
I think this is weird because just a few days ago when I was doing some Android development, I followed the same way to add a Twitter API library. And it works fine even when I declared "package jerry.search_twittes" at the beginning of my .java
file. I'm confused by this problem and couldn't figure out what's going wrong. Could someone help me to explain it in detail? Thanks very much.
Check that Foo is same package as Deque class. If they are not same
package, you need to import Foo class in Deque class.
For example,
package jerry.deque;
import packagename.foo; // packagename.foo
public class Deque {
.....
.....
Foo item = new Foo(); //Already defined in the foo.jar
.....
}
Added Explanation
I want you to check access modifier of Foo class carefully.
There are 2 access level for top level(class) access control . These
are public, or package-private (no explicit modifier).
Your Foo class is under default package(not specified package)and may be no explicit access modifier. Hope so! Then, all classes under default package can access to Foo class. That's why when you remove package jerry.deque clause, it works.
Similarly, I want you to check Android development java code in which it works fine even when you declared "package jerry.search_twittes". In that case, classes inside Twitter API library's access modifier is public.So you can access it from anywhere.
For more information you can read this.Is this information helpful???
Foo is in default package. Classes from default package cannot be imported directly.
So when you remove the package declaration in your code, you don't get the error.
You can look for reflection api or write a proxy in the default package for that class.
Im trying to use Scala and Java in one project. Im working with the Scla IDE for Eclipse. I have two packages in my Scala Project: one for my scala code and one for my java code.
Now lets say I create new JavaClass with one static member.
package javastuff;
public class MyJavaClass {
public static String MESSAGE = "Im Java";
}
After that Im trying to get access to this variale and somehow I cannot. Funny thing, because scala is able to see the Java class "MyJavaClass" just not able to see MESSAGE.
import javastuff.MyJavaClass
object Main {
def main(args: Array[String]) {
println(MyJavaClass.MESSAGE)
}
}
Value MESSAGE is not a member of object javastuff.MyJavaClass
If I use Project/Clean... 1-2x times eclipse is maybe starting realizing that the member MESSAGE is really there and everything is fine. Is this normal? Maybe Im doing something wrong, I know eclipse is really a bad IDE and I should maybe try IntelliJ, but somehow I like eclipse and I would like to use later some of my favorite plugins, thats why I would not change the IDE just because of this problem. Any ideas how to handle this problem better?
Scala doesn't have any static fields. Here is a blogpost about it
btw. public static without final is pretty bad design (no encapsulation => possible memory leaks)
So, I have something written in Java, and I want to extend it in Scala... The issue I'm running into is that Scala isn't seeing methods I need.
Here is how it's set up:
Player extends Mob, and Mob extends Entity.
I need to access a method in Player that isn't defined in Mob or Entity, but Scala doesn't think it exists even though Java does.
It can see methods defined by Mob and Entity just fine. Also, all the methods I'm talking about are non-static.
So, am I doing something wrong, or is this a limitation imposed by Scala?
Edit --
Here is the relevant code:
package test
import rsca.gs.model.Player
object Test {
def handle(p:Player): Unit = {
p.getActionSender().sendTeleBubble(0, 0, false);
}
}
Player class:
package rsca.gs.model;
// imports
public final class Player extends Mob {
// Implemented methods (not going to post them, as there are quite a few)
// Relevant code
private MiscPacketBuilder actionSender;
public MiscPacketBuilder getActionSender() {
return actionSender;
}
}
Error:
value getActionSender is not a member of rsca.gs.model.Player
I never encountered such problems, and you probably checked your configuration and everything else twice, so I would guess this is some Eclipse related build issue. You should try to build from the command line in order to see whether Scala or Eclipse is the problem.
Is it possible for you to run a test against the class just to see if you got the right one?
p.getClass.getMethods
... and if possible (may run into NPE) in order to find the source:
p.getClass.getProtectionDomain.getCodeSource.getLocation.getPath
When compiling the Scala class, do something like this:
scalac *.scala *.java
This way, Scala will look a the Java code to see what is available. If, however, the Java code is already compiled and provided as a jar file, just add it to the classpath used when compiling the Scala code.
In Java- "Static Members of the default package cannot be imported"- Can some one explain this statement? It would be better if its with an example. I am not sure if it has a really simple answer but then I tried to understand but couldn't figure it out.
It means that if a class is defined in the default package (meaning it doesn't have any package definition), then you can't import it's static methods in another class. So the following code wouldn't work:
// Example1.java
public class Example1 {
public static void example1() {
System.out.println("Example1");
}
}
// Example2.java
import static Example1.*; // THIS IMPORT FAILS
public class Example2 {
public static void main(String... args) {
example1();
}
}
The import fails because you can't import static methods from a class that's in the default package, which is the case for Example1. In fact, you can't even use a non-static import.
This bug report has some discussion about why Java acts this way, and it was eventually closed as "not a defect" -- it's the way Java was designed to behave. Default package just has some unexpected behavior, and this is one of the reasons why programmers are encouraged to never used the default package.
as #kageb Brasee mentions : It is true that you cannot do the static import or non-static import of the class which is in a default package.
but there is a case where you can use the class (of default package) in another class: -> And this can only be done if and only if the class (in which you want to use the class of default package) is also present in a default package
if both the classes are in default packages (no matter at what location they are present) then you can use them (note : we are not import them just using them)
eg. if i want to import a class temp.class (which is in a default package) located at Home/files/temp.class into my program use.java
then just set the CLASSPATH while compiling it
you can do that in two ways : permanent set OR temporary set (Not using technical terms )
permanent set : by setting the CLASSPATH (which is an environment variable) variable (different methods to do that for different OS's) -> for mac - - > export CLASSPATH=Home/files/
in this method the CLASSPATH environment variable is set till your terminal is open
so in this case :
export CLASSPATH=Home/files/
javac use.java
java use
temporary set : in this method we use either of two option provided for both java and javac (java compiler) tool and they are -classpath and -cp (both these do the same job, its just -cp is short for the -classpath), in this method of setting classpath for the other files the main difference is that in this type the address(path) of the file is set only for the time period while that command (operation) is executing
as soon as the statement execution complete the value of CLASSPATH(the environment) -> again reaches to the same path as it was earlier,
Note: by default the CLASSPATH is . (i.e. representing the same directory)
And in this case :
java -cp .:Home/files use.java // Note: don't forget . and : is for separating the different paths
java use
Hope it helped :)
Create a new package under the default package such as sample>utils, then transfer that Java file whose Static Methods are not imported, to new created utils package, such as sample>utils>ResizeHelper.java :
And you are good to Go.