1,105,320 Community Members

Is It Possible To Run Code From a .txt File?

Member Avatar
Lemony Lime
Light Poster
25 posts since Dec 2011
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

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

Member Avatar
NormR1
Posting Sage
7,723 posts since Jun 2010
Reputation Points: 563 [?]
Q&As Helped to Solve: 793 [?]
Skill Endorsements: 16 [?]
Team Colleague
 
0
 

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.

Member Avatar
Ezzaral
Posting Sage
7,431 posts since May 2007
Reputation Points: 2,714 [?]
Q&As Helped to Solve: 953 [?]
Skill Endorsements: 31 [?]
Moderator
Featured
 
0
 

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

Member Avatar
Lemony Lime
Light Poster
25 posts since Dec 2011
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

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.

Member Avatar
Ezzaral
Posting Sage
7,431 posts since May 2007
Reputation Points: 2,714 [?]
Q&As Helped to Solve: 953 [?]
Skill Endorsements: 31 [?]
Moderator
Featured
 
0
 

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?

timmy568
Deleted Member
 
0
 

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

Member Avatar
Lemony Lime
Light Poster
25 posts since Dec 2011
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

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.

Member Avatar
Ezzaral
Posting Sage
7,431 posts since May 2007
Reputation Points: 2,714 [?]
Q&As Helped to Solve: 953 [?]
Skill Endorsements: 31 [?]
Moderator
Featured
 
0
 

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

Member Avatar
Lemony Lime
Light Poster
25 posts since Dec 2011
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

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

Member Avatar
Ezzaral
Posting Sage
7,431 posts since May 2007
Reputation Points: 2,714 [?]
Q&As Helped to Solve: 953 [?]
Skill Endorsements: 31 [?]
Moderator
Featured
 
0
 

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.

Member Avatar
thines01
Postaholic
2,420 posts since Oct 2009
Reputation Points: 389 [?]
Q&As Helped to Solve: 413 [?]
Skill Endorsements: 10 [?]
Team Colleague
Featured
 
0
 

...good way to make a virus or malware

Member Avatar
jwenting
duckman
8,330 posts since Nov 2004
Reputation Points: 1,399 [?]
Q&As Helped to Solve: 445 [?]
Skill Endorsements: 35 [?]
Infraction Points: 5
Team Colleague
 
0
 

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.

Member Avatar
Lemony Lime
Light Poster
25 posts since Dec 2011
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

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.)

Member Avatar
NormR1
Posting Sage
7,723 posts since Jun 2010
Reputation Points: 563 [?]
Q&As Helped to Solve: 793 [?]
Skill Endorsements: 16 [?]
Team Colleague
 
0
 

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

Read the API doc for the javac command.

Member Avatar
Lemony Lime
Light Poster
25 posts since Dec 2011
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

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.

Member Avatar
NormR1
Posting Sage
7,723 posts since Jun 2010
Reputation Points: 563 [?]
Q&As Helped to Solve: 793 [?]
Skill Endorsements: 16 [?]
Team Colleague
 
0
 

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.

Member Avatar
Lemony Lime
Light Poster
25 posts since Dec 2011
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

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.

Member Avatar
NormR1
Posting Sage
7,723 posts since Jun 2010
Reputation Points: 563 [?]
Q&As Helped to Solve: 793 [?]
Skill Endorsements: 16 [?]
Team Colleague
 
0
 

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.

Member Avatar
Lemony Lime
Light Poster
25 posts since Dec 2011
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

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?

Member Avatar
NormR1
Posting Sage
7,723 posts since Jun 2010
Reputation Points: 563 [?]
Q&As Helped to Solve: 793 [?]
Skill Endorsements: 16 [?]
Team Colleague
 
0
 

Read the API doc for the javac command to see how to set the classpath for it.

You
This article has been dead for over three months: Start a new discussion instead
Post:
Start New Discussion
View similar articles that have also been tagged: