I've done some searching, and it seems a few people have asked similar questions, but none of them were quite what I was looking for.

I need to be able to read lines of code from a .txt file, and then execute them in Java.

Or, if that's impossible, would there be a way to convert that text file into another file type, and then run the code from that?

The code in this text file is going to be user created with another program, so I want to keep this as simple as possible for them... if it's even possible. Also, I'm fairly new to programming, especially java, so please try to make it simple for me too. Haha

Edited 5 Years Ago by Lemony Lime: n/a

I need to be able to read lines of code from a .txt file, and then execute them in Java.

Yes, you could use classes that implement the JavaCompiler interface to compile a java program within your running java program and have the class files created available for using in your program.

It's not simple.

What "other program" is generating the code? Why the need for two programs?

It's a mod for Minecraft. My program allows you to draw the building outside of the game, then save the code for that building into a text file, and then Minecraft will run the code from that text file to create your building in the game.

Edited 5 Years Ago by Lemony Lime: n/a

If Minecraft is executing the code, then all your program needs to do is write the correct instructions to a text file and send it along. Right?

Why would you need to execute it?

Sorry im no help, but if you do find a way to do this, message me :D

If Minecraft is executing the code, then all your program needs to do is write the correct instructions to a text file and send it along. Right?

Why would you need to execute it?

I may be using the wrong terminology I guess.

I just need to place the code from the text file into minecraft, and then then let minecraft do it's thing like normal.

Then you need to read up on the APIs and tools Minecraft (may) provide for such things. This isn't really a Java question.

Minecraft modding is done in Java, so I figured it'd just be a regular Java thing.

Java is a general programming language. Minecraft is an application written in Java and any interfaces available to it would be those created by the writers or other modders.

You need to hit the Minecraft wiki/forums/docs etc for more specific information.

Edited 5 Years Ago by Ezzaral: n/a

you can indeed call the Java compiler API on the fly from Java code. That's how JSP engines (for example) work to provide runtime compilation of generated Java source into classfiles.

Okay, I figured it, but I've run into one last problem that I haven't been able to figure out.

So, what my program does now, is write everything it needs into a .java file, run a batch file to compile the .java file into a .class file, and put it straight into the minecraft.jar, all with one button.

However, when I compile it, I get a ton of errors, because it has minecraft code, but is being compiled separately from minecraft. So, from what I can figure out, I need to set the classpath to use minecraft.jar while compiling... but this is where I get lost. Like I said, I'm just now learning java, so I don't really understand the javac terminology and stuff. =/ (My program is written in python.)

Edited 5 Years Ago by Lemony Lime: n/a

need to set the classpath to use minecraft.jar while compiling.

Read the API doc for the javac command.

I did, that's how I figured out that this is what I need to do. I've tried several different things based on what I read there, but none of them work. It always says that the java file I made doesn't exist, when it definitely does.

Like I said, I'm not actually coding in java, I just have to do this because my program works with minecraft... so I don't really know what I'm doing.

Edited 5 Years Ago by Lemony Lime: n/a

It always says that the java file I made doesn't exist

If you get error messages, you need to post the full text of the messages if you want any help with them. You also need to show the command you entered and the contents of the folder where the command was entered.

To copy the contents of the command prompt window:
Click on Icon in upper left corner
Select Edit
Select 'Select All' - The selection will show
Click in upper left again
Select Edit and click 'Copy'

Paste here.

Well, when I just write javac myfile.java

It gives me this error:

LLBLOCK1.java:3: error: cannot find symbol
public class LLBLOCK1 extends Block
                              ^
  symbol: class Block
LLBLOCK1.java:5: error: cannot find symbol
    private World worldObj;
            ^
  symbol:   class World
  location: class LLBLOCK1
LLBLOCK1.java:12: error: cannot find symbol
        public boolean blockActivated(World world, int i, int j, int k, EntityPlayer entityplayer)
                                      ^
  symbol:   class World
  location: class LLBLOCK1
LLBLOCK1.java:12: error: cannot find symbol
        public boolean blockActivated(World world, int i, int j, int k, EntityPlayer entityplayer)
                                                                        ^
  symbol:   class EntityPlayer
  location: class LLBLOCK1
LLBLOCK1.java:9: error: cannot find symbol
        super(i, j, Material.wood);
                    ^
  symbol:   variable Material
  location: class LLBLOCK1
LLBLOCK1.java:14: error: package Block does not exist
world.setBlockWithNotify(i + 0, j + 0, k + 0, Block.stone.blockID);
                                                   ^
6 errors

Which I think is because it's being compiled away from the minecraft.jar, and I honestly don't know what to write when specifying the path. I'm not going to know where this program is going to be installed, so I need to write it in a way that will work with everyone's computer.

The "cannot find symbol" error says the compiler can not find the definition for that class named in the error message.
Where are those classes defined?
You need to put them on the classpath for the javac command so it can find them.

Edited 5 Years Ago by NormR1: n/a

I'm assuming they're in the minecraft.jar (which, on my computer, is located at: C:\Users\Jonathan\AppData\Roaming\.minecraft\bin\minecraft.jar). So how do I set the classpath for this?

I just told you that I did, and don't understand it.

Edited 5 Years Ago by Lemony Lime: n/a

What have you tried?
You just code the option followed by the path to the jar file.

-cp path or -classpath path
Specify where to find user class files, and (optionally) annotation processors and source files. This classpath overrides the user class path in the CLASSPATH environment variable. If neither CLASSPATH, -cp nor -classpath is specified, the user class path consists of the current directory. See Setting the Class Path for more details.

I don't understand how you could execute any programs that require jar files if you don't know how to specify the classpath. Don't you need to set it for to execute minecraft?

This is what I tried, and it gives me the same error as before: javac -classpath %appdata%\.minecraft\bin\minecraft.jar LLBLOCK1.java

LLBLOCK1.java is in the same directory as the batch file that's running this, so I'm not sure if I have to specify a path to it. I've tried it with and without the exact path though.

Minecraft has an .exe to launch it, so I've never had to do that, no.

gives me the same error as before

Can you post the full contents of the console screen when you execute the command?

For testing don't use the %variable% use the path.
What is the . after the first \ ?

Can you post the full contents of the console screen when you execute the command?

LLBLOCK1.java:3: error: cannot find symbol
public class LLBLOCK1 extends Block
                              ^
  symbol: class Block
LLBLOCK1.java:5: error: cannot find symbol
    private World worldObj;
            ^
  symbol:   class World
  location: class LLBLOCK1
LLBLOCK1.java:12: error: cannot find symbol
        public boolean blockActivated(World world, int i, int j, int k, EntityPlayer entityplayer)
                                      ^
  symbol:   class World
  location: class LLBLOCK1
LLBLOCK1.java:12: error: cannot find symbol
        public boolean blockActivated(World world, int i, int j, int k, EntityPlayer entityplayer)
                                                                        ^
  symbol:   class EntityPlayer
  location: class LLBLOCK1
LLBLOCK1.java:9: error: cannot find symbol
        super(i, j, Material.wood);
                    ^
  symbol:   variable Material
  location: class LLBLOCK1
LLBLOCK1.java:14: error: package Block does not exist
world.setBlockWithNotify(i + 0, j + 0, k + 0, Block.stone.blockID);
                                                   ^
6 errors

What is the . after the first \ ?

That folder is actually named ".minecraft". I'm not sure why it has the . but it does.

For testing don't use the %variable% use the path.

Okay. I tried that too, and it was the same result.

Edited 5 Years Ago by Lemony Lime: n/a

You do NOT show the full contents of the console. You have editted out the command you are executing. The full contents would look like this:


Microsoft Windows XP [Version 5.1.2600]

D:\JavaDevelopment\Testing\ForumQuestions7>java TestCode8
a->z
b->y
c->x
...
y->b
z->a

D:\JavaDevelopment\Testing\ForumQuestions7>

Oh, I'm running it through eclipse, and that's all the built in console showed. Didn't realize it wasn't the whole thing.

I'll do it with the actual command prompt real quick.

Microsoft Windows [Version 6.1.7600]
Copyright (c) 2009 Microsoft Corporation.  All rights reserved.

C:\Users\Jonathan>cd desktop\mcblueprint\eclipse\mcblueprint\src

C:\Users\Jonathan\Desktop\MCBlueprint\Eclipse\MCBlueprint\src>javac -classpath C
:\users\Jonathan\AppData\Roaming\.minecraft\bin\minecraft.jar LLBLOCK1.java
LLBLOCK1.java:3: error: cannot find symbol
public class LLBLOCK1 extends Block
                              ^
  symbol: class Block
LLBLOCK1.java:5: error: cannot find symbol
    private World worldObj;
            ^
  symbol:   class World
  location: class LLBLOCK1
LLBLOCK1.java:12: error: cannot find symbol
        public boolean blockActivated(World world, int i, int j, int k, EntityPl
ayer entityplayer)
                                      ^
  symbol:   class World
  location: class LLBLOCK1
LLBLOCK1.java:12: error: cannot find symbol
        public boolean blockActivated(World world, int i, int j, int k, EntityPl
ayer entityplayer)
                                                                        ^
  symbol:   class EntityPlayer
  location: class LLBLOCK1
LLBLOCK1.java:9: error: cannot find symbol
        super(i, j, Material.wood);
                    ^
  symbol:   variable Material
  location: class LLBLOCK1
LLBLOCK1.java:14: error: package Block does not exist
world.setBlockWithNotify(i + 0, j + 0, k + 0, Block.stone.blockID);
                                                   ^
6 errors

C:\Users\Jonathan\Desktop\MCBlueprint\Eclipse\MCBlueprint\src>

Edited 5 Years Ago by Lemony Lime: n/a

It would look like the Block class etc are not in the minecraft.jar file.

A test would be to copy the minecraft.jar file to the same folder with the source file and try the command:
javac -classpath minecraft.jar LLBLOCK1.java

yeah, I get the same error.

There's also jinput.jar, lwjgl.jar, and lwjgl_util.jar in the same location as the minecraft.jar, but I tried all of these already and it didn't work.

Edited 5 Years Ago by Lemony Lime: n/a

You need to find which jar file has the Block class. Use any Zip file utility to read the jar files.

This article has been dead for over six months. Start a new discussion instead.