How to call function in another project in Java - java

I checked few other stackoverflow posts. But I am not able to call a function that is present in another project.
SampleTwo.java
package a.two;
public class SampleTwo {
public static void bar() {
System.out.println("Bar");
}
public static void main(String[] args) {
bar();
}
}
Updated SampleOne.java
package a.samp;
import a.two.*;
public class SampleOne {
public static void foo() {
System.out.println("Foo");
SampleTwo.bar(); // <------ I want this to work
}
public static void main(String[] args) {
foo();
}
}
Here is my project properties of both projects
[Version 1: Before blackjack26's answer]
Eclipse shows the error SampleTwo cannot be resolved
Can you please tell if I am missing something? Thanks!
[Version 2: After blackjack26's answer]
Exception in thread "main" java.lang.Error: Unresolved compilation problem:
SampleTwo cannot be resolved
[Version 3] Removed SampleOne from SampleTwo's project property fixed it.

In your SampleOne Java file, you are missing and import to access SampleTwo.
You should add the following to your imports for SampleOne:
import a.two.SampleTwo;

Related

- Syntax error on token ".", # expected after this token

My Eclipse worked fine a couple of days ago before a Windows update. Now I get error messages whenever I'm trying to do anything in Eclipse. Just a simple program as this will display a bunch of error messages:
package lab6;
public class Hellomsg {
System.out.println("Hello.");
}
These are the errors I receive on the same line as I have my
"System.out.println":
"Multiple markers at this line
- Syntax error, insert ")" to complete MethodDeclaration
- Syntax error on token ".", # expected after this token
- Syntax error, insert "Identifier (" to complete MethodHeaderName"
You can't just have statements floating in the middle of classes in Java. You either need to put them in methods:
package lab6;
public class Hellomsg {
public void myMethod() {
System.out.println("Hello.");
}
}
Or in static blocks:
package lab6;
public class Hellomsg {
static {
System.out.println("Hello.");
}
}
You can't have statements outside of initializer blocks or methods.
Try something like this:
public class Hellomsg {
{
System.out.println("Hello.");
}
}
or this
public class Hellomsg {
public void printMessage(){
System.out.println("Hello.");
}
}
You have a method call outside of a method which is not possible.
Correct code Looks like:
public class Hellomsg {
public static void main(String[] args) {
System.out.println("Hello.");
}
}
Just now I too faced the same issue, so I think I can answer this question.
You have to write the code inside the methods not on the class, class are generally used to do some initialization for the variables and writing methods.
So for your issue, I'm just adding your statement inside the main function.
package lab6;
public class Hellomsg {
public static void main(String args[]){
System.out.println("Hello.");
}
}
Execute the above, the code will work now.

Lambda/default methods/type erasure quirk/bug using ECJ?

Came accross this today and spent ages trying to reproduce/figure out what was happening. Can somebody explain why this happens or is this a bug with type erasure/default methods/lambda's/polymorphism? Uncommenting the default method makes it run fine, but I would have expected this to work as is
Output:
Works fine with an object
Calling consume
Hello
Calling accept with context
Hello
Calling accept via consumer...
Exception in thread "main" java.lang.AbstractMethodError: Method test/LambdaTest$$Lambda$1.accept(Ljava/lang/Object;)V is abstract
at test.LambdaTest$$Lambda$1/834600351.accept(Unknown Source)
at test.LambdaTest.main(LambdaTest.java:24)
Code
package test;
import java.util.function.Consumer;
public class LambdaTest {
public static void main(String[] args) {
Consumer<Context> contextIgnoringObject = new ContextUnawareObject();
contextIgnoringObject.accept(new Context());
ContextIgnorer contextIgnoringLambda = () -> {
System.err.println("Hello");
};
System.err.println("Calling consume");
contextIgnoringLambda.consume();
System.err.println("Calling accept with context");
contextIgnoringLambda.accept(new Context());
Consumer<Context> consumer = contextIgnoringLambda;
System.err.println("Calling accept via consumer...");
consumer.accept(new Context());
}
#FunctionalInterface
public interface ContextIgnorer extends Consumer<Context> {
// default void accept(Object object) {
// System.err.println("Manual bridge method");
// accept((Context)object);
// }
#Override
default void accept(Context context) {
consume();
}
void consume();
}
public static class ContextUnawareObject implements ContextIgnorer {
#Override
public void consume() {
System.err.println("Works fine with an object");
}
}
public static class Context {
}
}
The problem appears with older ECJ compiler (3.10.0):
$ java -jar org.eclipse.jdt.core-3.10.0.v20140604-1726.jar -source 1.8 LambdaTest.java
$ java LambdaTest
Works fine with an object
Calling consume
Hello
Calling accept with context
Hello
Calling accept via consumer...
Exception in thread "main" java.lang.AbstractMethodError: Method LambdaTest$$Lambda$1.accept(Ljava/lang/Object;)V is abstract
at LambdaTest$$Lambda$1/424058530.accept(Unknown Source)
at LambdaTest.main(LambdaTest.java:24)
Using org.eclipse.jdt.core_3.10.0.v20140902-0626.jar or newer solves the problem. The Oracle javac compiler has no such problem. Thus the solution would be to update your ECJ compiler or move to the javac.

Intellij14.1 Error:No main class for module

I have just try HelloWorld
public class hw {
public static void main(String []args){
System.out.println("HelloWorld");
}
}
but, console said when I tried to compile:
Error:No main class for module: HelloWorldTest
Error:Compilation failed
I don't know I don't know what I have wrong, Why it give me this warn ?
Create a file HelloWorld.java at the root of your source package:
public class HelloWorld {
public static void main(String... args) {
System.out.println("HelloWorld");
}
}
Then from the project explorer (left pane), right click on your HelloWorld class, and select Run 'HelloWorld.main()'
Do you have the Haxe plugin installed?
If so it's likely the source of the problem.
https://devnet.jetbrains.com/thread/435604?tstart=0 and https://devnet.jetbrains.com/thread/435708
Try the fix in the first link or uninstall the plugin.

Getting error in Eclipse: syntax error on token start identifier expected

I am getting a strange error while creating a simple thread program in JAVA using Eclipse. The code is:
package threadshow;
public class Thread_Show extends Thread{
public void run(){
System.out.println("Inside the thread");
}
}
class Thread_Definition{
Thread_Show ts=new Thread_Show();
ts.start(); //Getting the error here
}
I am getting error "syntax error on token start identifier expected" in the line ts.start();. Why am I getting this?
EDIT I have used the code from http://tutorials.jenkov.com/java-concurrency/creating-and-starting-threads.html#thread-subclass
Found a very bad mistake done my me. Forgot to add public static void main(String args[]) in the Thread_Definition class.
You can't start your method inside class. Create some method first.
Are you defining both of your classes in the same java file?. If so, you define both the classes in different java files naming Thread_show and Thread_definition. Then inside Thread_definition you can create an object of Thread_show and call its function.
ADD main method-public static void main(String[] args)
package threadshow;
public class Thread_Show extends Thread
{
public void run()
{
System.out.println("Inside the thread");
}
}
class Thread_Definition
{
public static void main(String[] args)
{
Thread_Show ts=new Thread_Show();
ts.start();
}
}

Java: Can't get my main method to work

I'm using Fedora and I have had some issues to get javac to work (I finally succeeded by making an alias). But now I can't execute my java code. I get the error in the title.
Here is the class that contains the main method:
public class test
{
public static void main(String args)
{
int res[]= {4,2,6};
res=Trieur.tri(res);
for(int i: res)
System.out.println(i);
}
}
I've been trying a lot of solutions in this forum but none seems to work. The program compiles successfully.
Can you please help me?
change this:
public static void main(String args[])
Or as public static void main(String[] args). Either syntax is valid in Java, although this format is arguably slightly more popular.

Categories