package freshjuice;
class FreshJuice {
enum FreshJuiceSize { SMALL, MEDIUM, LARGE }
FreshJuiceSize size;
}
}
public class FreshJuiceTest {
/**
* #param args the command line arguments
*/
public static void main(String[] args) {
FreshJuice juice = new FreshJuice();
juice.size = FreshJuice.FreshJuiceSize.MEDIUM ;
System.out.println("Size: " + juice.size);
// TODO code application logic here
}
}
This is the error message I am getting:
Error: Main method not found in class freshjuice.FreshJuice, please define the main method as:
public static void main(String[] args)
or a JavaFX application class must extend javafx.application.Application
C:\Users\TheGODMasterDu\AppData\Local\NetBeans\Cache\8.2\executor-snippets\run.xml:53: Java returned: 1
BUILD FAILED (total time: 2 seconds)
Your file must be called FreshJuiceTest.java, because that is the class where your main method is located.
Your file name is FreshJuice.java which does not contains main()
In short netbeans looking for main() in your first class
Either change your file name to FreshJuiceTest.java or swap the code of your classes and define FreshJuice.java as public and remove public from second class
Related
I have a class Normal with the following code:
public class Normal {
private static String myStr = "Not working...";
private static boolean running = true;
public static void main(String[] args) {
while(running) {
System.out.println(myStr);
}
}
}
And I have another class named Injector in another project. Its purpose is to change the values of Normal even though they are not in the same JVM:
public class Injector {
public static void main(String[] args) {
String PID = //Gets PID, which works fine
VirtualMachine vm = VirtualMachine.attach(PID);
/*
Set/Get field values for classes in vm?
*/
}
}
What I want to do is change the values myStr and running in the class Normal to "Working!" and false respectively without changing the code in Normal (Only in Injector).
Thanks in advance
You'll need two JARs:
One is Java Agent that uses Reflection to change the field value. Java Agent's main class should have agentmain entry point.
public static void agentmain(String args, Instrumentation instr) throws Exception {
Class normalClass = Class.forName("Normal");
Field myStrField = normalClass.getDeclaredField("myStr");
myStrField.setAccessible(true);
myStrField.set(null, "Working!");
}
You'll have to add MANIFEST.MF with Agent-Class attribute and pack the agent into a jar file.
The second one is a utility that uses Dynamic Attach to inject the agent jar into the running VM. Let pid be the target Java process ID.
import com.sun.tools.attach.VirtualMachine;
...
VirtualMachine vm = VirtualMachine.attach(pid);
try {
vm.loadAgent(agentJarPath, "");
} finally {
vm.detach();
}
A bit more details in the article.
I Have three classes
StaticHolder.java - Which holds a static variable.
StaticInitializer.java -Responsible only for initializing the variable through a static method.
Application.java - Retrieves the static variables value through getter method.
I thought initializing a static variable once in JVM will not go until we stop the JVM. So I called ran StaticInitializer once which will do the initialization. And tired to access its value from another class which is not working and returning null. Can anyone explain why. Thanks In Advance.
public class StaticHolder {
private static String hello;
public static void ini() {
hello = "Hello World";
}
public static String getHello() {
return hello;
}
public static void setHello(String hello) {
StaticHolder.hello = hello;
}
}
class StaticInitializer {
public static void main(String[] args) {
StaticHolder.ini();
while (true) {
Thread.sleep(1000);
}
}
}
public class Application {
public static void main(String[] args) {
System.out.println(StaticHolder.getHello());
}
}
static does not mean that this value is there forever!
It is only theree for the current java session.
Invocing the java command at the command line starts a new java session where the value needs to be initialized again.
Actually I have a daemon thread which does the initialization and stays alive.And I have another stand alone java program which tries to get the value.
Without knowing that other code involved my gueass is that you did not establish inter process communication.
The easiest way it that you "deamon" opens a server socket and your "stand alone java program" connects to it an queries the desired data through it.
So there is only one main method that can be executed as entry point for the entire application for each JVM run.
When the JVM is executed you can specify which class has to be loaded at start. The Classloader take care to load that class and then the JVM can execute the only one public static void main(String[] args) method.
In Java you need to have at least one class with a public static method named main. I suggest to read this post to understand why it is public static.
The Java Classloader is a part of the Java Runtime Environment that
dynamically loads Java classes into the Java Virtual Machine.
Usually classes are only loaded on demand.
So returning to your question, given that when Application.main is running there is no way to execute StaticHolder.init(), I suggest to change your main in this way:
public class Application {
public static void main(String[] args) {
StaticHolder.init();
System.out.println(StaticHolder.getHello());
}
}
or change StaticHolder in this way and remove the init:
public class StaticHolder {
private static String hello;
static {
hello = "Hello World";
}
public static String getHello() {
return hello;
}
public static void setHello(String hello) {
StaticHolder.hello = hello;
}
}
On the other hand, just to be clear if you run the StaticInitializer.main this has no effect on Application.main execution.
In your program , when main method of StaticInitializer is first executed, a String named hello is initalized. and as ini() method is called, the value 'Hello world' is assigned to hello. Then jvm exists main method, and then stops working. Again when we compile application class,instead of the previous hello variable , a new hello string variable is created with no value assigned(null valued) . That's why you're getting null as output. Thankyou.
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();
}
}
I have made a .jar file in Eclipse to call a paint class. When I finish it gives me an error:
JAR export finished with warnings. See details for additional information.
Exported with compile warnings: Graphics/src/G1.java
Jar export finished with problems. See details for additional information.
Could not find main method from given launch configuration.
Here is my code to call the paint method:
public class G1Starter {
public void main(String[] args)
{
Graphics1 g1 = new Graphics1();
g1.repaint();
}
}
I tried making a main method in the Graphics1 class but it did not work.
Add the static keyword so that the application has a valid entry point
public static void main(String[] args)
Method 'main' should be 'static'
public static void main(String[] args)
I've got a java class, calling a native method and trying to load library:
import java.io.UnsupportedEncodingException;
public class Main {
public static native String getMyString(String s);
/**
* #param args
* #throws UnsupportedEncodingException
*/
public static void main(String[] args) throws UnsupportedEncodingException {
// TODO Auto-generated method stub
// System.out.println("here!");
String s2 = getMyString("string text");
for (Byte b : s2.getBytes("UTF-8")) {
System.out.print(b);
System.out.print(",");
}
}
static {
System.loadLibrary("mylib.so");
}
}
The "mylib.so" is in the directory, where Main.class is located.
When I run java Main I get following exception:
Exception in thread "main" java.lang.UnsatisfiedLinkError: no mylib.so in java.library.path
at java.lang.ClassLoader.loadLibrary(ClassLoader.java:1856)
at java.lang.Runtime.loadLibrary0(Runtime.java:845)
at java.lang.System.loadLibrary(System.java:1084)
at Main.<clinit>(Main.java:24)
What should I change for this to wark?
I've tried setting library full path without success
Do the following:
Use System.loadLibrary("mylib");
Copy mylib.so to libmylib.so
Run java -Djava.library.path=/root/ Main
"How to load native library"
public final class NativeLibsLoaderUtil {
private static final String JAVA_LIBRARY_PATH = "java.library.path";
private static final String SYS_PATHS = "sys_paths";
private NativeLibsLoaderUtil() {
}
private static void addLibsToJavaLibraryPath(final String tmpDirName) {
try {
System.setProperty(JAVA_LIBRARY_PATH, tmpDirName);
/* Optionally add these two lines */
System.setProperty("jna.library.path", tmpDirName);
System.setProperty("jni.library.path", tmpDirName);
final Field fieldSysPath = ClassLoader.class.getDeclaredField(SYS_PATHS);
fieldSysPath.setAccessible(true);
fieldSysPath.set(null, null);
} catch (IllegalAccessException | NoSuchFieldException e) {
LOGGER.error(e.getMessage(), e);
}
}
}
Where tmpDirName is a directory where you store your library.
Or you can modify above class and use temp directory from your system property, like this:
/**
* Temporary directory system property name
*/
private static final String JAVA_IO_TMPDIR = "java.io.tmpdir";
/**
*
* #return
*/
private static File getTempDir() {
final String tmpDirName = System.getProperty(JAVA_IO_TMPDIR);
final File tmpDir = new File(tmpDirName);
if (!tmpDir.exists()) {
tmpDir.mkdir();
}
return tmpDir;
}
!But first you have to copy there your native lib :)
Then to load native library call "addLibsToJavaLibraryPath" method in static block in "most root" class before any class constructor was executed.
static {
NativeLibsLoaderUtil.addLibsToJavaLibraryPath("/tmp");
}
You should add the so to library path:
-Djava.libarary.path= (this is in the java command).
if you run from eclipse:
How to add native library to "java.library.path" with Eclipse launch (instead of overriding it)
If you compiled opencv, on installation you should have seen something like:
make install:
-- Up-to-date: /usr/local/share/java/opencv4/libopencv_java460.so
-- Up-to-date: /usr/local/share/java/opencv4/opencv-460.jar
make a hard link to the /usr/lib/ folder:
$ sudo ln /usr/local/share/java/opencv4/libopencv_java460.so /usr/lib/libopencv_java460.so
And then just run:
System.loadLibrary(Core.NATIVE_LIBRARY_NAME);
that he will get the *.so
As Reimeus answered.
Or you can use
System.load("/Library/Path/libsample.so");