Anyone have an obnoxious "Hello World" program?

  • (2 Pages)
  • +
  • 1
  • 2

19 Replies - 1015 Views - Last Post: 10 October 2013 - 11:34 AM Rate Topic: -----

#16 cfoley  Icon User is offline

  • Cabbage
  • member icon

Reputation: 2069
  • View blog
  • Posts: 4,307
  • Joined: 11-December 07

Re: Anyone have an obnoxious "Hello World" program?

Posted 10 October 2013 - 07:36 AM

I don't think we will be able to outdo g00se's offering but you could always load the bytecode on the fly.

import java.math.BigInteger;

public class HelloWorld extends ClassLoader {
		
	public static void main(String[] args) throws Exception {
		new HelloWorld();
	}	
	
	public HelloWorld() throws Exception {
		BigInteger bytecode = new BigInteger("19wuzz7wj7bxaxce75ei5n1gp5pghtkdhqrduzncvg035kyizbvn5v56rkzxzxk4ddkcjygmhhduiykie5nd1z5sa9y919uc4oth7chu082644lb1s63rbvcr03m1m7zquaufe9i9ndsoy2ud5k5fh1fw6npviim1kx9nt7qfwnt7i67jk34vw57c5peu54zcn9fg4qikwhl7za40n6tfqnktzigfc4ncga7wo6ol9h859t5g2b4vzmqhromquz795iom4b2xbg3uqlktg9hje7xz9mhscvnlmh31jic8s5xk69uvglkvu8oft05q7bqrcdnhnaf4aoz3egwawhlzjp970mkltp0837qmmdspwgmzrx60b7pjx4rfbx0d8veegdwrmd7dcdy1ckpx6m0m2ra51lwa0dv4p1861wriwct5n59q9odczfamhufetpsfedl3brrsonu7f6ozlnez1hnj75bprekd2xuikdlbolb8k91a9wkf674izj90sipl0xt8978r6htusurv71929cmor8r0ycoa8ew25ra7jym6rl3zqzi9tzb1zcz48pajkk4o2rbmw75ksgm8zqouliozcespvk0t", 36);
		byte[] bytes = bytecode.toByteArray();
		
		Class<?> result = defineClass(null, bytes, 1, bytes.length-1);
		resolveClass(result);
		
		result.newInstance();
	}
	
}


I'n struggling to get a version that compiles and runs the java source in memory.
Was This Post Helpful? 0
  • +
  • -

#17 jon.kiparsky  Icon User is offline

  • Pancakes!
  • member icon


Reputation: 8011
  • View blog
  • Posts: 13,716
  • Joined: 19-March 11

Re: Anyone have an obnoxious "Hello World" program?

Posted 10 October 2013 - 07:57 AM

I believe - not sure, but I believe - that that is the sort of thing the java security model is designed to prevent.

Let us know if you manage it!
Was This Post Helpful? 0
  • +
  • -

#18 g00se  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2831
  • View blog
  • Posts: 11,995
  • Joined: 20-September 08

Re: Anyone have an obnoxious "Hello World" program?

Posted 10 October 2013 - 09:51 AM

Not entirely sure of what the goal is there, but is it something like this?

import java.lang.reflect.Method;

import java.net.URL;
import java.net.URLClassLoader;


public class HW2 {
    public static void main(String[] argv) throws Exception {
        URLClassLoader loader = new URLClassLoader(new URL[] {
                    new URL("http://technojeeves.com/tech")
                });
        Class<?> c = loader.loadClass("HW1");
        Method meth = c.getMethod("main", String[].class);
        String[] params = null;
        meth.invoke(null, (Object) params);
    }
}


Was This Post Helpful? 0
  • +
  • -

#19 cfoley  Icon User is offline

  • Cabbage
  • member icon

Reputation: 2069
  • View blog
  • Posts: 4,307
  • Joined: 11-December 07

Re: Anyone have an obnoxious "Hello World" program?

Posted 10 October 2013 - 11:12 AM

The goal is to take a String, compile it and load the class all in memory, without touching the file system.

With a lot of searching, I got this to work. A big caveat is that it needs to be executed with the JDK. The JRE returns null for the compiler for obvious reasons.

package cfoley.dynamicCompile;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.net.URI;
import java.util.Collections;

import javax.tools.FileObject;
import javax.tools.ForwardingJavaFileManager;
import javax.tools.JavaCompiler;
import javax.tools.JavaCompiler.CompilationTask;
import javax.tools.JavaFileManager;
import javax.tools.JavaFileObject;
import javax.tools.JavaFileObject.Kind;
import javax.tools.SimpleJavaFileObject;
import javax.tools.ToolProvider;

public class HelloWorld {
	
	private static final String className = "Hello";
	private static final URI uri = URI.create(className + ".java");
	private static final String source = 
			"public class Hello {public String toString() {return \"Hello World!\";}}";
	private static ByteArrayOutputStream bytecode = new ByteArrayOutputStream();
	
	public static void main(String[] args) throws ClassNotFoundException, InstantiationException, IllegalAccessException {
		compileSource();
		Class<?> c = loadClass();
		System.out.println(c.newInstance());
	}
	
	private static void compileSource() {
		JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
		
		JavaFileManager defaultFileManager = compiler.getStandardFileManager(null, null, null);
		JavaFileManager fileManager = new MyJavaFilleManager(defaultFileManager);
        
		CompilationTask task = compiler.getTask(null, fileManager, null, null, null, makeCompilationUnits());
		task.call();
	}
	
	static class MyJavaFilleManager extends ForwardingJavaFileManager<JavaFileManager> {
		protected MyJavaFilleManager(JavaFileManager delegate) {
			super(delegate);
		}

		public JavaFileObject getJavaFileForOutput(Location location, String className, Kind kind, FileObject sibling) throws IOException {
			return new SimpleJavaFileObject(uri, Kind.SOURCE) {
				public OutputStream openOutputStream() throws IOException {
					return bytecode;
				}
			};
		}
	}
	
	private static Iterable<JavaFileObject> makeCompilationUnits() {
		JavaFileObject x = new SimpleJavaFileObject(uri, Kind.SOURCE) {
			public CharSequence getCharContent(boolean ignoreEncodingErrors) throws IOException {
				return source;
			}
		};
		return Collections.singleton(x);
	}
	
	private static Class<?> loadClass() {
		MyClassLoader loader = new MyClassLoader();
		return loader.loadFromMemory(bytecode.toByteArray());
	}
	
	private static class MyClassLoader extends ClassLoader {
		Class<?> loadFromMemory(byte[]b) {
			return defineClass(null, b, 0, b.length);
		}
	}

}


This post has been edited by cfoley: 10 October 2013 - 11:14 AM

Was This Post Helpful? 0
  • +
  • -

#20 g00se  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2831
  • View blog
  • Posts: 11,995
  • Joined: 20-September 08

Re: Anyone have an obnoxious "Hello World" program?

Posted 10 October 2013 - 11:34 AM

Quote

compile it and load the class all in memory,
Oh OK. Of course my code also doesn't touch the file system, but of course doesn't compile it
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2